Trait GenericDataSource

Source
pub trait GenericDataSource: DataSource {
    // Provided methods
    fn read_int<T: Pod + PrimInt>(&mut self) -> Result<T> { ... }
    fn read_int_le<T: Pod + PrimInt>(&mut self) -> Result<T> { ... }
    fn read_data<T: Pod>(&mut self) -> Result<T> { ... }
    fn read_data_slice<'a, T: Pod>(
        &mut self,
        buf: &'a mut [T],
    ) -> Result<&'a [T]> { ... }
}
Expand description

Reads generic data from a source.

Provided Methods§

Source

fn read_int<T: Pod + PrimInt>(&mut self) -> Result<T>

Reads a big-endian integer.

§Errors

Returns Error::End if the stream ends before exactly the type’s size in bytes can be read.

§Example
use data_streams::GenericDataSource;
 
let mut buf: &[u8] = &[0x12, 0x34, 0x56, 0x78];
let int: u32 = buf.read_int()?;
assert_eq!(int, 0x12345678);
Source

fn read_int_le<T: Pod + PrimInt>(&mut self) -> Result<T>

Reads a little-endian integer.

§Errors

Returns Error::End if the stream ends before exactly the type’s size in bytes can be read.

§Example
use data_streams::GenericDataSource;

let mut buf: &[u8] = &[0x12, 0x34, 0x56, 0x78];
let int: u32 = buf.read_int_le()?;
assert_eq!(int, 0x78563412);
Source

fn read_data<T: Pod>(&mut self) -> Result<T>

Reads a value of generic type T supporting an arbitrary bit pattern. See Pod.

§Errors

Returns Error::End if the stream ends before exactly the type’s size in bytes can be read.

§Example
use data_streams::GenericDataSource;

let mut buf: &[u8] = &[0x12, 0x34, 0x56, 0x78];
let int: u32 = buf.read_data()?;
assert_eq!(int, 0x78563412);
Source

fn read_data_slice<'a, T: Pod>(&mut self, buf: &'a mut [T]) -> Result<&'a [T]>

Reads multiple values of generic type T supporting an arbitrary bit pattern, returning the read values.

§Errors

Returns any IO errors encountered.

§Panics

Panics if the DataSource::read_aligned_bytes implementation returns an unaligned slice.

§Example
use data_streams::GenericDataSource;

let mut input: &[u8] = &[0x12, 0x34, 0x56, 0x78, 0xFF];
let buf: &mut [u16] = &mut [0; 3];
assert_eq!(input.read_data_slice(buf)?, [0x3412, 0x7856]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§