pub struct FlatBufferBuilder<'fbb> { /* private fields */ }Expand description
FlatBufferBuilder builds a FlatBuffer through manipulating its internal
state. It has an owned Vec<u8> that grows as needed (up to the hardcoded
limit of 2GiB, which is set by the FlatBuffers format).
Implementations§
Source§impl<'fbb> FlatBufferBuilder<'fbb>
impl<'fbb> FlatBufferBuilder<'fbb>
Sourcepub fn new() -> FlatBufferBuilder<'fbb>
pub fn new() -> FlatBufferBuilder<'fbb>
Create a FlatBufferBuilder that is ready for writing.
pub fn new_with_capacity(size: usize) -> FlatBufferBuilder<'fbb>
with_capacitySourcepub fn with_capacity(size: usize) -> FlatBufferBuilder<'fbb>
pub fn with_capacity(size: usize) -> FlatBufferBuilder<'fbb>
Create a FlatBufferBuilder that is ready for writing, with a ready-to-use capacity of the provided size.
The maximum valid value is FLATBUFFERS_MAX_BUFFER_SIZE.
Sourcepub fn from_vec(buffer: Vec<u8>) -> FlatBufferBuilder<'fbb>
pub fn from_vec(buffer: Vec<u8>) -> FlatBufferBuilder<'fbb>
Create a FlatBufferBuilder that is ready for writing, reusing an existing vector.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the FlatBufferBuilder internal state. Use this method after a
call to a finish function in order to re-use a FlatBufferBuilder.
This function is the only way to reset the finished state and start
again.
If you are using a FlatBufferBuilder repeatedly, make sure to use this
function, because it re-uses the FlatBufferBuilder’s existing
heap-allocated Vec<u8> internal buffer. This offers significant speed
improvements as compared to creating a new FlatBufferBuilder for every
new object.
Sourcepub fn collapse(self) -> (Vec<u8>, usize)
pub fn collapse(self) -> (Vec<u8>, usize)
Destroy the FlatBufferBuilder, returning its internal byte vector and the index into it that represents the start of valid data.
Sourcepub fn push<P>(&mut self, x: P) -> WIPOffset<<P as Push>::Output>where
P: Push,
pub fn push<P>(&mut self, x: P) -> WIPOffset<<P as Push>::Output>where
P: Push,
Push a Push’able value onto the front of the in-progress data.
This function uses traits to provide a unified API for writing scalars, tables, vectors, and WIPOffsets.
Sourcepub fn push_slot<X>(&mut self, slotoff: u16, x: X, default: X)
pub fn push_slot<X>(&mut self, slotoff: u16, x: X, default: X)
Push a Push’able value onto the front of the in-progress data, and store a reference to it in the in-progress vtable. If the value matches the default, then this is a no-op.
Sourcepub fn push_slot_always<X>(&mut self, slotoff: u16, x: X)where
X: Push,
pub fn push_slot_always<X>(&mut self, slotoff: u16, x: X)where
X: Push,
Push a Push’able value onto the front of the in-progress data, and store a reference to it in the in-progress vtable.
Sourcepub fn num_written_vtables(&self) -> usize
pub fn num_written_vtables(&self) -> usize
Retrieve the number of vtables that have been serialized into the FlatBuffer. This is primarily used to check vtable deduplication.
Sourcepub fn start_table(&mut self) -> WIPOffset<TableUnfinishedWIPOffset>
pub fn start_table(&mut self) -> WIPOffset<TableUnfinishedWIPOffset>
Start a Table write.
Asserts that the builder is not in a nested state.
Users probably want to use push_slot to add values after calling this.
Sourcepub fn end_table(
&mut self,
off: WIPOffset<TableUnfinishedWIPOffset>,
) -> WIPOffset<TableFinishedWIPOffset>
pub fn end_table( &mut self, off: WIPOffset<TableUnfinishedWIPOffset>, ) -> WIPOffset<TableFinishedWIPOffset>
End a Table write.
Asserts that the builder is in a nested state.
Sourcepub fn start_vector<T>(&mut self, num_items: usize)where
T: Push,
pub fn start_vector<T>(&mut self, num_items: usize)where
T: Push,
Start a Vector write.
Asserts that the builder is not in a nested state.
Most users will prefer to call create_vector.
Speed optimizing users who choose to create vectors manually using this
function will want to use push to add values.
Sourcepub fn end_vector<T>(&mut self, num_elems: usize) -> WIPOffset<Vector<'fbb, T>>where
T: Push,
pub fn end_vector<T>(&mut self, num_elems: usize) -> WIPOffset<Vector<'fbb, T>>where
T: Push,
End a Vector write.
Note that the num_elems parameter is the number of written items, not
the byte count.
Asserts that the builder is in a nested state.
Sourcepub fn create_string<'a, 'b>(&'a mut self, s: &'b str) -> WIPOffset<&'fbb str>where
'a: 'b,
pub fn create_string<'a, 'b>(&'a mut self, s: &'b str) -> WIPOffset<&'fbb str>where
'a: 'b,
Create a utf8 string.
The wire format represents this as a zero-terminated byte vector.
Sourcepub fn create_byte_string(&mut self, data: &[u8]) -> WIPOffset<&'fbb [u8]>
pub fn create_byte_string(&mut self, data: &[u8]) -> WIPOffset<&'fbb [u8]>
Create a zero-terminated byte vector.
Sourcepub fn create_vector<'a, 'b, T>(
&'a mut self,
items: &'b [T],
) -> WIPOffset<Vector<'fbb, <T as Push>::Output>>where
'a: 'b,
T: Push + 'b,
pub fn create_vector<'a, 'b, T>(
&'a mut self,
items: &'b [T],
) -> WIPOffset<Vector<'fbb, <T as Push>::Output>>where
'a: 'b,
T: Push + 'b,
Create a vector of Push-able objects.
Speed-sensitive users may wish to reduce memory usage by creating the
vector manually: use start_vector, push, and end_vector.
Sourcepub fn create_vector_from_iter<T>(
&mut self,
items: impl ExactSizeIterator<Item = T> + DoubleEndedIterator,
) -> WIPOffset<Vector<'fbb, <T as Push>::Output>>where
T: Push,
pub fn create_vector_from_iter<T>(
&mut self,
items: impl ExactSizeIterator<Item = T> + DoubleEndedIterator,
) -> WIPOffset<Vector<'fbb, <T as Push>::Output>>where
T: Push,
Create a vector of Push-able objects.
Speed-sensitive users may wish to reduce memory usage by creating the
vector manually: use start_vector, push, and end_vector.
Sourcepub fn force_defaults(&mut self, force_defaults: bool)
pub fn force_defaults(&mut self, force_defaults: bool)
Set whether default values are stored.
In order to save space, fields that are set to their default value
aren’t stored in the buffer. Setting force_defaults to true
disables this optimization.
By default, force_defaults is false.
Sourcepub fn unfinished_data(&self) -> &[u8] ⓘ
pub fn unfinished_data(&self) -> &[u8] ⓘ
Get the byte slice for the data that has been written, regardless of whether it has been finished.
Sourcepub fn finished_data(&self) -> &[u8] ⓘ
pub fn finished_data(&self) -> &[u8] ⓘ
Get the byte slice for the data that has been written after a call to
one of the finish functions.
§Panics
Panics if the buffer is not finished.
Sourcepub fn mut_finished_buffer(&mut self) -> (&mut [u8], usize)
pub fn mut_finished_buffer(&mut self) -> (&mut [u8], usize)
Returns a mutable view of a finished buffer and location of where the flatbuffer starts. Note that modifying the flatbuffer data may corrupt it.
§Panics
Panics if the flatbuffer is not finished.
Sourcepub fn required(
&self,
tab_revloc: WIPOffset<TableFinishedWIPOffset>,
slot_byte_loc: u16,
assert_msg_name: &'static str,
)
pub fn required( &self, tab_revloc: WIPOffset<TableFinishedWIPOffset>, slot_byte_loc: u16, assert_msg_name: &'static str, )
Assert that a field is present in the just-finished Table.
This is somewhat low-level and is mostly used by the generated code.
Sourcepub fn finish_size_prefixed<T>(
&mut self,
root: WIPOffset<T>,
file_identifier: Option<&str>,
)
pub fn finish_size_prefixed<T>( &mut self, root: WIPOffset<T>, file_identifier: Option<&str>, )
Finalize the FlatBuffer by: aligning it, pushing an optional file
identifier on to it, pushing a size prefix on to it, and marking the
internal state of the FlatBufferBuilder as finished. Afterwards,
users can call finished_data to get the resulting data.
Sourcepub fn finish<T>(&mut self, root: WIPOffset<T>, file_identifier: Option<&str>)
pub fn finish<T>(&mut self, root: WIPOffset<T>, file_identifier: Option<&str>)
Finalize the FlatBuffer by: aligning it, pushing an optional file
identifier on to it, and marking the internal state of the
FlatBufferBuilder as finished. Afterwards, users can call
finished_data to get the resulting data.
Sourcepub fn finish_minimal<T>(&mut self, root: WIPOffset<T>)
pub fn finish_minimal<T>(&mut self, root: WIPOffset<T>)
Finalize the FlatBuffer by: aligning it and marking the internal state
of the FlatBufferBuilder as finished. Afterwards, users can call
finished_data to get the resulting data.
Trait Implementations§
Source§impl<'fbb> Clone for FlatBufferBuilder<'fbb>
impl<'fbb> Clone for FlatBufferBuilder<'fbb>
Source§fn clone(&self) -> FlatBufferBuilder<'fbb>
fn clone(&self) -> FlatBufferBuilder<'fbb>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more