Trait View

Source
pub trait View {
    // Required methods
    fn read_at<E: Endian>(&self, offset: usize) -> Option<E>;
    fn write_at<E: Endian>(&mut self, offset: usize, num: E) -> Result<(), ()>;
}
Expand description

A data view for reading and writing data in byte array.

§Examples

use data_view::View;

let mut buf = [0; 16];

buf.write_at(1, 42_u16);
assert_eq!(buf.read_at::<u16>(1).unwrap(), 42);

Required Methods§

Source

fn read_at<E: Endian>(&self, offset: usize) -> Option<E>

Reads a value of type E: Endian from view.

§Examples
use data_view::View;

let buf = [42];

assert_eq!(buf.read_at::<u8>(0), Some(42));
assert_eq!(buf.read_at::<u16>(1), None);
Source

fn write_at<E: Endian>(&mut self, offset: usize, num: E) -> Result<(), ()>

Writes a value of type E: Endian to data view.

§Examples
use data_view::View;

let mut buf = [0];

assert_eq!(buf.write_at(0, 42_u8), Ok(()));
assert_eq!(buf.write_at(1, 123_u16), Err(()));

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.

Implementations on Foreign Types§

Source§

impl View for [u8]

Source§

fn read_at<E: Endian>(&self, offset: usize) -> Option<E>

Source§

fn write_at<E: Endian>(&mut self, offset: usize, num: E) -> Result<(), ()>

Implementors§