Skip to content

Use C size for serialization/deserialization#208

Merged
nunoplopes merged 11 commits into
Cpp2Rust:masterfrom
lucic71:byte-size
Jun 26, 2026
Merged

Use C size for serialization/deserialization#208
nunoplopes merged 11 commits into
Cpp2Rust:masterfrom
lucic71:byte-size

Conversation

@lucic71

@lucic71 lucic71 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

This adds a new method in ByteRepr called byte_size. It is used to describe the C/C++ byte size of the object and it's used for serializatoin/deserialization through from/to_bytes.

In refcount, Rust structs have a different size than the C/C++ structs because each field is wrapped in Value<>. For example:

struct S {
  int32_t a;
  int32_t b;
}
sizeof(S) == 8

becomes

struct S {
  a: Value<i32>,
  b: Value<i32>
}
size_of(S) == 16 // one Rc is 8

This difference in size breaks ByteRepr for structs. To do correct serialization we need the C/C++ size. Codegen embeds this into the generated code using the ByteRepr::byte_size method.

Then, when a reinterpreted pointer is created, ReinterpretedView::elem_byte_size saves ByteRepr::byte_size. This is an optimization so that the ByteRepr trait is not exposed to all Ptr methods, even into methods that don't have to do with reinterpreted pointers.

The goal of this is to support structs inside unions. Structs, as any other type that takes part in an union, is a reinterpreted view into the byte buffer of the union.

@lucic71 lucic71 force-pushed the byte-size branch 4 times, most recently from 6c74dba to e6a5b18 Compare June 25, 2026 15:39
@lucic71 lucic71 marked this pull request as draft June 25, 2026 16:27
@lucic71 lucic71 marked this pull request as ready for review June 25, 2026 16:39
@nunoplopes nunoplopes merged commit d82d983 into Cpp2Rust:master Jun 26, 2026
9 checks passed
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