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 atfield_positionARE the value.Offset: the bytes atfield_positionare a u32 forward offset; the actual data starts atfield_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§
Required Associated Types§
Sourcetype ReadOutput
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§
Sourcefn read_at(buf: &'a [u8], offset: usize) -> Self::ReadOutput
fn read_at(buf: &'a [u8], offset: usize) -> Self::ReadOutput
Decode a value from buf at absolute byte position offset.
Sourcefn default_output() -> Self::ReadOutput
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:
| Type | Default |
|---|---|
&str / String | "" |
Vec<T> | empty ListView |
FileBlob<T> | empty slice view |
| generated Tables | View::default() |
| generated Unions | None variant |
Provided Methods§
fn read_with_tag_at(buf: &'a [u8], offset: usize, _tag: u8) -> Self::ReadOutput
Sourcefn payload_block_end(_buf: &'a [u8], pos: usize) -> usizewhere
Self: Sized,
fn payload_block_end(_buf: &'a [u8], pos: usize) -> usizewhere
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...]
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§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.
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§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).
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§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.
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.