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.
use data_view::DataView;
let mut view = DataView::new([0; 4]);
view.write::<u16>(42);
view.offset = 0;
assert_eq!(view.read::<u16>(), 42);
Creates a new View from a byte array.
The offset is set to 0.
use data_view::DataView;
let view = DataView::new([0; 16]);
Returns remaining slice from the current offset.
It doesn’t change the offset.
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.
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 if the offset is out of bounds.
Read slice from the current offset.
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 if the offset is out of bounds.
Create a buffer and returns it, from the current offset.
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 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.
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 if the offset is out of bounds.
Writes a slice into the data view.
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 if the offset is out of bounds.
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
impl<T> Any for T where
T: 'static + ?Sized,
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
impl<T, U> Into<U> for T where
U: From<T>,
The type returned in the event of a conversion error.
The type returned in the event of a conversion error.