From 49061f8c7c99363eeea25c5e4337ebb499928467 Mon Sep 17 00:00:00 2001 From: Benjamin Kietzman Date: Mon, 28 Oct 2024 10:42:55 -0500 Subject: [PATCH] use ALIGN for Push::alignment in struct types (#8398) * use ALIGN for Push::alignment in struct types * regenerate and add a test for struct alignment --- rust/flatbuffers/src/lib.rs | 2 +- samples/rust_generated/my_game/sample/vec_3_generated.rs | 4 ++++ src/idl_gen_rust.cpp | 4 ++++ .../arrays_test/my_game/example/array_struct_generated.rs | 4 ++++ .../my_game/example/nested_struct_generated.rs | 4 ++++ .../my_game/other_name_space/unused_generated.rs | 4 ++++ .../my_game/other_name_space/unused_generated.rs | 4 ++++ tests/monster_test/my_game/example/ability_generated.rs | 4 ++++ .../my_game/example/struct_of_structs_generated.rs | 4 ++++ .../example/struct_of_structs_of_structs_generated.rs | 4 ++++ tests/monster_test/my_game/example/test_generated.rs | 4 ++++ tests/monster_test/my_game/example/vec_3_generated.rs | 4 ++++ .../my_game/other_name_space/unused_generated.rs | 4 ++++ .../my_game/example/ability_generated.rs | 4 ++++ .../my_game/example/struct_of_structs_generated.rs | 4 ++++ .../example/struct_of_structs_of_structs_generated.rs | 4 ++++ .../my_game/example/test_generated.rs | 4 ++++ .../my_game/example/vec_3_generated.rs | 4 ++++ .../my_game/other_name_space/unused_generated.rs | 4 ++++ .../namespace_b/struct_in_nested_ns_generated.rs | 4 ++++ tests/private_annotation_test/object_generated.rs | 4 ++++ tests/rust_usage_test/tests/integration_test.rs | 8 ++++++++ 22 files changed, 89 insertions(+), 1 deletion(-) diff --git a/rust/flatbuffers/src/lib.rs b/rust/flatbuffers/src/lib.rs index 7ace52682..1ded36817 100644 --- a/rust/flatbuffers/src/lib.rs +++ b/rust/flatbuffers/src/lib.rs @@ -52,7 +52,7 @@ pub use crate::builder::{Allocator, DefaultAllocator, FlatBufferBuilder}; pub use crate::endian_scalar::{emplace_scalar, read_scalar, read_scalar_at, EndianScalar}; pub use crate::follow::{Follow, FollowStart}; pub use crate::primitives::*; -pub use crate::push::Push; +pub use crate::push::{Push, PushAlignment}; pub use crate::table::{buffer_has_identifier, Table}; pub use crate::vector::{follow_cast_ref, Vector, VectorIter}; pub use crate::verifier::{ diff --git a/samples/rust_generated/my_game/sample/vec_3_generated.rs b/samples/rust_generated/my_game/sample/vec_3_generated.rs index 185677f89..a7a110077 100644 --- a/samples/rust_generated/my_game/sample/vec_3_generated.rs +++ b/samples/rust_generated/my_game/sample/vec_3_generated.rs @@ -50,6 +50,10 @@ impl<'b> flatbuffers::Push for Vec3 { let src = ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Vec3 { diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index a85a7812d..946881f51 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -2703,6 +2703,10 @@ class RustGenerator : public BaseGenerator { "{{STRUCT_TY}} as *const u8, Self::size());"; code_ += " dst.copy_from_slice(src);"; code_ += " }"; + code_ += " #[inline]"; + code_ += " fn alignment() -> flatbuffers::PushAlignment {"; + code_ += " flatbuffers::PushAlignment::new({{ALIGN}})"; + code_ += " }"; code_ += "}"; code_ += ""; diff --git a/tests/arrays_test/my_game/example/array_struct_generated.rs b/tests/arrays_test/my_game/example/array_struct_generated.rs index 31e1f9ccc..d727b6961 100644 --- a/tests/arrays_test/my_game/example/array_struct_generated.rs +++ b/tests/arrays_test/my_game/example/array_struct_generated.rs @@ -53,6 +53,10 @@ impl<'b> flatbuffers::Push for ArrayStruct { let src = ::core::slice::from_raw_parts(self as *const ArrayStruct as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(8) + } } impl<'a> flatbuffers::Verifiable for ArrayStruct { diff --git a/tests/arrays_test/my_game/example/nested_struct_generated.rs b/tests/arrays_test/my_game/example/nested_struct_generated.rs index 0f1f54467..5dfa0dbd8 100644 --- a/tests/arrays_test/my_game/example/nested_struct_generated.rs +++ b/tests/arrays_test/my_game/example/nested_struct_generated.rs @@ -51,6 +51,10 @@ impl<'b> flatbuffers::Push for NestedStruct { let src = ::core::slice::from_raw_parts(self as *const NestedStruct as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(8) + } } impl<'a> flatbuffers::Verifiable for NestedStruct { diff --git a/tests/include_test1/my_game/other_name_space/unused_generated.rs b/tests/include_test1/my_game/other_name_space/unused_generated.rs index 69be4869e..012df93ed 100644 --- a/tests/include_test1/my_game/other_name_space/unused_generated.rs +++ b/tests/include_test1/my_game/other_name_space/unused_generated.rs @@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for Unused { let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Unused { diff --git a/tests/include_test2/my_game/other_name_space/unused_generated.rs b/tests/include_test2/my_game/other_name_space/unused_generated.rs index 69be4869e..012df93ed 100644 --- a/tests/include_test2/my_game/other_name_space/unused_generated.rs +++ b/tests/include_test2/my_game/other_name_space/unused_generated.rs @@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for Unused { let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Unused { diff --git a/tests/monster_test/my_game/example/ability_generated.rs b/tests/monster_test/my_game/example/ability_generated.rs index 042df75f1..25a423c54 100644 --- a/tests/monster_test/my_game/example/ability_generated.rs +++ b/tests/monster_test/my_game/example/ability_generated.rs @@ -49,6 +49,10 @@ impl<'b> flatbuffers::Push for Ability { let src = ::core::slice::from_raw_parts(self as *const Ability as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Ability { diff --git a/tests/monster_test/my_game/example/struct_of_structs_generated.rs b/tests/monster_test/my_game/example/struct_of_structs_generated.rs index 77410a674..82d7e48d2 100644 --- a/tests/monster_test/my_game/example/struct_of_structs_generated.rs +++ b/tests/monster_test/my_game/example/struct_of_structs_generated.rs @@ -50,6 +50,10 @@ impl<'b> flatbuffers::Push for StructOfStructs { let src = ::core::slice::from_raw_parts(self as *const StructOfStructs as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for StructOfStructs { diff --git a/tests/monster_test/my_game/example/struct_of_structs_of_structs_generated.rs b/tests/monster_test/my_game/example/struct_of_structs_of_structs_generated.rs index 4e4597f7b..6fcb0d491 100644 --- a/tests/monster_test/my_game/example/struct_of_structs_of_structs_generated.rs +++ b/tests/monster_test/my_game/example/struct_of_structs_of_structs_generated.rs @@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for StructOfStructsOfStructs { let src = ::core::slice::from_raw_parts(self as *const StructOfStructsOfStructs as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for StructOfStructsOfStructs { diff --git a/tests/monster_test/my_game/example/test_generated.rs b/tests/monster_test/my_game/example/test_generated.rs index 98494510b..b2bf71738 100644 --- a/tests/monster_test/my_game/example/test_generated.rs +++ b/tests/monster_test/my_game/example/test_generated.rs @@ -49,6 +49,10 @@ impl<'b> flatbuffers::Push for Test { let src = ::core::slice::from_raw_parts(self as *const Test as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(2) + } } impl<'a> flatbuffers::Verifiable for Test { diff --git a/tests/monster_test/my_game/example/vec_3_generated.rs b/tests/monster_test/my_game/example/vec_3_generated.rs index b4f076522..686560303 100644 --- a/tests/monster_test/my_game/example/vec_3_generated.rs +++ b/tests/monster_test/my_game/example/vec_3_generated.rs @@ -53,6 +53,10 @@ impl<'b> flatbuffers::Push for Vec3 { let src = ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(8) + } } impl<'a> flatbuffers::Verifiable for Vec3 { diff --git a/tests/monster_test/my_game/other_name_space/unused_generated.rs b/tests/monster_test/my_game/other_name_space/unused_generated.rs index 69be4869e..012df93ed 100644 --- a/tests/monster_test/my_game/other_name_space/unused_generated.rs +++ b/tests/monster_test/my_game/other_name_space/unused_generated.rs @@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for Unused { let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Unused { diff --git a/tests/monster_test_serialize/my_game/example/ability_generated.rs b/tests/monster_test_serialize/my_game/example/ability_generated.rs index 46d24bfee..076f85eb4 100644 --- a/tests/monster_test_serialize/my_game/example/ability_generated.rs +++ b/tests/monster_test_serialize/my_game/example/ability_generated.rs @@ -51,6 +51,10 @@ impl<'b> flatbuffers::Push for Ability { let src = ::core::slice::from_raw_parts(self as *const Ability as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Ability { diff --git a/tests/monster_test_serialize/my_game/example/struct_of_structs_generated.rs b/tests/monster_test_serialize/my_game/example/struct_of_structs_generated.rs index 669a6c865..5e6cdf9c6 100644 --- a/tests/monster_test_serialize/my_game/example/struct_of_structs_generated.rs +++ b/tests/monster_test_serialize/my_game/example/struct_of_structs_generated.rs @@ -52,6 +52,10 @@ impl<'b> flatbuffers::Push for StructOfStructs { let src = ::core::slice::from_raw_parts(self as *const StructOfStructs as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for StructOfStructs { diff --git a/tests/monster_test_serialize/my_game/example/struct_of_structs_of_structs_generated.rs b/tests/monster_test_serialize/my_game/example/struct_of_structs_of_structs_generated.rs index 2cb1b5e02..cf7af52b0 100644 --- a/tests/monster_test_serialize/my_game/example/struct_of_structs_of_structs_generated.rs +++ b/tests/monster_test_serialize/my_game/example/struct_of_structs_of_structs_generated.rs @@ -50,6 +50,10 @@ impl<'b> flatbuffers::Push for StructOfStructsOfStructs { let src = ::core::slice::from_raw_parts(self as *const StructOfStructsOfStructs as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for StructOfStructsOfStructs { diff --git a/tests/monster_test_serialize/my_game/example/test_generated.rs b/tests/monster_test_serialize/my_game/example/test_generated.rs index 835a90545..0096f5735 100644 --- a/tests/monster_test_serialize/my_game/example/test_generated.rs +++ b/tests/monster_test_serialize/my_game/example/test_generated.rs @@ -51,6 +51,10 @@ impl<'b> flatbuffers::Push for Test { let src = ::core::slice::from_raw_parts(self as *const Test as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(2) + } } impl<'a> flatbuffers::Verifiable for Test { diff --git a/tests/monster_test_serialize/my_game/example/vec_3_generated.rs b/tests/monster_test_serialize/my_game/example/vec_3_generated.rs index c080dd742..7005cc050 100644 --- a/tests/monster_test_serialize/my_game/example/vec_3_generated.rs +++ b/tests/monster_test_serialize/my_game/example/vec_3_generated.rs @@ -55,6 +55,10 @@ impl<'b> flatbuffers::Push for Vec3 { let src = ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(8) + } } impl<'a> flatbuffers::Verifiable for Vec3 { diff --git a/tests/monster_test_serialize/my_game/other_name_space/unused_generated.rs b/tests/monster_test_serialize/my_game/other_name_space/unused_generated.rs index b676318f7..5fdcc86fc 100644 --- a/tests/monster_test_serialize/my_game/other_name_space/unused_generated.rs +++ b/tests/monster_test_serialize/my_game/other_name_space/unused_generated.rs @@ -50,6 +50,10 @@ impl<'b> flatbuffers::Push for Unused { let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Unused { diff --git a/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs b/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs index f2a15f4be..73229bf1c 100644 --- a/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs +++ b/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs @@ -49,6 +49,10 @@ impl<'b> flatbuffers::Push for StructInNestedNS { let src = ::core::slice::from_raw_parts(self as *const StructInNestedNS as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for StructInNestedNS { diff --git a/tests/private_annotation_test/object_generated.rs b/tests/private_annotation_test/object_generated.rs index cb93412f3..92a31449d 100644 --- a/tests/private_annotation_test/object_generated.rs +++ b/tests/private_annotation_test/object_generated.rs @@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for Object { let src = ::core::slice::from_raw_parts(self as *const Object as *const u8, Self::size()); dst.copy_from_slice(src); } + #[inline] + fn alignment() -> flatbuffers::PushAlignment { + flatbuffers::PushAlignment::new(4) + } } impl<'a> flatbuffers::Verifiable for Object { diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs index 89cacc171..a1d5a02b5 100644 --- a/tests/rust_usage_test/tests/integration_test.rs +++ b/tests/rust_usage_test/tests/integration_test.rs @@ -2422,6 +2422,14 @@ mod push_impls { 0, 0, 0, 0, 0, 0, 0, 1]); } + + #[test] + fn push_u8_generated_struct_alignment() { + let mut b = flatbuffers::FlatBufferBuilder::new(); + b.push(1u8); + b.push(my_game::example::Test::new(10, 20)); + check(&b, &[10, 0, 20, 0, 0, 1]); + } } #[cfg(test)]