Skip to content

Use reinterpret_sized for array translation in unions#221

Closed
lucic71 wants to merge 8 commits into
Cpp2Rust:masterfrom
lucic71:reinterpret-sized
Closed

Use reinterpret_sized for array translation in unions#221
lucic71 wants to merge 8 commits into
Cpp2Rust:masterfrom
lucic71:reinterpret-sized

Conversation

@lucic71

@lucic71 lucic71 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Box<[T]>::byte_size cannot be implemented because [T] is not Sized. For dynamic types, like Box<[T]> use Ptr::reinterpret_sized(usize) instead of Ptr::reinterpret_cast(). The usize argument of reinterpret_sized encodes the C size of the dynamic Rust object.

Internally, reinterpret_cast calls reinterpret_sized(U::byte_size()).

Box<[T]>::to_bytes and Box<[T]>::from_bytes can be implemented and they are required in order to read and write a reinterpreted array.

@lucic71 lucic71 force-pushed the reinterpret-sized branch from 55c6ad4 to 6823a43 Compare July 1, 2026 15:42
@nunoplopes

Copy link
Copy Markdown
Contributor

I'm not following this set of patches.
Shouldn't raw_()return Ptr<u8> instead of Ptr<Box<[u8]>>?

The pointer can access the byte size. Rust knows the size, so I don't understand why we need to store it ourselves.

@lucic71

lucic71 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

I'm not following this set of patches.

After #205 in which I translated unions as a byte array + Ptr accessors, I focused on adding ByteRepr for types that can take part in unions: enums, structs, pointers, arrays.

The current PR adds translation for array members. But I think I over-engineered this. Ptr<T> instead of Ptr<Box<[u8]>> is enough. My reasoning was that the accessor must return a pointer to the declared member type. But in the case of arrays, returning Ptr<T> is easier and actually faster because Ptr<T> doesn't have to materialize the entire array, just one element.

Shouldn't raw_()return Ptr<u8> instead of Ptr<Box<[u8]>>?

The pointer can access the byte size. Rust knows the size, so I don't understand why we need to store it ourselves.

I will close this PR and add one more small commit over #218 to have array members translated as Ptr<T>

@lucic71 lucic71 closed this Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants