diff --git a/rust/flatbuffers/src/lib.rs b/rust/flatbuffers/src/lib.rs index 7ace5268263..1ded368171c 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 185677f8946..a7a110077bf 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 a85a7812dc6..946881f51d8 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 31e1f9ccc84..d727b696128 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 0f1f54467ec..5dfa0dbd805 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 69be4869eec..012df93ed48 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 69be4869eec..012df93ed48 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 042df75f162..25a423c54cd 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 77410a6744d..82d7e48d271 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 4e4597f7b18..6fcb0d49180 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 98494510b71..b2bf7173865 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 b4f076522e8..68656030343 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 69be4869eec..012df93ed48 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 46d24bfeed5..076f85eb47c 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 669a6c8651d..5e6cdf9c682 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 2cb1b5e023a..cf7af52b090 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 835a905455b..0096f573575 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 c080dd7429f..7005cc0506d 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 b676318f7c6..5fdcc86fc33 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 f2a15f4beda..73229bf1c98 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 cb93412f3d5..92a31449d93 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 89cacc1713a..a1d5a02b532 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)]