pub trait Source {
// Required methods
fn read_bits(&mut self, n: u32) -> Result<u128, BitError>;
fn bit_pos(&self) -> usize;
// Provided methods
fn byte_order(&self) -> ByteOrder { ... }
fn seek_to_bit(&mut self, _pos: usize) -> Result<(), BitError> { ... }
fn read<T: Bits>(&mut self) -> Result<T, BitError> { ... }
fn as_read(&mut self) -> SourceReader<'_, Self> ⓘ
where Self: Sized { ... }
}Expand description
A bit-level input the codec recurses over. Implemented by BitReader
(in-memory slice), StreamBitReader (forward Read), BufSource (a
retain-and-seek socket adapter), and SeekReader (Read + Seek); the codec is
generic over Source, so one decoder runs over any of them — see
guide::io.
§Examples
use bnb::{BitReader, Source, u4};
// A reader generic over any `Source`.
fn first_nibble<S: Source>(s: &mut S) -> u4 { s.read().unwrap() }
let mut r = BitReader::new(&[0xA5]);
assert_eq!(first_nibble(&mut r), u4::new(0xA));Required Methods§
Provided Methods§
Sourcefn byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
The byte order applied to a byte-multiple value (default big-endian).
Sourcefn 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.
§Errors
ErrorKind::NotSeekable unless the source is seekable.
Sourcefn 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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".