Skip to content

Commit 49061f8

Browse files
authored
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
1 parent 807adb7 commit 49061f8

File tree

22 files changed

+89
-1
lines changed

22 files changed

+89
-1
lines changed

rust/flatbuffers/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use crate::builder::{Allocator, DefaultAllocator, FlatBufferBuilder};
5252
pub use crate::endian_scalar::{emplace_scalar, read_scalar, read_scalar_at, EndianScalar};
5353
pub use crate::follow::{Follow, FollowStart};
5454
pub use crate::primitives::*;
55-
pub use crate::push::Push;
55+
pub use crate::push::{Push, PushAlignment};
5656
pub use crate::table::{buffer_has_identifier, Table};
5757
pub use crate::vector::{follow_cast_ref, Vector, VectorIter};
5858
pub use crate::verifier::{

samples/rust_generated/my_game/sample/vec_3_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ impl<'b> flatbuffers::Push for Vec3 {
5050
let src = ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size());
5151
dst.copy_from_slice(src);
5252
}
53+
#[inline]
54+
fn alignment() -> flatbuffers::PushAlignment {
55+
flatbuffers::PushAlignment::new(4)
56+
}
5357
}
5458

5559
impl<'a> flatbuffers::Verifiable for Vec3 {

src/idl_gen_rust.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,10 @@ class RustGenerator : public BaseGenerator {
27032703
"{{STRUCT_TY}} as *const u8, Self::size());";
27042704
code_ += " dst.copy_from_slice(src);";
27052705
code_ += " }";
2706+
code_ += " #[inline]";
2707+
code_ += " fn alignment() -> flatbuffers::PushAlignment {";
2708+
code_ += " flatbuffers::PushAlignment::new({{ALIGN}})";
2709+
code_ += " }";
27062710
code_ += "}";
27072711
code_ += "";
27082712

tests/arrays_test/my_game/example/array_struct_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ impl<'b> flatbuffers::Push for ArrayStruct {
5353
let src = ::core::slice::from_raw_parts(self as *const ArrayStruct as *const u8, Self::size());
5454
dst.copy_from_slice(src);
5555
}
56+
#[inline]
57+
fn alignment() -> flatbuffers::PushAlignment {
58+
flatbuffers::PushAlignment::new(8)
59+
}
5660
}
5761

5862
impl<'a> flatbuffers::Verifiable for ArrayStruct {

tests/arrays_test/my_game/example/nested_struct_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl<'b> flatbuffers::Push for NestedStruct {
5151
let src = ::core::slice::from_raw_parts(self as *const NestedStruct as *const u8, Self::size());
5252
dst.copy_from_slice(src);
5353
}
54+
#[inline]
55+
fn alignment() -> flatbuffers::PushAlignment {
56+
flatbuffers::PushAlignment::new(8)
57+
}
5458
}
5559

5660
impl<'a> flatbuffers::Verifiable for NestedStruct {

tests/include_test1/my_game/other_name_space/unused_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for Unused {
4848
let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, Self::size());
4949
dst.copy_from_slice(src);
5050
}
51+
#[inline]
52+
fn alignment() -> flatbuffers::PushAlignment {
53+
flatbuffers::PushAlignment::new(4)
54+
}
5155
}
5256

5357
impl<'a> flatbuffers::Verifiable for Unused {

tests/include_test2/my_game/other_name_space/unused_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for Unused {
4848
let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, Self::size());
4949
dst.copy_from_slice(src);
5050
}
51+
#[inline]
52+
fn alignment() -> flatbuffers::PushAlignment {
53+
flatbuffers::PushAlignment::new(4)
54+
}
5155
}
5256

5357
impl<'a> flatbuffers::Verifiable for Unused {

tests/monster_test/my_game/example/ability_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl<'b> flatbuffers::Push for Ability {
4949
let src = ::core::slice::from_raw_parts(self as *const Ability as *const u8, Self::size());
5050
dst.copy_from_slice(src);
5151
}
52+
#[inline]
53+
fn alignment() -> flatbuffers::PushAlignment {
54+
flatbuffers::PushAlignment::new(4)
55+
}
5256
}
5357

5458
impl<'a> flatbuffers::Verifiable for Ability {

tests/monster_test/my_game/example/struct_of_structs_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ impl<'b> flatbuffers::Push for StructOfStructs {
5050
let src = ::core::slice::from_raw_parts(self as *const StructOfStructs as *const u8, Self::size());
5151
dst.copy_from_slice(src);
5252
}
53+
#[inline]
54+
fn alignment() -> flatbuffers::PushAlignment {
55+
flatbuffers::PushAlignment::new(4)
56+
}
5357
}
5458

5559
impl<'a> flatbuffers::Verifiable for StructOfStructs {

tests/monster_test/my_game/example/struct_of_structs_of_structs_generated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl<'b> flatbuffers::Push for StructOfStructsOfStructs {
4848
let src = ::core::slice::from_raw_parts(self as *const StructOfStructsOfStructs as *const u8, Self::size());
4949
dst.copy_from_slice(src);
5050
}
51+
#[inline]
52+
fn alignment() -> flatbuffers::PushAlignment {
53+
flatbuffers::PushAlignment::new(4)
54+
}
5155
}
5256

5357
impl<'a> flatbuffers::Verifiable for StructOfStructsOfStructs {

0 commit comments

Comments
 (0)