Skip to content

Commit 860d645

Browse files
authored
Support Rust edition 2024 (#8638)
* Developers intro how to contribute * Fix Rust code generation for Rust edition 2024 The errors look like: ``` warning[E0133]: call to unsafe function `fbs::flatbuffers::emplace_scalar` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `fbs::flatbuffers::follow_cast_ref` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `fbs::flatbuffers::Follow::follow` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `fbs::flatbuffers::read_scalar_at` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `fbs::flatbuffers::root_unchecked` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `fbs::flatbuffers::size_prefixed_root_unchecked` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `fbs::flatbuffers::Table::<'a>::new` is unsafe and requires unsafe block warning[E0133]: call to unsafe function `std::slice::from_raw_parts` is unsafe and requires unsafe block ``` * Update goldens Ran `goldens/generate_goldens.py` * Regenerate code files Ran `scripts/generate_code.py`
1 parent 06a53df commit 860d645

File tree

95 files changed

+211
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+211
-188
lines changed

CONTRIBUTING.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,26 @@ Some tips for good pull requests:
4040
# The small print
4141
Contributions made by corporations are covered by a different agreement than
4242
the one above, the Software Grant and Corporate Contributor License Agreement.
43+
44+
# Code
45+
46+
TL/DR
47+
48+
See [how to build flatc](https://flatbuffers.dev/building/).
49+
50+
When making changes, build `flatc` and then re-generate the goldens files to see the effect of your changes:
51+
52+
```
53+
$ cp build/flatc .
54+
$ goldens/generate_goldens.py
55+
```
56+
57+
Re-generate other code files to see the effects of the changes:
58+
59+
```
60+
$ scripts/generate_code.py
61+
```
62+
63+
Run tests with [TestAll.sh](tests/TestAll.sh) in [tests](tests), or directly any of the sub-scripts run by it.
64+
65+
[Format the code](Formatters.md) before submitting a PR.

goldens/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
This directory is a repository for the generated files of `flatc`.
44

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

99
These files are **NOT** intended to be depended on by any code, such as tests or
1010
or compiled examples.

goldens/rust/basic_generated.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a> flatbuffers::Follow<'a> for Galaxy<'a> {
3737
type Inner = Galaxy<'a>;
3838
#[inline]
3939
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
40-
Self { _tab: flatbuffers::Table::new(buf, loc) }
40+
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
4141
}
4242
}
4343

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

@@ -288,14 +288,14 @@ pub fn size_prefixed_root_as_universe_with_opts<'b, 'o>(
288288
/// # Safety
289289
/// Callers must trust the given bytes do indeed contain a valid `Universe`.
290290
pub unsafe fn root_as_universe_unchecked(buf: &[u8]) -> Universe {
291-
flatbuffers::root_unchecked::<Universe>(buf)
291+
unsafe { flatbuffers::root_unchecked::<Universe>(buf) }
292292
}
293293
#[inline]
294294
/// Assumes, without verification, that a buffer of bytes contains a size prefixed Universe and returns it.
295295
/// # Safety
296296
/// Callers must trust the given bytes do indeed contain a valid size prefixed `Universe`.
297297
pub unsafe fn size_prefixed_root_as_universe_unchecked(buf: &[u8]) -> Universe {
298-
flatbuffers::size_prefixed_root_unchecked::<Universe>(buf)
298+
unsafe { flatbuffers::size_prefixed_root_unchecked::<Universe>(buf) }
299299
}
300300
#[inline]
301301
pub fn finish_universe_buffer<'a, 'b, A: flatbuffers::Allocator + 'a>(

samples/rust_generated/my_game/sample/color_generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> flatbuffers::Follow<'a> for Color {
6060
type Inner = Self;
6161
#[inline]
6262
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
63-
let b = flatbuffers::read_scalar_at::<i8>(buf, loc);
63+
let b = unsafe { flatbuffers::read_scalar_at::<i8>(buf, loc) };
6464
Self(b)
6565
}
6666
}
@@ -69,7 +69,7 @@ impl flatbuffers::Push for Color {
6969
type Output = Color;
7070
#[inline]
7171
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
72-
flatbuffers::emplace_scalar::<i8>(dst, self.0);
72+
unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
7373
}
7474
}
7575

samples/rust_generated/my_game/sample/equipment_generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'a> flatbuffers::Follow<'a> for Equipment {
5656
type Inner = Self;
5757
#[inline]
5858
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
59-
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
59+
let b = unsafe { flatbuffers::read_scalar_at::<u8>(buf, loc) };
6060
Self(b)
6161
}
6262
}
@@ -65,7 +65,7 @@ impl flatbuffers::Push for Equipment {
6565
type Output = Equipment;
6666
#[inline]
6767
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
68-
flatbuffers::emplace_scalar::<u8>(dst, self.0);
68+
unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
6969
}
7070
}
7171

samples/rust_generated/my_game/sample/monster_generated.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a> flatbuffers::Follow<'a> for Monster<'a> {
2020
type Inner = Monster<'a>;
2121
#[inline]
2222
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
23-
Self { _tab: flatbuffers::Table::new(buf, loc) }
23+
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
2424
}
2525
}
2626

@@ -451,14 +451,14 @@ pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>(
451451
/// # Safety
452452
/// Callers must trust the given bytes do indeed contain a valid `Monster`.
453453
pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster {
454-
flatbuffers::root_unchecked::<Monster>(buf)
454+
unsafe { flatbuffers::root_unchecked::<Monster>(buf) }
455455
}
456456
#[inline]
457457
/// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it.
458458
/// # Safety
459459
/// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`.
460460
pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster {
461-
flatbuffers::size_prefixed_root_unchecked::<Monster>(buf)
461+
unsafe { flatbuffers::size_prefixed_root_unchecked::<Monster>(buf) }
462462
}
463463
#[inline]
464464
pub fn finish_monster_buffer<'a, 'b, A: flatbuffers::Allocator + 'a>(

samples/rust_generated/my_game/sample/vec_3_generated.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ impl<'a> flatbuffers::Follow<'a> for Vec3 {
3333
type Inner = &'a Vec3;
3434
#[inline]
3535
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
36-
<&'a Vec3>::follow(buf, loc)
36+
unsafe { <&'a Vec3>::follow(buf, loc) }
3737
}
3838
}
3939
impl<'a> flatbuffers::Follow<'a> for &'a Vec3 {
4040
type Inner = &'a Vec3;
4141
#[inline]
4242
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
43-
flatbuffers::follow_cast_ref::<Vec3>(buf, loc)
43+
unsafe { flatbuffers::follow_cast_ref::<Vec3>(buf, loc) }
4444
}
4545
}
4646
impl<'b> flatbuffers::Push for Vec3 {
4747
type Output = Vec3;
4848
#[inline]
4949
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
50-
let src = ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, <Self as flatbuffers::Push>::size());
50+
let src = unsafe { ::core::slice::from_raw_parts(self as *const Vec3 as *const u8, <Self as flatbuffers::Push>::size()) };
5151
dst.copy_from_slice(src);
5252
}
5353
#[inline]

samples/rust_generated/my_game/sample/weapon_generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a> flatbuffers::Follow<'a> for Weapon<'a> {
2020
type Inner = Weapon<'a>;
2121
#[inline]
2222
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
23-
Self { _tab: flatbuffers::Table::new(buf, loc) }
23+
Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }
2424
}
2525
}
2626

src/idl_gen_rust.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ class RustGenerator : public BaseGenerator {
841841
code_ += " #[inline]";
842842
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
843843
code_ +=
844-
" let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc);";
844+
" let b = unsafe { flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc) };";
845845
if (IsBitFlagsEnum(enum_def)) {
846846
code_ += " Self::from_bits_retain(b)";
847847
} else {
@@ -855,8 +855,8 @@ class RustGenerator : public BaseGenerator {
855855
code_ += " #[inline]";
856856
code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {";
857857
code_ +=
858-
" flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, "
859-
"{{INTO_BASE}});";
858+
" unsafe { flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, "
859+
"{{INTO_BASE}}); }";
860860
code_ += " }";
861861
code_ += "}";
862862
code_ += "";
@@ -1658,7 +1658,7 @@ class RustGenerator : public BaseGenerator {
16581658
code_ += " type Inner = {{STRUCT_TY}}<'a>;";
16591659
code_ += " #[inline]";
16601660
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
1661-
code_ += " Self { _tab: flatbuffers::Table::new(buf, loc) }";
1661+
code_ += " Self { _tab: unsafe { flatbuffers::Table::new(buf, loc) } }";
16621662
code_ += " }";
16631663
code_ += "}";
16641664
code_ += "";
@@ -2481,7 +2481,7 @@ class RustGenerator : public BaseGenerator {
24812481
code_ +=
24822482
"pub unsafe fn root_as_{{STRUCT_FN}}_unchecked"
24832483
"(buf: &[u8]) -> {{STRUCT_TY}} {";
2484-
code_ += " flatbuffers::root_unchecked::<{{STRUCT_TY}}>(buf)";
2484+
code_ += " unsafe { flatbuffers::root_unchecked::<{{STRUCT_TY}}>(buf) }";
24852485
code_ += "}";
24862486
code_ += "#[inline]";
24872487
code_ +=
@@ -2495,8 +2495,8 @@ class RustGenerator : public BaseGenerator {
24952495
"pub unsafe fn size_prefixed_root_as_{{STRUCT_FN}}"
24962496
"_unchecked(buf: &[u8]) -> {{STRUCT_TY}} {";
24972497
code_ +=
2498-
" flatbuffers::size_prefixed_root_unchecked::<{{STRUCT_TY}}>"
2499-
"(buf)";
2498+
" unsafe { flatbuffers::size_prefixed_root_unchecked::<{{STRUCT_TY}}>"
2499+
"(buf) }";
25002500
code_ += "}";
25012501

25022502
if (parser_.file_identifier_.length()) {
@@ -2656,23 +2656,23 @@ class RustGenerator : public BaseGenerator {
26562656
code_ += " type Inner = &'a {{STRUCT_TY}};";
26572657
code_ += " #[inline]";
26582658
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
2659-
code_ += " <&'a {{STRUCT_TY}}>::follow(buf, loc)";
2659+
code_ += " unsafe { <&'a {{STRUCT_TY}}>::follow(buf, loc) }";
26602660
code_ += " }";
26612661
code_ += "}";
26622662
code_ += "impl<'a> flatbuffers::Follow<'a> for &'a {{STRUCT_TY}} {";
26632663
code_ += " type Inner = &'a {{STRUCT_TY}};";
26642664
code_ += " #[inline]";
26652665
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
2666-
code_ += " flatbuffers::follow_cast_ref::<{{STRUCT_TY}}>(buf, loc)";
2666+
code_ += " unsafe { flatbuffers::follow_cast_ref::<{{STRUCT_TY}}>(buf, loc) }";
26672667
code_ += " }";
26682668
code_ += "}";
26692669
code_ += "impl<'b> flatbuffers::Push for {{STRUCT_TY}} {";
26702670
code_ += " type Output = {{STRUCT_TY}};";
26712671
code_ += " #[inline]";
26722672
code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {";
26732673
code_ +=
2674-
" let src = ::core::slice::from_raw_parts(self as *const "
2675-
"{{STRUCT_TY}} as *const u8, <Self as flatbuffers::Push>::size());";
2674+
" let src = unsafe { ::core::slice::from_raw_parts(self as *const "
2675+
"{{STRUCT_TY}} as *const u8, <Self as flatbuffers::Push>::size()) };";
26762676
code_ += " dst.copy_from_slice(src);";
26772677
code_ += " }";
26782678
code_ += " #[inline]";

tests/arrays_test/my_game/example/array_struct_generated.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ impl<'a> flatbuffers::Follow<'a> for ArrayStruct {
3636
type Inner = &'a ArrayStruct;
3737
#[inline]
3838
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
39-
<&'a ArrayStruct>::follow(buf, loc)
39+
unsafe { <&'a ArrayStruct>::follow(buf, loc) }
4040
}
4141
}
4242
impl<'a> flatbuffers::Follow<'a> for &'a ArrayStruct {
4343
type Inner = &'a ArrayStruct;
4444
#[inline]
4545
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
46-
flatbuffers::follow_cast_ref::<ArrayStruct>(buf, loc)
46+
unsafe { flatbuffers::follow_cast_ref::<ArrayStruct>(buf, loc) }
4747
}
4848
}
4949
impl<'b> flatbuffers::Push for ArrayStruct {
5050
type Output = ArrayStruct;
5151
#[inline]
5252
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
53-
let src = ::core::slice::from_raw_parts(self as *const ArrayStruct as *const u8, <Self as flatbuffers::Push>::size());
53+
let src = unsafe { ::core::slice::from_raw_parts(self as *const ArrayStruct as *const u8, <Self as flatbuffers::Push>::size()) };
5454
dst.copy_from_slice(src);
5555
}
5656
#[inline]

0 commit comments

Comments
 (0)