mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-08 22:20:56 +00:00
[rust] Add use declarations to Rust-generated bindings for imported FB definitions (#5645)
* Bugfix for Rust generation of union fields named with language keywords
Looking at ParseField, it appears that in the case of unions, an extra field with a `UnionTypeFieldSuffix` is added to the type definition, however, if the name of this field is a keyword in the target language, it isn't escaped.
For example, if generating code for rust for a union field named `type`, flatc will generate a (non-keyword escaped) field named `type_type` for this hidden union field, and one (keyword escaped) called `type_` for the actual union contents.
When the union accessors are generated, they refer to this `type_type` field, but they will escape it mistakenly, generating code like this:
```
#[inline]
#[allow(non_snake_case)]
pub fn type__as_int(&self) -> Option<Int<'a>> {
if self.type__type() == Type::Int {
self.type_().map(|u| Int::init_from_table(u))
} else {
None
}
}
```
Which will fail to build because the field is called `self.type_type()`, not `self.type__type()`.
* [Rust] Add crate-relative use statements for FBS includes.
At present if a flatbuffer description includes a reference to a type in
another file, the generated Rust code needs to be hand-modified to add
the appropriate `use` statements.
This assumes that the dependencies are built into the same crate, which
I think is a reasonable assumption?
* Revert "[Rust] Add crate-relative use statements for FBS includes."
This reverts commit d554d79fec.
* Add updated generated test files.
* Fixing Rust test harness to handle new includes.
Test binaries need to add references to generated code that's
transitively included.
This also has the knock-on in that this code (which is referenced by
include directives directly in the flatbuffer schema files) also needs
to be generated, hence the changes to generate_code.sh.
* Test harnesses expect test data to be checked in.
Put include_test2 files into the same directory as the include_test2
schema definition.
Update all code generation scripts (forgot the batch file from last
time).
Path updates in Rust test.
* Include updated generated code
* Address comments raised in PR
* Fix failing Rust tests.
* Previous merge clobbered this branch change.
* Add updated imports to benchmarks.
* Clarifying comment per PR request
* Update documentation comments per feedback
* Remove non-Rust generated files for include tests, per feedback from @rw/@aardappel
* Broken code generation batch file
* Fix typo
* Add TODO for tidying up use declaration traversal sometime in the future
* Update test files.
This commit is contained in:
committed by
Robert Winslow
parent
bee1df96dc
commit
6da1cf79d9
87
tests/include_test/include_test1_generated.rs
Normal file
87
tests/include_test/include_test1_generated.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
|
||||
use crate::include_test2_generated::*;
|
||||
use std::mem;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
extern crate flatbuffers;
|
||||
use self::flatbuffers::EndianScalar;
|
||||
|
||||
pub enum TableAOffset {}
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
|
||||
pub struct TableA<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for TableA<'a> {
|
||||
type Inner = TableA<'a>;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self {
|
||||
_tab: flatbuffers::Table { buf: buf, loc: loc },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TableA<'a> {
|
||||
#[inline]
|
||||
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
TableA {
|
||||
_tab: table,
|
||||
}
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args TableAArgs<'args>) -> flatbuffers::WIPOffset<TableA<'bldr>> {
|
||||
let mut builder = TableABuilder::new(_fbb);
|
||||
if let Some(x) = args.b { builder.add_b(x); }
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
pub const VT_B: flatbuffers::VOffsetT = 4;
|
||||
|
||||
#[inline]
|
||||
pub fn b(&self) -> Option<my_game::other_name_space::TableB<'a>> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<my_game::other_name_space::TableB<'a>>>(TableA::VT_B, None)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TableAArgs<'a> {
|
||||
pub b: Option<flatbuffers::WIPOffset<my_game::other_name_space::TableB<'a >>>,
|
||||
}
|
||||
impl<'a> Default for TableAArgs<'a> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
TableAArgs {
|
||||
b: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct TableABuilder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> TableABuilder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_b(&mut self, b: flatbuffers::WIPOffset<my_game::other_name_space::TableB<'b >>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<my_game::other_name_space::TableB>>(TableA::VT_B, b);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableABuilder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
TableABuilder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<TableA<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user