Skip to main content

ReadAt

Trait ReadAt 

Source
pub trait ReadAt<'a> {
    type ReadOutput;

    const MODE: DataType;

    // Required methods
    fn read_at(buf: &'a [u8], offset: usize) -> Self::ReadOutput;
    fn default_output() -> Self::ReadOutput;

    // Provided methods
    fn read_with_tag_at(
        buf: &'a [u8],
        offset: usize,
        _tag: u8,
    ) -> Self::ReadOutput { ... }
    fn payload_block_end(_buf: &'a [u8], pos: usize) -> usize
       where Self: Sized { ... }
}
Expand description

Decode a value of type Self from a byte buffer at a given absolute offset.

MODE determines how the parent table locates this field’s data:

  • Inline: the bytes at field_position ARE the value.
  • Offset: the bytes at field_position are a u32 forward offset; the actual data starts at field_position + forward_offset.

The lifetime 'a ties the decoded value to the input buffer so that zero-copy reads (string slices, list views) compile correctly.

Required Associated Constants§

Source

const MODE: DataType

Whether the parent table stores this value inline or via a forward offset.

Required Associated Types§

Source

type ReadOutput

The type returned by read_at. For scalars this is Self; for strings it is &'a str; for arrays it is ListView<'a, T>.

Required Methods§

Source

fn read_at(buf: &'a [u8], offset: usize) -> Self::ReadOutput

Decode a value from buf at absolute byte position offset.

Source

fn default_output() -> Self::ReadOutput

Return the zero/empty default for this type’s ReadOutput.

Called by ListView::get when the forward-offset entry for an element is 0, which signals that the element slot is absent. This only fires for Offset- and Union-mode types; Inline types (scalars, bool, structs) are always present in their list slot, so their get path never reaches this method.

The default implementation is unreachable!() so that Inline-mode impls pay no code-size cost. Every Offset/Union impl must override this with the appropriate zero/empty value:

TypeDefault
&str / String""
Vec<T>empty ListView
FileBlob<T>empty slice view
generated TablesView::default()
generated UnionsNone variant

Provided Methods§

Source

fn read_with_tag_at(buf: &'a [u8], offset: usize, _tag: u8) -> Self::ReadOutput

Source

fn payload_block_end(_buf: &'a [u8], pos: usize) -> usize
where Self: Sized,

Returns the exclusive end address of the value whose payload starts at pos. Overridden by Table (→ view.block_end()), Struct (→ pos + size_of), and String (→ pos + 4 + length). Default returns pos (safe conservative fallback for scalars/unknown types).

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.

Implementations on Foreign Types§

Source§

impl<'a> ReadAt<'a> for &str

String layout: [u32 length][UTF-8 bytes...]

read_at is called with the absolute position of the length prefix. Returns a &'a str that borrows directly from the input buffer — no copy.

§Safety

The bytes are assumed to be valid UTF-8 (enforced at write time by str::as_bytes). Using from_utf8_unchecked avoids a redundant validation scan on every read.

Source§

const MODE: DataType = DataType::Offset

Source§

type ReadOutput = &'a str

Source§

fn read_at(buf: &'a [u8], offset: usize) -> &'a str

Source§

fn default_output() -> &'a str

Source§

fn payload_block_end(buf: &'a [u8], pos: usize) -> usize

Source§

impl<'a> ReadAt<'a> for bool

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = bool

Source§

fn read_at(buf: &[u8], offset: usize) -> bool

Source§

fn default_output() -> bool

Source§

impl<'a> ReadAt<'a> for f32

Source§

fn read_at(buf: &[u8], offset: usize) -> f32

Reads the bit pattern as u32 then reinterprets as f32.

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = f32

Source§

fn default_output() -> f32

Source§

impl<'a> ReadAt<'a> for f64

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = f64

Source§

fn read_at(buf: &[u8], offset: usize) -> f64

Source§

fn default_output() -> f64

Source§

impl<'a> ReadAt<'a> for i8

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = i8

Source§

fn read_at(buf: &[u8], offset: usize) -> i8

Source§

fn default_output() -> i8

Source§

impl<'a> ReadAt<'a> for i16

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = i16

Source§

fn read_at(buf: &[u8], offset: usize) -> i16

Source§

fn default_output() -> i16

Source§

impl<'a> ReadAt<'a> for i32

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = i32

Source§

fn read_at(buf: &[u8], offset: usize) -> i32

Source§

fn default_output() -> i32

Source§

impl<'a> ReadAt<'a> for i64

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = i64

Source§

fn read_at(buf: &[u8], offset: usize) -> i64

Source§

fn default_output() -> i64

Source§

impl<'a> ReadAt<'a> for i128

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = i128

Source§

fn read_at(buf: &[u8], offset: usize) -> i128

Source§

fn default_output() -> i128

Source§

impl<'a> ReadAt<'a> for u8

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = u8

Source§

fn read_at(buf: &[u8], offset: usize) -> u8

Source§

fn default_output() -> u8

Source§

impl<'a> ReadAt<'a> for u16

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = u16

Source§

fn read_at(buf: &[u8], offset: usize) -> u16

Source§

fn default_output() -> u16

Source§

impl<'a> ReadAt<'a> for u32

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = u32

Source§

fn read_at(buf: &[u8], offset: usize) -> u32

Source§

fn default_output() -> u32

Source§

impl<'a> ReadAt<'a> for u64

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = u64

Source§

fn read_at(buf: &[u8], offset: usize) -> u64

Source§

fn default_output() -> u64

Source§

impl<'a> ReadAt<'a> for u128

Source§

const MODE: DataType = DataType::Inline

Source§

type ReadOutput = u128

Source§

fn read_at(buf: &[u8], offset: usize) -> u128

Source§

fn default_output() -> u128

Source§

impl<'a> ReadAt<'a> for String

Identical to &str; exists so that Vec<String> fields use the same read path as Vec<&str> fields. Both return &'a str.

Source§

const MODE: DataType = DataType::Offset

Source§

type ReadOutput = &'a str

Source§

fn read_at(buf: &'a [u8], offset: usize) -> &'a str

Source§

fn default_output() -> &'a str

Source§

fn payload_block_end(buf: &'a [u8], pos: usize) -> usize

Source§

impl<'a, T> ReadAt<'a> for Vec<T>
where T: ReadAt<'a>,

Reads an array whose length is stored as a u32 length prefix immediately before the element data (or offset table for indirect elements).

Returns a ListView that borrows from buf. The ListView’s offset points to the first element (or first offset-table entry) — i.e. 4 bytes past the length prefix.

Source§

const MODE: DataType = DataType::Offset

Source§

type ReadOutput = ListView<'a, T>

Source§

fn read_at(buf: &'a [u8], offset: usize) -> ListView<'a, T>

Source§

fn default_output() -> ListView<'a, T>

Source§

impl<'a, T, const N: usize> ReadAt<'a> for [T; N]
where T: ReadAt<'a>,

Reads a fixed-size array [T; N] without a length prefix. Used for const-size inline buffers where N is known at compile time.

Source§

const MODE: DataType = DataType::Offset

Source§

type ReadOutput = ListView<'a, T>

Source§

fn read_at(buf: &'a [u8], offset: usize) -> ListView<'a, T>

Source§

fn default_output() -> ListView<'a, T>

Implementors§

Source§

impl<'a, T> ReadAt<'a> for FileBlob<T>
where T: FileKind,

Source§

const MODE: DataType = DataType::Offset

Source§

type ReadOutput = FileBlobView<'a, T>

Source§

impl<'a, T> ReadAt<'a> for ListView<'a, T>
where T: ReadAt<'a>,

ReadAt for ListView itself — allows nested arrays (Vec<Vec<T>>).

Source§

const MODE: DataType = DataType::Offset

Source§

type ReadOutput = ListView<'a, T>