Skip to main content

DataReader

Enum DataReader 

Source
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<'_>

Source

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:

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>

Source

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<'_>

Source

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<'_>

Source§

fn stream_position(&self) -> u64

Returns the current position in the data stream.
Source§

fn offset_from_start(&self, pos: SeekFrom) -> u64

Computes the absolute byte offset from a SeekFrom position.
Source§

fn read_range(&mut self, range: Range<u64>) -> Result<&[u8], Error>

Reads a range of bytes from the data. Read more
Source§

fn read_count(&mut self, count: u64) -> Result<&[u8], Error>

Reads up to count bytes from the current position. Read more
Source§

fn read_exact_range(&mut self, range: Range<u64>) -> Result<&[u8], Error>

Reads exactly the specified byte range. Read more
Source§

fn read_exact_count(&mut self, count: u64) -> Result<&[u8], Error>

Reads exactly count bytes from the current position. Read more
Source§

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

Reads exactly enough bytes to fill buf. Read more
Source§

fn read_until_any_delim_or_limit( &mut self, delims: &[u8], limit: u64, ) -> Result<&[u8], Error>

Reads bytes until any of the delimiters or limit bytes is reached. Read more
Source§

fn read_until_or_limit(&mut self, byte: u8, limit: u64) -> Result<&[u8], Error>

Reads bytes until byte or limit bytes is reached. Read more
Source§

fn read_while_or_limit<F>(&mut self, f: F, limit: u64) -> Result<&[u8], Error>
where F: Fn(u8) -> bool,

Reads bytes while f returns true or until limit bytes is reached. Read more
Source§

fn read_until_utf16_or_limit( &mut self, utf16_char: &[u8; 2], limit: u64, ) -> Result<&[u8], Error>

Reads bytes until a UTF-16 character or limit bytes is reached. Read more
Source§

fn data_size(&self) -> u64

Returns the total size of the data in bytes.
Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Sets the position for future reads.

Auto Trait Implementations§

§

impl<'b> Freeze for DataReader<'b>

§

impl<'b> RefUnwindSafe for DataReader<'b>

§

impl<'b> Send for DataReader<'b>

§

impl<'b> Sync for DataReader<'b>

§

impl<'b> Unpin for DataReader<'b>

§

impl<'b> UnsafeUnpin for DataReader<'b>

§

impl<'b> UnwindSafe for DataReader<'b>

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> 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, 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