pub struct DataView<T> {
    pub data: T,
    pub offset: usize,
}
Expand description

This struct represents a data view for reading and writing data in a byte array. When read/write, This increment current offset by the size of the value.

Fields

data: Toffset: usize

Implementations

Examples
use data_view::DataView;

let view = DataView::new([0; 16]);

Returns remaining slice from the current offset. It doesn’t change the offset.

Examples
use data_view::DataView;

let mut view = DataView::new([1, 2]);

assert_eq!(view.remaining_slice(), &[1, 2]);
view.offset = 42;
assert!(view.remaining_slice().is_empty());

Reads a value of type E: Endian from the DataView.

Examples
use data_view::DataView;

let mut view = DataView::new([0; 4]);

view.write::<u16>(42);
view.offset = 0;
assert_eq!(view.read::<u16>(), Some(42));
assert_eq!(view.read::<u32>(), None);

Read slice from the current offset.

Example
use data_view::DataView;

let mut view = DataView::new([1, 2, 3]);

assert_eq!(view.read_slice(2), Some([1, 2].as_ref()));
assert_eq!(view.read_slice(3), None);

Create a buffer and returns it, from the current offset.

Examples
use data_view::DataView;

let mut view = DataView::new([1, 2, 3]);

assert_eq!(view.read_buf(), Some([1, 2]));
assert_eq!(view.read_buf::<3>(), None);
Examples
use data_view::DataView;

let mut view = DataView::new([0; 3]);

assert_eq!(view.write(42_u16), Ok(()));
assert_eq!(view.write(123_u32), Err(()));

Writes a slice into the data view.

Examples
use data_view::DataView;

let mut view = DataView::new([0; 3]);

assert_eq!(view.write_slice([4, 2]), Ok(()));
assert_eq!(view.write_slice([1, 2, 3]), Err(()));
assert_eq!(view.data, [4, 2, 0]);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.