Skip to main content

Reader

Struct Reader 

pub struct Reader {
    state: ReaderState,
    consumed: u64,
    max_frame_len: usize,
    layout: Layout,
}
Available on crate feature alloc only.
Expand description

Decodes CRC-framed journal records one frame at a time.

See the module-level documentation for the streaming protocol.

Fields§

§state: ReaderState§consumed: u64§max_frame_len: usize

Upper bound on a frame’s declared payload length. A header claiming more is rejected as corruption before the oversized read is demanded. usize::MAX (the new default) disables the bound.

§layout: Layout

The encoding of the ops in the frames.

Implementations§

§

impl Reader

pub const fn new() -> Self

Creates a fresh reader positioned at the start of a journal, with no bound on frame length.

pub const fn with_max_frame_len(max_frame_len: usize) -> Self

Creates a reader that rejects any frame whose declared payload length exceeds max_frame_len, reporting it as Step::Corrupt before demanding the oversized read.

A streaming driver should pass the journal’s total byte length. No frame can legitimately exceed the whole source, so a header claiming more is corruption (a flipped length byte or a crash-truncated tail) and must not be allowed to demand a multi-gigabyte read for a frame that cannot exist. The CRC, which covers the payload, can only be checked after the payload has been read, so this length bound is the only thing standing between one bad byte and a huge allocation.

pub const fn with_header(self, header: Header) -> Self

Reads the frames of a journal file that starts with header.

Without it, the reader expects frames in the format this crate writes.

pub const fn bytes_consumed(&self) -> u64

Returns the number of bytes successfully decoded and applied to the session.

If the reader reports corruption or the source ends mid-frame, this is the offset at which the source should be truncated.

pub const fn is_frame_boundary(&self) -> bool

Returns true if the reader is positioned exactly on a frame boundary, so EOF here is a clean shutdown (not corruption).

pub const fn demand(&self) -> Demand

Returns what the I/O layer should do next.

pub fn feed<S: State>( &mut self, chunk: &[u8], session: &mut S, ) -> Result<Step, FrameError<S::Error>>

Feeds a chunk of exactly the size requested by the previous demand call.

§Errors

Returns FrameError::Decode if a CRC-validated payload contains malformed ops (a bug, not crash corruption), and FrameError::Session if session has no room for them. All other problems (size mismatch, CRC mismatch) are reported as Step::Corrupt so the caller can self-heal by truncating.

pub fn replay_slice<S: State>( &mut self, bytes: &[u8], session: &mut S, ) -> Result<u64, FrameError<S::Error>>

Drives the reader over an in-memory journal slice, applying every valid frame to session and stopping at the first truncated or CRC-mismatched frame.

Returns the number of bytes successfully consumed; everything before that offset is known-good and everything after (if any) is the corrupt or truncated tail.

§Errors

Returns the error of feed.

fn feed_header(&mut self, chunk: &[u8]) -> Step

fn feed_payload<S: State>( &mut self, chunk: &[u8], length: usize, expected_crc: u32, session: &mut S, ) -> Result<Step, FrameError<S::Error>>

Trait Implementations§

§

impl Debug for Reader

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
§

impl Default for Reader

§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more