flatbuf

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package flatbuf reads flatbuffers, by hand, from untrusted bytes, and builds them (Builder).

A Vortex file writes its flatbuffers with finish_minimal: no size prefix and no file identifier (vortex-array/src/flatbuffers/traits.rs). The root offset is the first four bytes.

Every offset is checked against the buffer before it is used, so a hostile buffer gives an *Error carrying the offending offset, never a panic and never a read out of bounds. Vectors are views over the buffer: no allocation is made from a length read in the buffer, except the slices of scalars that are bounded by the buffer size itself.

Field ids follow the declaration order of the table in the .fbs file; the caller passes a negative-free constant. A union occupies two ids: its type, then its value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder writes a flatbuffer back to front, like the reference implementation does. It finishes a buffer "minimal": no size prefix and no file identifier. A table is written between StartTable and EndTable, and everything it points to (child tables, strings, vectors) before StartTable. A Builder is reused with Reset, and then allocates nothing once its buffer has grown to the size of the largest message.

func NewBuilder

func NewBuilder(size int) *Builder

NewBuilder returns a Builder with room for size bytes.

func (*Builder) Bytes

func (b *Builder) Bytes(v []byte) Off

Bytes writes a vector of bytes.

func (*Builder) EndTable

func (b *Builder) EndTable() Off

EndTable writes the vtable and returns the table. A vtable equal to one already written is not written again: the table points to the first one, as the reference builder does, so a message of many tables of the same shape (the fields of a schema, say) carries one vtable.

func (*Builder) Finish

func (b *Builder) Finish(root Off) []byte

Finish writes the offset of the root table, and returns the message. It stays valid until the next Reset.

func (*Builder) Offsets

func (b *Builder) Offsets(v []Off) Off

Offsets writes a vector of tables or strings, which are already written.

func (*Builder) Ref

func (b *Builder) Ref(id int, off Off)

Ref sets a field that points to a table, a string or a vector.

func (*Builder) Reset

func (b *Builder) Reset()

Reset empties the builder, keeping its memory.

func (*Builder) StartTable

func (b *Builder) StartTable(n int)

StartTable begins a table of n fields (the ids 0 to n-1). A union takes two ids.

func (*Builder) String

func (b *Builder) String(s string) Off

String writes a string, with its terminating zero.

func (*Builder) Structs

func (b *Builder) Structs(size, align int, raw []byte) Off

Structs writes a vector of structs of size bytes each, with the given alignment. raw holds the structs in order, already in little-endian: its length is a multiple of size.

func (*Builder) Uint8

func (b *Builder) Uint8(id int, v, def uint8)

Uint8 sets a field of one byte (also a bool or an enum). A field equal to its default is left out.

func (*Builder) Uint16

func (b *Builder) Uint16(id int, v, def uint16)

Uint16 sets a field of two bytes.

func (*Builder) Uint16s

func (b *Builder) Uint16s(v []uint16) Off

Uint16s writes a vector of uint16.

func (*Builder) Uint32

func (b *Builder) Uint32(id int, v, def uint32)

Uint32 sets a field of four bytes.

func (*Builder) Uint32s

func (b *Builder) Uint32s(v []uint32) Off

Uint32s writes a vector of uint32.

func (*Builder) Uint64

func (b *Builder) Uint64(id int, v, def uint64)

Uint64 sets a field of eight bytes.

func (*Builder) Union

func (b *Builder) Union(id int, typ uint8, value Off)

Union sets the two fields of a union: its type at id, its value at id+1.

type Error

type Error struct {
	Msg string // what was wrong with it
	Off int    // the offset that was read, or that a computed offset led to
}

Error is a bounds violation in a buffer.

func (*Error) Error

func (e *Error) Error() string

type Off

type Off uint32

Off is the position of a table, a vector or a string that a Builder has written: its distance from the end of the buffer. It is only meaningful for the Builder that returned it.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table is a view of one flatbuffers table.

func Root

func Root(buf []byte, maxDepth int) (Table, error)

Root returns the root table of buf. maxDepth bounds how many tables may be nested below (and including) the root; the root is at depth 1.

func (Table) AppendUint16s added in v0.1.1

func (t Table) AppendUint16s(dst []uint16, id int) ([]uint16, error)

AppendUint16s appends a vector of uint16 to dst: in its memory when it has room.

func (Table) Bool

func (t Table) Bool(id int, def bool) (bool, error)

Bool returns the field, or def when it is absent.

func (Table) Bytes

func (t Table) Bytes(id int) ([]byte, bool, error)

Bytes returns a vector of bytes (or a string's bytes). It aliases the buffer. The bool is false when the field is absent.

func (Table) Has

func (t Table) Has(id int) bool

Has reports whether the table has the field. A field with a "= null" default is optional: Has tells it apart from a field set to zero.

func (Table) Int8

func (t Table) Int8(id int, def int8) (int8, error)

Int8 returns the field, or def when it is absent.

func (Table) String

func (t Table) String(id int) (string, bool, error)

String returns a string field. The bool is false when the field is absent.

func (Table) Strings

func (t Table) Strings(id int) (Vector, error)

Strings returns a vector of strings.

func (Table) Structs

func (t Table) Structs(id, size int) (Vector, error)

Structs returns a vector of structs of size bytes each.

func (Table) Table

func (t Table) Table(id int) (Table, bool, error)

Table returns a table field. The bool is false when the field is absent.

func (Table) Tables

func (t Table) Tables(id int) (Vector, error)

Tables returns a vector of tables.

func (Table) Uint8

func (t Table) Uint8(id int, def uint8) (uint8, error)

Uint8 returns the field, or def when it is absent. Bools and enums of a byte use it too.

func (Table) Uint16

func (t Table) Uint16(id int, def uint16) (uint16, error)

Uint16 returns the field, or def when it is absent.

func (Table) Uint16s

func (t Table) Uint16s(id int) ([]uint16, error)

Uint16s returns a vector of uint16 as a new slice, its size bounded by the buffer.

func (Table) Uint32

func (t Table) Uint32(id int, def uint32) (uint32, error)

Uint32 returns the field, or def when it is absent.

func (Table) Uint32Vec added in v0.2.1

func (t Table) Uint32Vec(id int) (Uint32Vec, error)

Uint32Vec returns a vector of uint32, its size bounded by the buffer. It is empty when the field is absent.

func (Table) Uint64

func (t Table) Uint64(id int, def uint64) (uint64, error)

Uint64 returns the field, or def when it is absent.

func (Table) Union

func (t Table) Union(id int) (uint8, Table, error)

Union returns the type and the table of a union field: the type is at id, the value at id+1. The type is 0 (NONE) when the field is absent.

type Uint32Vec added in v0.2.1

type Uint32Vec struct {
	// contains filtered or unexported fields
}

Uint32Vec is a vector of uint32 that is not decoded yet: its reader chooses where the numbers go, so that the vectors of a whole tree can share one slice.

func (Uint32Vec) Len added in v0.2.1

func (v Uint32Vec) Len() int

Len is the number of elements.

func (Uint32Vec) Read added in v0.2.1

func (v Uint32Vec) Read(dst []uint32)

Read decodes the vector to dst, which has Len elements.

type Vector

type Vector struct {
	// contains filtered or unexported fields
}

Vector is a view of a vector of tables, strings or structs, built by Tables, Strings or Structs.

func (Vector) Len

func (v Vector) Len() int

Len is the number of elements, 0 for an absent vector.

func (Vector) String

func (v Vector) String(i int) (string, error)

String returns the i-th string of a vector built by Strings.

func (Vector) Struct

func (v Vector) Struct(i int) ([]byte, error)

Struct returns the bytes of the i-th struct of a vector built by Structs. It aliases the buffer.

func (Vector) Table

func (v Vector) Table(i int) (Table, error)

Table returns the i-th table of a vector built by Tables.

Directories

Path Synopsis
Package fbtest builds flatbuffers for tests.
Package fbtest builds flatbuffers for tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL