Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port FlatBuffers to Rust #4898

Merged
merged 37 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
93f9162
Port FlatBuffers to Rust: generator/runtime/tests.
rw Aug 29, 2018
66c6440
add idl_gen_rust.cpp to BUILD
rw Aug 31, 2018
d608827
remove the duplicate function `fill` and add some inline attributes
rw Aug 31, 2018
596017e
add comment in docs that Rust might add a verifier in the future
rw Aug 31, 2018
38b264c
generate tests/monsterdata_rust_wire.mon during rust tests (and gitig…
rw Aug 31, 2018
80725be
add rust to flatbuffers.md docs
rw Aug 31, 2018
129dd2d
docs
rw Aug 31, 2018
a865717
simplify nested asserts
rw Aug 31, 2018
2ed43e0
refactor vtable writing
rw Aug 31, 2018
9499632
remove duplicate assert
rw Aug 31, 2018
5138f85
comment
rw Aug 31, 2018
71a5ea4
use size_of for constants
rw Aug 31, 2018
9ea510c
comment
rw Aug 31, 2018
9736b9f
use auto instead of std::string in many places in the rust generator
rw Aug 31, 2018
3e74948
update generate_code.bat with rust
rw Aug 31, 2018
52d43ca
comments on reserved keywords; indentation of union accessors
rw Aug 31, 2018
b5b4631
delete test output
rw Aug 31, 2018
b5e5096
comment to explain why we do not inline a struct creation
rw Aug 31, 2018
77fb225
refactor some tests with macros
rw Aug 31, 2018
47ebb61
add support column for rust
rw Aug 31, 2018
ee51cc0
ergonomics/lifetimes tweaks in builder
rw Sep 1, 2018
a78edd0
no more need for ZeroTerminatedByteSlice
rw Sep 1, 2018
67004f3
wip for making Push::size a static method
rw Sep 1, 2018
894fe3e
simplifying push some more
rw Sep 1, 2018
fb28cad
delete old test
rw Sep 1, 2018
bd33f75
inlining, push
rw Sep 1, 2018
a3ef1de
Push::size is static
rw Sep 1, 2018
87cef86
start_vector requires push
rw Sep 2, 2018
bd2a5ed
more push/vector typing
rw Sep 2, 2018
36de894
no more phantomdata
rw Sep 2, 2018
3c03f30
good sample and docs
rw Sep 2, 2018
0de4628
add a todo to rust lib
rw Sep 2, 2018
7972b79
regenerate rust test code
rw Sep 2, 2018
f070f53
more alignment/size tests
rw Sep 2, 2018
8081c5a
remove dead code
rw Sep 2, 2018
acf48d0
doc tweak
rw Sep 2, 2018
4d005c1
revert old typescript file to master
rw Sep 2, 2018
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
Prev Previous commit
Next Next commit
inlining, push
  • Loading branch information
rw committed Sep 1, 2018
commit bd33f7548d4b2e379dd5beae66790456f05ecd28
13 changes: 6 additions & 7 deletions rust/flatbuffers/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ impl<'fbb> FlatBufferBuilder<'fbb> {
/// This function uses traits to provide a unified API for writing
/// scalars, tables, vectors, and WIPOffsets.
#[inline]
pub fn push<X: Push>(&mut self, x: X) -> WIPOffset<X::Output> {
self.align(x.size(), x.alignment());
self.make_space(x.size());
pub fn push<P: Push>(&mut self, x: P) -> WIPOffset<P::Output> {
let sz = x.size();
self.align(sz, sz);
self.make_space(sz);
{
let (dst, rest) = (&mut self.owned_buf[self.head..]).split_at_mut(x.size());
let (dst, rest) = (&mut self.owned_buf[self.head..]).split_at_mut(sz);
x.push(dst, rest);
}
WIPOffset::new(self.used_space() as UOffsetT)
Expand Down Expand Up @@ -251,14 +252,12 @@ impl<'fbb> FlatBufferBuilder<'fbb> {
self.assert_not_nested("create_vector_direct can not be called when a table or vector is under construction");
let elem_size = size_of::<T>();
self.align(SIZE_UOFFSET + items.len() * elem_size, max(elem_size, SIZE_UOFFSET));
self.make_space(SIZE_UOFFSET + items.len() * elem_size);

let bytes = unsafe {
let ptr = items.as_ptr() as *const T as *const u8;
from_raw_parts(ptr, items.len() * elem_size)
};

self.owned_buf[self.head..self.head+bytes.len()].copy_from_slice(bytes);
self.push_bytes_unprefixed(bytes);
self.push(items.len() as UOffsetT);

WIPOffset::new(self.used_space() as UOffsetT)
Expand Down
3 changes: 3 additions & 0 deletions src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,7 @@ class RustGenerator : public BaseGenerator {

// Generate an impl of Default for the *Args type:
code_ += "impl<'a> Default for {{STRUCT_NAME}}Args<'a> {";
code_ += " #[inline]";
code_ += " fn default() -> Self {";
code_ += " {{STRUCT_NAME}}Args {";
for (auto it = struct_def.fields.vec.begin();
Expand Down Expand Up @@ -1385,6 +1386,7 @@ class RustGenerator : public BaseGenerator {
}

// Struct initializer (all fields required);
code_ += " #[inline]";
code_ +=
" pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> "
"{{STRUCT_NAME}}Builder<'a, 'b> {";
Expand All @@ -1397,6 +1399,7 @@ class RustGenerator : public BaseGenerator {
code_ += " }";

// finish() function.
code_ += " #[inline]";
code_ += " pub fn finish(self) -> "
"flatbuffers::WIPOffset<{{STRUCT_NAME}}<'a>> {";
code_ += " let o = self.fbb_.end_table(self.start_);";
Expand Down
21 changes: 21 additions & 0 deletions tests/monster_test_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct InParentNamespaceArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for InParentNamespaceArgs<'a> {
#[inline]
fn default() -> Self {
InParentNamespaceArgs {
_phantom: PhantomData,
Expand All @@ -64,13 +65,15 @@ pub struct InParentNamespaceBuilder<'a: 'b, 'b> {
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
}
impl<'a: 'b, 'b> InParentNamespaceBuilder<'a, 'b> {
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> InParentNamespaceBuilder<'a, 'b> {
let start = _fbb.start_table();
InParentNamespaceBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<InParentNamespace<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
Expand Down Expand Up @@ -129,6 +132,7 @@ pub struct MonsterArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for MonsterArgs<'a> {
#[inline]
fn default() -> Self {
MonsterArgs {
_phantom: PhantomData,
Expand All @@ -140,13 +144,15 @@ pub struct MonsterBuilder<'a: 'b, 'b> {
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
}
impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> {
let start = _fbb.start_table();
MonsterBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<Monster<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
Expand Down Expand Up @@ -585,6 +591,7 @@ pub struct TestSimpleTableWithEnumArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for TestSimpleTableWithEnumArgs<'a> {
#[inline]
fn default() -> Self {
TestSimpleTableWithEnumArgs {
color: Color::Green,
Expand All @@ -601,13 +608,15 @@ impl<'a: 'b, 'b> TestSimpleTableWithEnumBuilder<'a, 'b> {
pub fn add_color(&mut self, color: Color) {
self.fbb_.push_slot::<Color>(TestSimpleTableWithEnum::VT_COLOR, color, Color::Green);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TestSimpleTableWithEnumBuilder<'a, 'b> {
let start = _fbb.start_table();
TestSimpleTableWithEnumBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<TestSimpleTableWithEnum<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
Expand Down Expand Up @@ -677,6 +686,7 @@ pub struct StatArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for StatArgs<'a> {
#[inline]
fn default() -> Self {
StatArgs {
id: None,
Expand All @@ -703,13 +713,15 @@ impl<'a: 'b, 'b> StatBuilder<'a, 'b> {
pub fn add_count(&mut self, count: u16) {
self.fbb_.push_slot::<u16>(Stat::VT_COUNT, count, 0);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> StatBuilder<'a, 'b> {
let start = _fbb.start_table();
StatBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<Stat<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
Expand Down Expand Up @@ -773,6 +785,7 @@ pub struct ReferrableArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for ReferrableArgs<'a> {
#[inline]
fn default() -> Self {
ReferrableArgs {
id: 0,
Expand All @@ -789,13 +802,15 @@ impl<'a: 'b, 'b> ReferrableBuilder<'a, 'b> {
pub fn add_id(&mut self, id: u64) {
self.fbb_.push_slot::<u64>(Referrable::VT_ID, id, 0);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ReferrableBuilder<'a, 'b> {
let start = _fbb.start_table();
ReferrableBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<Referrable<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
Expand Down Expand Up @@ -1188,6 +1203,7 @@ pub struct MonsterArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for MonsterArgs<'a> {
#[inline]
fn default() -> Self {
MonsterArgs {
pos: None,
Expand Down Expand Up @@ -1409,13 +1425,15 @@ impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
pub fn add_vector_of_non_owning_references(&mut self, vector_of_non_owning_references: flatbuffers::WIPOffset<flatbuffers::Vector<'b , u64>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, vector_of_non_owning_references);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> {
let start = _fbb.start_table();
MonsterBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<Monster<'a>> {
let o = self.fbb_.end_table(self.start_);
self.fbb_.required(o, Monster::VT_NAME,"name");
Expand Down Expand Up @@ -1549,6 +1567,7 @@ pub struct TypeAliasesArgs<'a> {
pub _phantom: PhantomData<&'a ()>, // pub for default trait
}
impl<'a> Default for TypeAliasesArgs<'a> {
#[inline]
fn default() -> Self {
TypeAliasesArgs {
i8_: 0,
Expand Down Expand Up @@ -1620,13 +1639,15 @@ impl<'a: 'b, 'b> TypeAliasesBuilder<'a, 'b> {
pub fn add_vf64(&mut self, vf64: flatbuffers::WIPOffset<flatbuffers::Vector<'b , f64>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(TypeAliases::VT_VF64, vf64);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TypeAliasesBuilder<'a, 'b> {
let start = _fbb.start_table();
TypeAliasesBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<TypeAliases<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
Expand Down