DataView

Struct DataView 

Source
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§

Source§

impl DataView

Source

pub fn from<T: ?Sized + Pod>(v: &T) -> &DataView

Returns a data view into the object’s memory.

Source

pub fn from_mut<T: ?Sized + Pod>(v: &mut T) -> &mut DataView

Returns a mutable data view into the object’s memory.

Source§

impl DataView

Source

pub const fn len(&self) -> usize

Returns the number of bytes in the instance.

Source

pub const fn tail_len<T>(&self, offset: usize) -> usize

Returns the number of elements that would fit a slice starting at the given offset.

Source§

impl DataView

Reads a (potentially unaligned) value from the view.

Source

pub fn try_read<T: Pod>(&self, offset: usize) -> Option<T>

Reads a (potentially unaligned) value from the view.

Source

pub fn read<T: Pod>(&self, offset: usize) -> T

Reads a (potentially unaligned) value from the view.

Source

pub unsafe fn read_unchecked<T: Pod>(&self, offset: usize) -> T

Reads a (potentially unaligned) value from the view.

Source§

impl DataView

Reads a (potentially unaligned) value from the view into the destination.

Source

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.

Source

pub fn read_into<T: ?Sized + Pod>(&self, offset: usize, dest: &mut T)

Reads a (potentially unaligned) value from the view into the destination.

Source

pub unsafe fn read_into_unchecked<T: ?Sized + Pod>( &self, offset: usize, dest: &mut T, )

Reads a (potentially unaligned) value from the view into the destination.

Source§

impl DataView

Gets an aligned reference into the view.

Source

pub fn try_get<T: Pod>(&self, offset: usize) -> Option<&T>

Gets an aligned reference into the view.

Source

pub fn get<T: Pod>(&self, offset: usize) -> &T

Gets an aligned reference into the view.

Source

pub unsafe fn get_unchecked<T: Pod>(&self, offset: usize) -> &T

Gets an aligned reference into the view.

Source§

impl DataView

Gets an aligned mutable reference into the view.

Source

pub fn try_get_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut T>

Gets an aligned mutable reference into the view.

Source

pub fn get_mut<T: Pod>(&mut self, offset: usize) -> &mut T

Gets an aligned mutable reference into the view.

Source

pub unsafe fn get_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut T

Gets an aligned mutable reference into the view.

Source§

impl DataView

Gets an aligned slice into the view.

Source

pub fn try_slice<T: Pod>(&self, offset: usize, len: usize) -> Option<&[T]>

Gets an aligned slice into the view.

Source

pub fn slice<T: Pod>(&self, offset: usize, len: usize) -> &[T]

Gets an aligned slice into the view.

Source

pub unsafe fn slice_unchecked<T: Pod>(&self, offset: usize, len: usize) -> &[T]

Gets an aligned slice into the view.

Source§

impl DataView

Gets an aligned mutable slice into the view.

Source

pub fn try_slice_mut<T: Pod>( &mut self, offset: usize, len: usize, ) -> Option<&mut [T]>

Gets an aligned mutable slice into the view.

Source

pub fn slice_mut<T: Pod>(&mut self, offset: usize, len: usize) -> &mut [T]

Gets an aligned mutable slice into the view.

Source

pub unsafe fn slice_unchecked_mut<T: Pod>( &mut self, offset: usize, len: usize, ) -> &mut [T]

Gets an aligned mutable slice into the view.

Source§

impl DataView

Writes a value into the view.

Source

pub fn try_write<T: ?Sized + Pod>( &mut self, offset: usize, value: &T, ) -> Option<()>

Writes a value into the view.

Source

pub fn write<T: ?Sized + Pod>(&mut self, offset: usize, value: &T)

Writes a value into the view.

Source

pub unsafe fn write_unchecked<T: ?Sized + Pod>( &mut self, offset: usize, value: &T, )

Writes a value into the view.

Source§

impl DataView

Source

pub fn index<R: RangeBounds<usize>>(&self, range: R) -> Option<&DataView>

Index the DataView creating a subview.

Source

pub fn index_mut<R: RangeBounds<usize>>( &mut self, range: R, ) -> Option<&mut DataView>

Index the DataView creating a mutable subview.

Trait Implementations§

Source§

impl AsMut<[u8]> for DataView

Source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<[u8]> for DataView

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<R: RangeBounds<usize>> Index<R> for DataView

Source§

type Output = DataView

The returned type after indexing.
Source§

fn index(&self, range: R) -> &DataView

Performs the indexing (container[index]) operation. Read more
Source§

impl<R: RangeBounds<usize>> IndexMut<R> for DataView

Source§

fn index_mut(&mut self, range: R) -> &mut DataView

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Pod for DataView

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> PodMethods for T
where T: Pod + ?Sized,

Source§

fn zeroed() -> T

Returns a zero-initialized instance of the type.
Source§

fn as_bytes(&self) -> &[u8]

Returns the object’s memory as a byte slice.
Source§

fn as_bytes_mut(&mut self) -> &mut [u8]

Returns the object’s memory as a mutable byte slice.
Source§

fn as_data_view(&self) -> &DataView

Returns a data view into the object’s memory.
Source§

fn as_data_view_mut(&mut self) -> &mut DataView

Returns a mutable data view into the object’s memory.