ReadExactExt

Trait ReadExactExt 

Source
pub trait ReadExactExt: Read {
    // Provided methods
    fn read_array_exact<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]> { ... }
    fn read_bool(&mut self) -> Result<bool> { ... }
    fn read_vec_exact(&mut self, size: usize) -> Result<Vec<u8>> { ... }
    unsafe fn read_heapless_vec_exact<const SIZE: usize>(
        &mut self,
        size: usize,
    ) -> Result<Vec<u8, SIZE>> { ... }
    fn read_num_be<N, const SIZE: usize>(&mut self) -> Result<N>
       where N: FromBytes<Bytes = [u8; SIZE]> { ... }
    fn read_num_le<N, const SIZE: usize>(&mut self) -> Result<N>
       where N: FromBytes<Bytes = [u8; SIZE]> { ... }
    fn read_num_ne<N, const SIZE: usize>(&mut self) -> Result<N>
       where N: FromBytes<Bytes = [u8; SIZE]> { ... }
}

Provided Methods§

Source

fn read_array_exact<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]>

Read a byte array of a constant size.

For further semantics please refer to Read::read_exact.

§Examples
use rw_exact_ext::ReadExactExt;
use std::io::Cursor;

let bytes = [0xAB, 0xCD, 0xEF, 0x42];
let array: [u8; 4] = Cursor::new(&bytes).read_array_exact().unwrap();
assert_eq!(array, bytes);
Source

fn read_bool(&mut self) -> Result<bool>

Read one byte and interpret it as a bool.

Returns true if the read byte is non-zero, or false otherwise.

For further semantics please refer to Read::read_exact.

§Examples
use rw_exact_ext::ReadExactExt;
use std::io::Cursor;

let bytes = [0x01, 0x00, 0xEF, 0x42];
let mut cursor = Cursor::new(&bytes);
assert!(cursor.read_bool().unwrap());
assert!(!cursor.read_bool().unwrap());
assert!(cursor.read_bool().unwrap());
assert!(cursor.read_bool().unwrap());
Source

fn read_vec_exact(&mut self, size: usize) -> Result<Vec<u8>>

Read a Vec<u8> of a given size.

For further semantics please refer to Read::read_exact.

§Examples
use rw_exact_ext::ReadExactExt;
use std::io::Cursor;

let bytes = [0xAB, 0xCD, 0xEF, 0x42];
let vec = Cursor::new(&bytes).read_vec_exact(bytes.len()).unwrap();
assert_eq!(vec, Vec::from(bytes));
Source

unsafe fn read_heapless_vec_exact<const SIZE: usize>( &mut self, size: usize, ) -> Result<Vec<u8, SIZE>>

Read a heapless::Vec<u8> of a constant size.

For further semantics please refer to Read::read_exact.

§Safety

Behaviour of this method is undefined, if size > SIZE.

§Examples
use rw_exact_ext::ReadExactExt;
use std::io::Cursor;

let bytes = [0xAB, 0xCD, 0xEF, 0x42];
let vec: heapless::Vec<u8, 4> = unsafe { Cursor::new(&bytes).read_heapless_vec_exact(4).unwrap() };
eprintln!("Capacity: {}", vec.capacity());
eprintln!("Len: {}", vec.len());
assert_eq!(&vec, &bytes);
Source

fn read_num_be<N, const SIZE: usize>(&mut self) -> Result<N>
where N: FromBytes<Bytes = [u8; SIZE]>,

Read a number from a byte array in big endian.

For further semantics please refer to Read::read_exact.

§Examples
use rw_exact_ext::ReadExactExt;
use std::io::Cursor;

let bytes = [0xAB, 0xCD, 0xEF, 0x42];

let unsigned: u32 = Cursor::new(&bytes).read_num_be().unwrap();
assert_eq!(unsigned, 0xABCDEF42);

let signed: i32 = Cursor::new(&bytes).read_num_be().unwrap();
assert_eq!(signed, -0x543210BE);

let float: f32 = Cursor::new(&bytes).read_num_be().unwrap();
assert_eq!(float, -1.4632533e-12);
Source

fn read_num_le<N, const SIZE: usize>(&mut self) -> Result<N>
where N: FromBytes<Bytes = [u8; SIZE]>,

Read a number from a byte array in little endian.

For further semantics please refer to Read::read_exact.

§Examples
use rw_exact_ext::ReadExactExt;
use std::io::Cursor;

let bytes = [0xAB, 0xCD, 0xEF, 0x42];

let unsigned: u32 = Cursor::new(&bytes).read_num_le().unwrap();
assert_eq!(unsigned, 0x42EFCDAB);

let signed: i32 = Cursor::new(&bytes).read_num_le().unwrap();
assert_eq!(signed, 0x42EFCDAB);

let float: f32 = Cursor::new(&bytes).read_num_le().unwrap();
assert_eq!(float, 119.901695);
Source

fn read_num_ne<N, const SIZE: usize>(&mut self) -> Result<N>
where N: FromBytes<Bytes = [u8; SIZE]>,

Read a number from a byte array in native endianness.

For further semantics please refer to Read::read_exact.

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§

Source§

impl<T> ReadExactExt for T
where T: Read,