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

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

Examples
use data_view::DataView;

let mut view = DataView::from([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: Endian from the DataView.

Examples
use data_view::DataView;

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

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

Reads a value of type E: Endian from the DataView, without doing bounds checking. For a safe alternative see read.

Safety

Calling this method with an out-of-bounds index is [undefined behavior]

Read slice from the current offset.

Example
use data_view::DataView;

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

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

Read slice from the current offset, without doing bounds checking. For a safe alternative see read_slice.

Example
use data_view::DataView;
let mut view = DataView::from([1, 2, 3, 4]);
unsafe {
    assert_eq!(view.read_slice_unchecked(2), &[1, 2]);
    assert_eq!(view.read_slice_unchecked(2), &[3, 4]);
}
Safety

Calling this method with an out-of-bounds index is [undefined behavior] even if the resulting reference is not used.

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

Examples
use data_view::DataView;

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

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

Returns a reference to an element or subslice, without doing bounds checking. For a safe alternative see read_buf.

Safety

Calling this method with an out-of-bounds index is [undefined behavior]

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::from([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::from([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

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

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

Examples
use data_view::DataView;

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

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.