Struct Builder

Source
pub struct Builder { /* private fields */ }
Expand description

Builder for serializing flatbuffers.

§Examples

use planus::Builder;
use planus_example::monster_generated::my_game::sample::Weapon;
let mut builder = Builder::new();
let weapon = Weapon::create(&mut builder, "Axe", 24);
builder.finish(weapon, None);

Implementations§

Source§

impl Builder

Source

pub fn new() -> Self

Creates a new Builder.

Source

pub fn len(&self) -> usize

Gets the length of the internal buffer in bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if the internal buffer is empty.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new builder with a specific internal capacity already allocated.

Source

pub fn create_string(&mut self, v: impl WriteAsOffset<str>) -> Offset<str>

Serializes a string and returns the offset to it

Source

pub fn create_vector<T>(&mut self, v: impl WriteAsOffset<[T]>) -> Offset<[T]>

Serializes a slice and returns the offset to it

Source

pub fn create_union_vector<T>( &mut self, v: impl WriteAsUnionVector<T>, ) -> UnionVectorOffset<T>

Serializes a slice of union values and returns the offset to it

Source

pub fn clear(&mut self)

Resets the builders internal state and clears the internal buffer.

Source

pub fn finish<T>( &mut self, root: impl WriteAsOffset<T>, file_identifier: Option<[u8; 4]>, ) -> &[u8]

Finish writing the internal buffer and return a byte slice of it.

This will make sure all alignment requirements are fullfilled and that the file identifier has been written if specified.

§Examples
use planus::Builder;
use planus_example::monster_generated::my_game::sample::Weapon;
let mut builder = Builder::new();
let weapon = Weapon::create(&mut builder, "Axe", 24);
builder.finish(weapon, None);

It can also be used to directly serialize an owned flatbuffers struct

use planus::Builder;
use planus_example::monster_generated::my_game::sample::Weapon;
let mut builder = Builder::new();
let weapon = Weapon { name: Some("Sword".to_string()), damage: 12 };
let data = builder.finish(&weapon, None);
Source

pub fn as_slice(&self) -> &[u8]

Returns a reference to the current data buffer.

It will return the same slice as the one return by finish, unless additional data has been appened afterwards.

Trait Implementations§

Source§

impl Debug for Builder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Builder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.