Trait RelaxedFlatBufferTrait

Source
pub unsafe trait RelaxedFlatBufferTrait<TBuffer>
where Self: Deref<Target = [u8]> + Sized,
{ type FlatBuffer: RelaxedFollowTrait + Verifiable; // Required method fn new(data: TBuffer) -> Result<Self, InvalidFlatbuffer>; // Provided methods fn as_actual( &self, ) -> <<<Self as RelaxedFlatBufferTrait<TBuffer>>::FlatBuffer as RelaxedFollowTrait>::Inner<'_> as Follow<'_>>::Inner { ... } fn verify(data: &[u8]) -> Result<(), InvalidFlatbuffer> { ... } }
Expand description

This trait serves as the foundation for this crate.
It allows access to the underlying FlatBuffer using the as_actual() method.
The lifetimes used here are more relaxed than those in the flatc generated code.
For example, it could be implemented for a fully owned Box<\[u8\]> new-type or even a reference to memory that has been temporarily pinned following a DB query.

This trait requires the RelaxedFollowTrait trait bound on the FlatBuffer type.
It can be implemented either manually or using the flatbuffers_owned! macro.

§Safety

The default implementation of the as_actual() method uses the unsafe .follow() method do return the actual FlatBuffer.
as_actual() does not verify the FlatBuffer.
If you choose to implement this trait manually, you must ensure that the underlying byte slice is verified and the buffer remains immutable.

§Example trait usage


fn store_fbs<TBuffer>(flatbuffers: &[impl RelaxedFlatBufferTrait<TBuffer>]) {
   for item in flatbuffers {
        let bytes: &[u8] = item.deref();
        // ... store the raw bytes somewhere.
   }
}

Required Associated Types§

Required Methods§

Source

fn new(data: TBuffer) -> Result<Self, InvalidFlatbuffer>

Provided Methods§

Source

fn as_actual( &self, ) -> <<<Self as RelaxedFlatBufferTrait<TBuffer>>::FlatBuffer as RelaxedFollowTrait>::Inner<'_> as Follow<'_>>::Inner

Initializes a actual FlatBuffer struct from the byte slice returned by the Self::deref() method.

Source

fn verify(data: &[u8]) -> Result<(), InvalidFlatbuffer>

Verifies the FlatBuffer data.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§