FlatBufferBuilder
struct FlatBufferBuilderBuilds one FlatBuffer, back to front.
Not reusable: finish leaves the builder holding the completed buffer,
and the intended lifecycle is one builder per message.
Implements Movable
__init__
def __init__(out self, initial: Int = Int(1024)) offset
def offset(self) -> Int Bytes written so far, which is also the position of the next write measured from the end of the buffer.
prepend_u8
def prepend_u8(mut self, v: UInt8) prepend_bool
def prepend_bool(mut self, v: Bool) prepend_i16
def prepend_i16(mut self, v: Int) prepend_i32
def prepend_i32(mut self, v: Int) prepend_i64
def prepend_i64(mut self, v: Int) start_table
def start_table(mut self, num_slots: Int) add_bool
def add_bool(mut self, slot: Int, v: Bool, default: Bool = False) Add a field, unless it already holds the schema default.
Omitting defaults is not just a size trick: a reader that asks for an absent field gets the default back, so writing it would be identical but larger.
add_i16
def add_i16(mut self, slot: Int, v: Int, default: Int = Int(0)) add_i32
def add_i32(mut self, slot: Int, v: Int, default: Int = Int(0)) add_i64
def add_i64(mut self, slot: Int, v: Int, default: Int = Int(0)) add_u8
def add_u8(mut self, slot: Int, v: UInt8, default: UInt8 = UInt8(0)) add_offset
def add_offset(mut self, slot: Int, target: Int) Add a reference field (table, string or vector). 0 means absent.
end_table
def end_table(mut self) -> Int Finish the table and return its offset.
Writes the vtable, then the table's own backwards pointer to it. The vtable is trimmed to the last field actually set, which is what lets a reader of a longer schema see the extra fields as absent rather than as garbage.
create_string
def create_string(mut self, s: String) -> Int Write a length-prefixed, NUL-terminated UTF-8 string.
start_vector
def start_vector(mut self, elem_size: Int, count: Int, align: Int) Begin a vector; elements are prepended in reverse order.
end_vector
def end_vector(mut self) -> Int create_offset_vector
def create_offset_vector(mut self, offsets: List[Int]) -> Int A vector of references, written back to front.
finish
def finish(mut self, root: Int) -> List[UInt8] Write the root offset and return the finished buffer.