#[repr(transparent)]pub struct DataView { /* private fields */ }
Expand description
Read and write data to and from the underlying byte buffer.
Operations
Each set of operations may support a try, panicking and unchecked variations, see below for more information.
-
read(offset)
Reads a (potentially unaligned) value out of the view.
-
read_into(offset, dest)
Reads a (potentially unaligned) value out of the view into the dest argument.
-
get(offset)
Gets a reference to the data given the offset. Errors if the final pointer is misaligned for the given type.
-
get_mut(offset)
Gets a mutable reference to the data given the offset. Errors if the final pointer is misaligned for the given type.
-
slice(offset, len)
Gets a slice to the data given the offset and len. Errors if the final pointer is misaligned for the given type.
-
slice_mut(offset, len)
Gets a mutable slice to the data given the offset. Errors if the final pointer is misaligned for the given type.
-
write(offset, value)
Writes a value to the view at the given offset.
Panics
Panicking methods have no prefix or suffix. They invoke the Try methods and panic if they return None
.
When calling Panicking variation with an offset that ends up out of bounds or if the final pointer is misaligned
for the given type the method panics with the message "invalid offset"
.
The relevant methods are annotated with #[track_caller]
providing a useful location where the error happened.
Safety
The Unchecked methods have the _unchecked
suffix and simply assume the offset is correct.
This is Undefined Behavior when it results in an out of bounds read or write or if a misaligned reference is produced.
If the Try variation returns None
then the Unchecked variation invokes Undefined Behavior.
Implementations
sourceimpl DataView
impl DataView
Reads a (potentially unaligned) value from the view.
sourcepub fn try_read<T: Pod>(&self, offset: usize) -> Option<T>
pub fn try_read<T: Pod>(&self, offset: usize) -> Option<T>
Reads a (potentially unaligned) value from the view.
sourcepub fn read<T: Pod>(&self, offset: usize) -> T
pub fn read<T: Pod>(&self, offset: usize) -> T
Reads a (potentially unaligned) value from the view.
sourcepub unsafe fn read_unchecked<T: Pod>(&self, offset: usize) -> T
pub unsafe fn read_unchecked<T: Pod>(&self, offset: usize) -> T
Reads a (potentially unaligned) value from the view.
sourceimpl DataView
impl DataView
Reads a (potentially unaligned) value from the view into the destination.
sourcepub fn try_read_into<T: ?Sized + Pod>(
&self,
offset: usize,
dest: &mut T
) -> Option<()>
pub fn try_read_into<T: ?Sized + Pod>(
&self,
offset: usize,
dest: &mut T
) -> Option<()>
Reads a (potentially unaligned) value from the view into the destination.
sourceimpl DataView
impl DataView
Gets an aligned reference into the view.
sourceimpl DataView
impl DataView
Gets an aligned mutable reference into the view.
sourcepub fn try_get_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut T>
pub fn try_get_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut T>
Gets an aligned mutable reference into the view.
sourcepub fn get_mut<T: Pod>(&mut self, offset: usize) -> &mut T
pub fn get_mut<T: Pod>(&mut self, offset: usize) -> &mut T
Gets an aligned mutable reference into the view.
sourcepub unsafe fn get_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut T
pub unsafe fn get_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut T
Gets an aligned mutable reference into the view.
sourceimpl DataView
impl DataView
Gets an aligned slice into the view.
sourceimpl DataView
impl DataView
Gets an aligned mutable slice into the view.