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.

Examples

use data_view::DataView;

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

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

Fields

data: Toffset: usize

Implementations

Creates a new View from a byte array. The offset is set to 0.

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, 3]);

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

Reads a value of type E from the data view. where E implements Endian.

Examples
use data_view::DataView;

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

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

Panics if the offset is out of bounds.

Read slice from the current offset.

Example
use data_view::DataView;

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

assert_eq!(view.read_slice(2), &[1, 2]);
assert_eq!(view.read_slice(2), &[3, 4]);
Panics

Panics if the offset is out of bounds.

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

Examples
use data_view::DataView;

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

assert_eq!(view.read_buf::<2>(), [1, 2]);
assert_eq!(view.read_buf::<2>(), [3, 4]);
Panics

Panics if the offset is out of bounds.

Writes a value of type E to the data view. where E is a type that implements Endian.

Examples
use data_view::DataView;

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

view.write::<u8>(12);
view.write::<u8>(34);
assert_eq!(view.data, [12, 34]);
Panics

Panics if the offset is out of bounds.

Writes a slice into the data view.

Examples
use data_view::DataView;

let mut view = DataView::new([0; 4]);
view.write_slice([1, 2]);
view.write_slice([3, 4]);
assert_eq!(view.data, [1, 2, 3, 4]);
Panics

Panics if the offset is out of bounds.

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

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.

Performs the conversion.

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.