Skip to main content

BufSource

Struct BufSource 

Source
pub struct BufSource<R> { /* private fields */ }
Available on crate feature std only.
Expand description

A seekable Source over a forward Read (a socket): it retains the bytes it has read, so a seek-using message (restore_position) works over a non-seekable stream by seeking within the retained buffer, reading more on demand. It is bounded — a retention cap (default 64 KiB) past which it errors ErrorKind::BufferFull rather than buffering unboundedly. The “continuously-receiving peer that also needs to seek” case.

§Examples

use bnb::{bin, BufSource};

#[bin(big)]
#[derive(Debug, PartialEq)]
struct Word { value: u32 }

let mut src = BufSource::new(&[0x12, 0x34, 0x56, 0x78][..]); // any `Read`
assert_eq!(Word::decode(&mut src).unwrap(), Word { value: 0x1234_5678 });

Implementations§

Source§

impl<R: Read> BufSource<R>

Source

pub fn new(inner: R) -> Self

Wraps inner with the default 64 KiB retention cap, MSB-first big-endian.

Source

pub fn with_capacity(inner: R, cap: usize) -> Self

Wraps inner with a retention cap (bytes), MSB-first big-endian.

Source

pub fn with_capacity_and_layout(inner: R, cap: usize, layout: Layout) -> Self

Wraps inner with a retention cap (bytes) and Layout.

Trait Implementations§

Source§

impl<R: Clone> Clone for BufSource<R>

Source§

fn clone(&self) -> BufSource<R>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug> Debug for BufSource<R>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<R: Read> SeekSource for BufSource<R>

Source§

impl<R: Read> Source for BufSource<R>

Source§

fn read_bits(&mut self, n: u32) -> Result<u128, BitError>

Reads n (<= 128) bits into the low bits of a u128, in the source’s bit order (MSB-first by default). Read more
Source§

fn bit_pos(&self) -> usize

The current absolute bit offset (for position-aware errors).
Source§

fn byte_order(&self) -> ByteOrder

The byte order applied to a byte-multiple value (default big-endian).
Source§

fn bit_order(&self) -> BitOrder

The bit order this source reads in (default MSB-first). Paired with byte_order to decide whether a byte-multiple value needs its bytes swapped — each bit order has a natural byte layout (big-endian under MSB, little-endian under LSB), and only the opposite declaration swaps.
Source§

fn seek_to_bit(&mut self, pos: usize) -> Result<(), BitError>

Moves the cursor to absolute bit pos. The default — for a forward-only source — fails with ErrorKind::NotSeekable; seekable sources (the slice BitReader) override it. A SeekSource guarantees this works. Read more
Source§

fn read<T: Bits>(&mut self) -> Result<T, BitError>

Reads one Bits value of its declared width, applying the byte order. Read more
Source§

fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, BitError>

Reads n bytes into a fresh Vec (at any bit offset — the bytes need not be aligned). The bulk form of the per-byte read::<u8>() loop, for blob/payload reads in custom codecs and container formats. Read more
Source§

fn read_into(&mut self, buf: &mut [u8]) -> Result<(), BitError>

Fills buf with bytes from the source — the no-alloc dual of read_bytes, for fixed scratch buffers and tight no_std paths. Read more
Source§

fn as_read(&mut self) -> SourceReader<'_, Self>
where Self: Sized,

Borrows this source as a std::io::Read over its bytes — for handing the cursor to std::io-based code from a #[br(parse_with = …)] (e.g. a decoder, or a Read-based parser). Reads 8 bits per byte; see SourceReader. Only with the std feature.

Auto Trait Implementations§

§

impl<R> Freeze for BufSource<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for BufSource<R>
where R: RefUnwindSafe,

§

impl<R> Send for BufSource<R>
where R: Send,

§

impl<R> Sync for BufSource<R>
where R: Sync,

§

impl<R> Unpin for BufSource<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for BufSource<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for BufSource<R>
where R: UnwindSafe,

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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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