pub enum DataReader<'b> {
Slice(BufReader<&'b [u8]>),
Vec(BufReader<Vec<u8>>),
File(LazyCache<File>),
}Expand description
A generic reader for data backed by different sources.
Provides a uniform interface for reading from in-memory buffers or files.
Variants§
Slice(BufReader<&'b [u8]>)
A reader backed by a borrowed byte slice.
Useful for zero-copy reads from existing in-memory data.
Vec(BufReader<Vec<u8>>)
A reader backed by an owned byte vector.
Useful when the data needs to be owned.
File(LazyCache<File>)
A reader backed by a file with lazy caching.
Uses LazyCache for efficient disk I/O.
Implementations§
Source§impl DataReader<'_>
impl DataReader<'_>
Sourcepub fn from_file(r: File) -> Result<Self, Error>
pub fn from_file(r: File) -> Result<Self, Error>
Creates a new DataReader backed by a file with lazy caching.
The file is wrapped in a LazyCache with:
- A hot cache of 14 MiB (2 ×
FILE_BYTES_MAX) - A warm cache of 100 MiB
This configuration is optimized for file-based magic number detection, balancing memory usage with I/O efficiency.
§Errors
Returns an error if the file cannot be read or if cache initialization fails.
Source§impl<'b> DataReader<'b>
impl<'b> DataReader<'b>
Sourcepub fn from_slice(s: &'b [u8]) -> Self
pub fn from_slice(s: &'b [u8]) -> Self
Creates a new DataReader backed by a borrowed byte slice.
This is a zero-copy constructor that wraps the slice in a BufReader.
The lifetime of the returned reader is tied to the input slice.
§Examples
use pure_magic::readers::{DataReader, DataRead};
let data = b"hello world";
let reader = DataReader::from_slice(data);
assert_eq!(reader.data_size(), data.len() as u64);Source§impl DataReader<'_>
impl DataReader<'_>
Sourcepub fn from_vec(v: Vec<u8>) -> Self
pub fn from_vec(v: Vec<u8>) -> Self
Creates a new DataReader backed by an owned byte vector.
The vector is wrapped in a BufReader, allowing the data to be owned
independently of any borrow.
§Examples
use pure_magic::readers::{DataReader, DataRead};
let data = vec![1u8, 2, 3, 4, 5];
let reader = DataReader::from_vec(data);
assert_eq!(reader.data_size(), 5);Trait Implementations§
Source§impl DataRead for DataReader<'_>
impl DataRead for DataReader<'_>
Source§fn stream_position(&self) -> u64
fn stream_position(&self) -> u64
Source§fn offset_from_start(&self, pos: SeekFrom) -> u64
fn offset_from_start(&self, pos: SeekFrom) -> u64
SeekFrom position.Source§fn read_range(&mut self, range: Range<u64>) -> Result<&[u8], Error>
fn read_range(&mut self, range: Range<u64>) -> Result<&[u8], Error>
Source§fn read_count(&mut self, count: u64) -> Result<&[u8], Error>
fn read_count(&mut self, count: u64) -> Result<&[u8], Error>
count bytes from the current position. Read moreSource§fn read_exact_range(&mut self, range: Range<u64>) -> Result<&[u8], Error>
fn read_exact_range(&mut self, range: Range<u64>) -> Result<&[u8], Error>
Source§fn read_exact_count(&mut self, count: u64) -> Result<&[u8], Error>
fn read_exact_count(&mut self, count: u64) -> Result<&[u8], Error>
count bytes from the current position. Read moreSource§fn read_exact_into(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact_into(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_until_any_delim_or_limit(
&mut self,
delims: &[u8],
limit: u64,
) -> Result<&[u8], Error>
fn read_until_any_delim_or_limit( &mut self, delims: &[u8], limit: u64, ) -> Result<&[u8], Error>
limit bytes is reached. Read more