Emit `private` and `protected` fields as opaque blobs of bytes. Before this CL, fields with a type that is unsupported Crubit would be emitted as a private field that is backed by an opaque blob of bytes (represented by a Rust array `[u8; N]` of appropriate size). After this CL, `private` and `protected` fields with a Crubit-supported type are also emitted as an opaque blob of bytes. This CL is desirable as a performance optimization - e.g. to avoid generating bindings for template instantiations that are only ever used in non-public fields. PiperOrigin-RevId: 452627863
diff --git a/rs_bindings_from_cc/src_code_gen.rs b/rs_bindings_from_cc/src_code_gen.rs index 2794f66..3e73c72 100644 --- a/rs_bindings_from_cc/src_code_gen.rs +++ b/rs_bindings_from_cc/src_code_gen.rs
@@ -2934,12 +2934,14 @@ rs_api, quote! { #[derive(Clone, Copy)] - #[repr(C)] + #[repr(C, align(4))] pub struct SomeStruct { __non_field_data: [crate::rust_std::mem::MaybeUninit<u8>; 0], pub public_int: i32, - protected_int: i32, - private_int: i32, + #[doc = " Reason for representing this field as a blob of bytes:\n Types of non-public C++ fields can be elided away"] + protected_int: [crate::rust_std::mem::MaybeUninit<u8>; 4], + #[doc = " Reason for representing this field as a blob of bytes:\n Types of non-public C++ fields can be elided away"] + private_int: [crate::rust_std::mem::MaybeUninit<u8>; 4], } } ); @@ -4276,10 +4278,11 @@ rs_api, quote! { #[derive(Clone, Copy)] - #[repr(C)] + #[repr(C, align(8))] pub union SomeUnionWithPrivateFields { pub public_field: i32, - private_field: i64, + #[doc = " Reason for representing this field as a blob of bytes:\n Types of non-public C++ fields can be elided away"] + private_field: [crate::rust_std::mem::MaybeUninit<u8>; 8], } } );