pub struct BufSource<R> { /* private fields */ }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_from(&mut src).unwrap(), Word { value: 0x1234_5678 });Implementations§
Source§impl<R: Read> BufSource<R>
impl<R: Read> BufSource<R>
Sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Wraps inner with the default 64 KiB retention cap, MSB-first big-endian.
Sourcepub fn with_cap(inner: R, cap: usize) -> Self
pub fn with_cap(inner: R, cap: usize) -> Self
Wraps inner with a retention cap (bytes), MSB-first big-endian.
Sourcepub fn with_cap_and_layout(inner: R, cap: usize, layout: Layout) -> Self
pub fn with_cap_and_layout(inner: R, cap: usize, layout: Layout) -> Self
Wraps inner with a retention cap (bytes) and Layout.
Trait Implementations§
impl<R: Read> SeekSource for BufSource<R>
Available on crate feature
std only.Source§impl<R: Read> Source for BufSource<R>
Available on crate feature std only.
impl<R: Read> Source for BufSource<R>
Available on crate feature
std only.Source§fn byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
The byte order applied to a byte-multiple value (default big-endian).
Source§fn seek_to_bit(&mut self, pos: usize) -> Result<(), BitError>
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 moreSource§fn as_read(&mut self) -> SourceReader<'_, Self> ⓘwhere
Self: Sized,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more