Struct bin_layout::DataView
source · [−]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: usizeImplementations
sourceimpl<T> DataView<T> where
T: AsRef<[u8]>,
impl<T> DataView<T> where
T: AsRef<[u8]>,
sourcepub fn remaining_slice(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn remaining_slice(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
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());sourcepub fn read<E>(&mut self) -> Option<E> where
E: Endian,
pub fn read<E>(&mut self) -> Option<E> where
E: Endian,
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);sourceimpl<T> DataView<T> where
T: AsMut<[u8]>,
impl<T> DataView<T> where
T: AsMut<[u8]>,
sourcepub fn write<E>(&mut self, num: E) -> Result<(), ()> where
E: Endian,
pub fn write<E>(&mut self, num: E) -> Result<(), ()> where
E: Endian,
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; 3]);
assert_eq!(view.write(42_u16), Ok(()));
assert_eq!(view.write(123_u32), Err(()));sourcepub fn write_slice(&mut self, slice: impl AsRef<[u8]>) -> Result<(), ()>
pub fn write_slice(&mut self, slice: impl AsRef<[u8]>) -> Result<(), ()>
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
impl<T> Eq for DataView<T> where
T: Eq,
impl<T> StructuralEq for DataView<T>
impl<T> StructuralPartialEq for DataView<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for DataView<T> where
T: RefUnwindSafe,
impl<T> Send for DataView<T> where
T: Send,
impl<T> Sync for DataView<T> where
T: Sync,
impl<T> Unpin for DataView<T> where
T: Unpin,
impl<T> UnwindSafe for DataView<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more