Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ Some tips for good pull requests:
# The small print
Contributions made by corporations are covered by a different agreement than
the one above, the Software Grant and Corporate Contributor License Agreement.

# Code

TL/DR

See [how to build flatc](https://flatbuffers.dev/building/).

When making changes, build `flatc` and then re-generate the goldens files to see the effect of your changes:

```
$ cp build/flatc .
$ goldens/generate_goldens.py
```

Re-generate other code files to see the effects of the changes:

```
$ scripts/generate_code.py
```

Run tests with [TestAll.sh](tests/TestAll.sh) in [tests](tests), or directly any of the sub-scripts run by it.

[Format the code](Formatters.md) before submitting a PR.
4 changes: 2 additions & 2 deletions goldens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This directory is a repository for the generated files of `flatc`.

We check in the generated code so we can see, during a PR review, how the
changes affect the generated output. Its also useful as a reference to point too
as how things work across various languages.
changes affect the generated output. It's also useful as a reference to show
how things work across various languages.

These files are **NOT** intended to be depended on by any code, such as tests or
or compiled examples.
Expand Down
8 changes: 4 additions & 4 deletions goldens/rust/basic_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a> flatbuffers::Follow<'a> for Galaxy<'a> {
type Inner = Galaxy<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { _tab: flatbuffers::Table::new(buf, loc) }
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
}
}

Expand Down Expand Up @@ -134,7 +134,7 @@ impl<'a> flatbuffers::Follow<'a> for Universe<'a> {
type Inner = Universe<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { _tab: flatbuffers::Table::new(buf, loc) }
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
}
}

Expand Down Expand Up @@ -288,14 +288,14 @@ pub fn size_prefixed_root_as_universe_with_opts<'b, 'o>(
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid `Universe`.
pub unsafe fn root_as_universe_unchecked(buf: &[u8]) -> Universe {
flatbuffers::root_unchecked::<Universe>(buf)
unsafe { flatbuffers::root_unchecked::<Universe>(buf) }
}
#[inline]
/// Assumes, without verification, that a buffer of bytes contains a size prefixed Universe and returns it.
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid size prefixed `Universe`.
pub unsafe fn size_prefixed_root_as_universe_unchecked(buf: &[u8]) -> Universe {
flatbuffers::size_prefixed_root_unchecked::<Universe>(buf)
unsafe { flatbuffers::size_prefixed_root_unchecked::<Universe>(buf) }
}
#[inline]
pub fn finish_universe_buffer<'a, 'b, A: flatbuffers::Allocator + 'a>(
Expand Down
4 changes: 2 additions & 2 deletions samples/rust_generated/my_game/sample/color_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> flatbuffers::Follow<'a> for Color {
type Inner = Self;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i8>(buf, loc);
let b = unsafe { flatbuffers::read_scalar_at::<i8>(buf, loc) };
Self(b)
}
}
Expand All @@ -69,7 +69,7 @@ impl flatbuffers::Push for Color {
type Output = Color;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<i8>(dst, self.0);
unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> flatbuffers::Follow<'a> for Equipment {
type Inner = Self;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
let b = unsafe { flatbuffers::read_scalar_at::<u8>(buf, loc) };
Self(b)
}
}
Expand All @@ -65,7 +65,7 @@ impl flatbuffers::Push for Equipment {
type Output = Equipment;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<u8>(dst, self.0);
unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
}
}

Expand Down
6 changes: 3 additions & 3 deletions samples/rust_generated/my_game/sample/monster_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> flatbuffers::Follow<'a> for Monster<'a> {
type Inner = Monster<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { _tab: flatbuffers::Table::new(buf, loc) }
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
}
}

Expand Down Expand Up @@ -451,14 +451,14 @@ pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>(
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid `Monster`.
pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster {
flatbuffers::root_unchecked::<Monster>(buf)
unsafe { flatbuffers::root_unchecked::<Monster>(buf) }
}
#[inline]
/// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it.
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`.
pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster {
flatbuffers::size_prefixed_root_unchecked::<Monster>(buf)
unsafe { flatbuffers::size_prefixed_root_unchecked::<Monster>(buf) }
}
#[inline]
pub fn finish_monster_buffer<'a, 'b, A: flatbuffers::Allocator + 'a>(
Expand Down
6 changes: 3 additions & 3 deletions samples/rust_generated/my_game/sample/vec_3_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ impl<'a> flatbuffers::Follow<'a> for Vec3 {
type Inner = &'a Vec3;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
<&'a Vec3>::follow(buf, loc)
unsafe { <&'a Vec3>::follow(buf, loc) }
}
}
impl<'a> flatbuffers::Follow<'a> for &'a Vec3 {
type Inner = &'a Vec3;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::follow_cast_ref::<Vec3>(buf, loc)
unsafe { flatbuffers::follow_cast_ref::<Vec3>(buf, loc) }
}
}
impl<'b> flatbuffers::Push for Vec3 {
type Output = Vec3;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
let src = ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, <Self as flatbuffers::Push>::size());
let src = unsafe { ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, <Self as flatbuffers::Push>::size()) };
dst.copy_from_slice(src);
}
#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> flatbuffers::Follow<'a> for Weapon<'a> {
type Inner = Weapon<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { _tab: flatbuffers::Table::new(buf, loc) }
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ class RustGenerator : public BaseGenerator {
code_ += " #[inline]";
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
code_ +=
" let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc);";
" let b = unsafe { flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc) };";
if (IsBitFlagsEnum(enum_def)) {
code_ += " Self::from_bits_retain(b)";
} else {
Expand All @@ -855,8 +855,8 @@ class RustGenerator : public BaseGenerator {
code_ += " #[inline]";
code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {";
code_ +=
" flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, "
"{{INTO_BASE}});";
" unsafe { flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, "
"{{INTO_BASE}}); }";
code_ += " }";
code_ += "}";
code_ += "";
Expand Down Expand Up @@ -1658,7 +1658,7 @@ class RustGenerator : public BaseGenerator {
code_ += " type Inner = {{STRUCT_TY}}<'a>;";
code_ += " #[inline]";
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
code_ += " Self { _tab: flatbuffers::Table::new(buf, loc) }";
code_ += " Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }";
code_ += " }";
code_ += "}";
code_ += "";
Expand Down Expand Up @@ -2481,7 +2481,7 @@ class RustGenerator : public BaseGenerator {
code_ +=
"pub unsafe fn root_as_{{STRUCT_FN}}_unchecked"
"(buf: &[u8]) -> {{STRUCT_TY}} {";
code_ += " flatbuffers::root_unchecked::<{{STRUCT_TY}}>(buf)";
code_ += " unsafe { flatbuffers::root_unchecked::<{{STRUCT_TY}}>(buf) }";
code_ += "}";
code_ += "#[inline]";
code_ +=
Expand All @@ -2495,8 +2495,8 @@ class RustGenerator : public BaseGenerator {
"pub unsafe fn size_prefixed_root_as_{{STRUCT_FN}}"
"_unchecked(buf: &[u8]) -> {{STRUCT_TY}} {";
code_ +=
" flatbuffers::size_prefixed_root_unchecked::<{{STRUCT_TY}}>"
"(buf)";
" unsafe { flatbuffers::size_prefixed_root_unchecked::<{{STRUCT_TY}}>"
"(buf) }";
code_ += "}";

if (parser_.file_identifier_.length()) {
Expand Down Expand Up @@ -2656,23 +2656,23 @@ class RustGenerator : public BaseGenerator {
code_ += " type Inner = &'a {{STRUCT_TY}};";
code_ += " #[inline]";
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
code_ += " <&'a {{STRUCT_TY}}>::follow(buf, loc)";
code_ += " unsafe { <&'a {{STRUCT_TY}}>::follow(buf, loc) }";
code_ += " }";
code_ += "}";
code_ += "impl<'a> flatbuffers::Follow<'a> for &'a {{STRUCT_TY}} {";
code_ += " type Inner = &'a {{STRUCT_TY}};";
code_ += " #[inline]";
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
code_ += " flatbuffers::follow_cast_ref::<{{STRUCT_TY}}>(buf, loc)";
code_ += " unsafe { flatbuffers::follow_cast_ref::<{{STRUCT_TY}}>(buf, loc) }";
code_ += " }";
code_ += "}";
code_ += "impl<'b> flatbuffers::Push for {{STRUCT_TY}} {";
code_ += " type Output = {{STRUCT_TY}};";
code_ += " #[inline]";
code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {";
code_ +=
" let src = ::core::slice::from_raw_parts(self as *const "
"{{STRUCT_TY}} as *const u8, <Self as flatbuffers::Push>::size());";
" let src = unsafe { ::core::slice::from_raw_parts(self as *const "
"{{STRUCT_TY}} as *const u8, <Self as flatbuffers::Push>::size()) };";
code_ += " dst.copy_from_slice(src);";
code_ += " }";
code_ += " #[inline]";
Expand Down
6 changes: 3 additions & 3 deletions tests/arrays_test/my_game/example/array_struct_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ impl<'a> flatbuffers::Follow<'a> for ArrayStruct {
type Inner = &'a ArrayStruct;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
<&'a ArrayStruct>::follow(buf, loc)
unsafe { <&'a ArrayStruct>::follow(buf, loc) }
}
}
impl<'a> flatbuffers::Follow<'a> for &'a ArrayStruct {
type Inner = &'a ArrayStruct;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::follow_cast_ref::<ArrayStruct>(buf, loc)
unsafe { flatbuffers::follow_cast_ref::<ArrayStruct>(buf, loc) }
}
}
impl<'b> flatbuffers::Push for ArrayStruct {
type Output = ArrayStruct;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
let src = ::core::slice::from_raw_parts(self as *const ArrayStruct as *const u8, <Self as flatbuffers::Push>::size());
let src = unsafe { ::core::slice::from_raw_parts(self as *const ArrayStruct as *const u8, <Self as flatbuffers::Push>::size()) };
dst.copy_from_slice(src);
}
#[inline]
Expand Down
6 changes: 3 additions & 3 deletions tests/arrays_test/my_game/example/array_table_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> flatbuffers::Follow<'a> for ArrayTable<'a> {
type Inner = ArrayTable<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { _tab: flatbuffers::Table::new(buf, loc) }
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
}
}

Expand Down Expand Up @@ -193,14 +193,14 @@ pub fn size_prefixed_root_as_array_table_with_opts<'b, 'o>(
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid `ArrayTable`.
pub unsafe fn root_as_array_table_unchecked(buf: &[u8]) -> ArrayTable {
flatbuffers::root_unchecked::<ArrayTable>(buf)
unsafe { flatbuffers::root_unchecked::<ArrayTable>(buf) }
}
#[inline]
/// Assumes, without verification, that a buffer of bytes contains a size prefixed ArrayTable and returns it.
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid size prefixed `ArrayTable`.
pub unsafe fn size_prefixed_root_as_array_table_unchecked(buf: &[u8]) -> ArrayTable {
flatbuffers::size_prefixed_root_unchecked::<ArrayTable>(buf)
unsafe { flatbuffers::size_prefixed_root_unchecked::<ArrayTable>(buf) }
}
pub const ARRAY_TABLE_IDENTIFIER: &str = "ARRT";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ impl<'a> flatbuffers::Follow<'a> for NestedStruct {
type Inner = &'a NestedStruct;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
<&'a NestedStruct>::follow(buf, loc)
unsafe { <&'a NestedStruct>::follow(buf, loc) }
}
}
impl<'a> flatbuffers::Follow<'a> for &'a NestedStruct {
type Inner = &'a NestedStruct;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::follow_cast_ref::<NestedStruct>(buf, loc)
unsafe { flatbuffers::follow_cast_ref::<NestedStruct>(buf, loc) }
}
}
impl<'b> flatbuffers::Push for NestedStruct {
type Output = NestedStruct;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
let src = ::core::slice::from_raw_parts(self as *const NestedStruct as *const u8, <Self as flatbuffers::Push>::size());
let src = unsafe { ::core::slice::from_raw_parts(self as *const NestedStruct as *const u8, <Self as flatbuffers::Push>::size()) };
dst.copy_from_slice(src);
}
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions tests/arrays_test/my_game/example/test_enum_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> flatbuffers::Follow<'a> for TestEnum {
type Inner = Self;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i8>(buf, loc);
let b = unsafe { flatbuffers::read_scalar_at::<i8>(buf, loc) };
Self(b)
}
}
Expand All @@ -69,7 +69,7 @@ impl flatbuffers::Push for TestEnum {
type Output = TestEnum;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<i8>(dst, self.0);
unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a> flatbuffers::Follow<'a> for FromInclude {
type Inner = Self;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i64>(buf, loc);
let b = unsafe { flatbuffers::read_scalar_at::<i64>(buf, loc) };
Self(b)
}
}
Expand All @@ -61,7 +61,7 @@ impl flatbuffers::Push for FromInclude {
type Output = FromInclude;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<i64>(dst, self.0);
unsafe { flatbuffers::emplace_scalar::<i64>(dst, self.0); }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> flatbuffers::Follow<'a> for TableB<'a> {
type Inner = TableB<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { _tab: flatbuffers::Table::new(buf, loc) }
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ impl<'a> flatbuffers::Follow<'a> for Unused {
type Inner = &'a Unused;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
<&'a Unused>::follow(buf, loc)
unsafe { <&'a Unused>::follow(buf, loc) }
}
}
impl<'a> flatbuffers::Follow<'a> for &'a Unused {
type Inner = &'a Unused;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::follow_cast_ref::<Unused>(buf, loc)
unsafe { flatbuffers::follow_cast_ref::<Unused>(buf, loc) }
}
}
impl<'b> flatbuffers::Push for Unused {
type Output = Unused;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
let src = ::core::slice::from_raw_parts(self as *const Unused as *const u8, <Self as flatbuffers::Push>::size());
let src = unsafe { ::core::slice::from_raw_parts(self as *const Unused as *const u8, <Self as flatbuffers::Push>::size()) };
dst.copy_from_slice(src);
}
#[inline]
Expand Down
Loading
Loading