pub struct DataView(_);
Expand description
Read and write data from and to the underlying byte buffer.
Construct the data view through Pod::as_data_view
or Pod::as_data_view_mut
.
Operations
Each set of operations may support a try, panicking and unchecked variations, see below for more information.
-
copy(offset)
Copies a (potentially unaligned) value out of the view.
-
copy_into(offset, dest)
Copies a (potentially unaligned) value out of the view into the dest argument.
-
read(offset)
Returns a reference to the data given the offset. Errors if the final pointer is misaligned for the given type.
-
read_mut(offset)
Returns a mutable reference to the data given the offset. Errors if the final pointer is misaligned for the given type.
-
slice(offset, len)
Returns 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)
Returns 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 would have returned None
then the Unchecked variation is Undefined Behavior.
Implementations
sourceimpl DataView
impl DataView
sourcepub fn try_copy<T: Pod>(&self, offset: usize) -> Option<T>
pub fn try_copy<T: Pod>(&self, offset: usize) -> Option<T>
Copies a (potentially unaligned) value from the view.
sourcepub fn copy<T: Pod>(&self, offset: usize) -> T
pub fn copy<T: Pod>(&self, offset: usize) -> T
Copies a (potentially unaligned) value from the view.
sourcepub unsafe fn copy_unchecked<T: Pod>(&self, offset: usize) -> T
pub unsafe fn copy_unchecked<T: Pod>(&self, offset: usize) -> T
Copies a (potentially unaligned) value from the view.
sourceimpl DataView
impl DataView
sourcepub fn try_copy_into<T: Pod + ?Sized>(
&self,
offset: usize,
dest: &mut T
) -> Option<()>
pub fn try_copy_into<T: Pod + ?Sized>(
&self,
offset: usize,
dest: &mut T
) -> Option<()>
Copies a (potentially unaligned) value from the view into the destination.
sourceimpl DataView
impl DataView
sourceimpl DataView
impl DataView
sourcepub fn try_read_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut T>
pub fn try_read_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut T>
Reads an aligned value from the view.
sourcepub fn read_mut<T: Pod>(&mut self, offset: usize) -> &mut T
pub fn read_mut<T: Pod>(&mut self, offset: usize) -> &mut T
Reads an aligned value from the view.
sourcepub unsafe fn read_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut T
pub unsafe fn read_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut T
Reads an aligned value from the view.