#[repr(transparent)]
pub struct VolatileSlice<'a>(IoBufMut<'a>);
Expand description

A slice of raw memory that supports volatile access. Like std::io::IoSliceMut, this type is guaranteed to be ABI-compatible with libc::iovec but unlike IoSliceMut, it doesn’t automatically deref to &mut [u8].

Tuple Fields§

§0: IoBufMut<'a>

Implementations§

source§

impl<'a> VolatileSlice<'a>

source

pub fn new(buf: &mut [u8]) -> VolatileSlice<'_>

Creates a slice of raw memory that must support volatile access.

source

pub unsafe fn from_raw_parts(addr: *mut u8, len: usize) -> VolatileSlice<'a>

Creates a VolatileSlice from a pointer and a length.

Safety

In order to use this method safely, addr must be valid for reads and writes of len bytes and should live for the entire duration of lifetime 'a.

source

pub fn as_ptr(&self) -> *const u8

Gets a const pointer to this slice’s memory.

source

pub fn as_mut_ptr(&self) -> *mut u8

Gets a mutable pointer to this slice’s memory.

source

pub fn size(&self) -> usize

Gets the size of this slice.

source

pub fn advance(&mut self, count: usize)

Advance the starting position of this slice.

Panics if count > self.size().

source

pub fn truncate(&mut self, len: usize)

Shorten the length of the slice.

Has no effect if len > self.size().

source

pub fn as_iobuf(&self) -> &IoBufMut<'_>

Returns this VolatileSlice as an IoBufMut.

source

pub fn as_iobufs<'mem, 'slice>( iovs: &'slice [VolatileSlice<'mem>] ) -> &'slice [IoBufMut<'mem>]

Converts a slice of VolatileSlices into a slice of IoBufMuts

source

pub fn as_iobufs_mut<'mem, 'slice>( iovs: &'slice mut [VolatileSlice<'mem>] ) -> &'slice mut [IoBufMut<'mem>]

Converts a mutable slice of VolatileSlices into a mutable slice of IoBufMuts

source

pub fn offset(self, count: usize) -> VolatileMemoryResult<VolatileSlice<'a>>

Creates a copy of this slice with the address increased by count bytes, and the size reduced by count bytes.

source

pub fn sub_slice( self, offset: usize, count: usize ) -> VolatileMemoryResult<VolatileSlice<'a>>

Similar to get_slice but the returned slice outlives this slice.

The returned slice’s lifetime is still limited by the underlying data’s lifetime.

source

pub fn write_bytes(&self, value: u8)

Sets each byte of this slice with the given byte, similar to memset.

The bytes of this slice are accessed in an arbitray order.

Examples
let mut mem = [0u8; 32];
let vslice = VolatileSlice::new(&mut mem[..]);
vslice.write_bytes(45);
for &v in &mem[..] {
    assert_eq!(v, 45);
}
source

pub fn copy_to<T>(&self, buf: &mut [T])where T: FromBytes + AsBytes + Copy,

Copies self.size() or buf.len() times the size of T bytes, whichever is smaller, to buf.

The copy happens from smallest to largest address in T sized chunks using volatile reads.

Examples
let mut mem = [0u8; 32];
let vslice = VolatileSlice::new(&mut mem[..]);
let mut buf = [5u8; 16];
vslice.copy_to(&mut buf[..]);
for v in &buf[..] {
    assert_eq!(buf[0], 0);
}
source

pub fn copy_to_volatile_slice(&self, slice: VolatileSlice<'_>)

Copies self.size() or slice.size() bytes, whichever is smaller, to slice.

The copies happen in an undefined order.

Examples
let mut mem = [0u8; 32];
let vslice = VolatileSlice::new(&mut mem[..]);
vslice.copy_to_volatile_slice(vslice.get_slice(16, 16).map_err(|_| ())?);
source

pub fn copy_from<T>(&self, buf: &[T])where T: FromBytes + AsBytes,

Copies self.size() or buf.len() times the size of T bytes, whichever is smaller, to this slice’s memory.

The copy happens from smallest to largest address in T sized chunks using volatile writes.

Examples
let mut mem = [0u8; 32];
let vslice = VolatileSlice::new(&mut mem[..]);
let buf = [5u8; 64];
vslice.copy_from(&buf[..]);
let mut copy_buf = [0u32; 4];
vslice.copy_to(&mut copy_buf);
for i in 0..4 {
    assert_eq!(copy_buf[i], 0x05050505);
}
source

pub fn is_all_zero(&self) -> bool

Returns whether all bytes in this slice are zero or not.

This is optimized for VolatileSlice aligned with 16 bytes.

TODO(b/274840085): Use SIMD for better performance.

Trait Implementations§

source§

impl<'a> AsIobuf for VolatileSlice<'a>

source§

fn as_iobuf(&self) -> iovec

Returns a iovec that describes a contiguous region of memory.
source§

fn as_iobuf_slice(bufs: &[Self]) -> &[iovec]

Returns a slice of iovecs that each describe a contiguous region of memory.
source§

fn as_iobuf_mut_slice(bufs: &mut [Self]) -> &mut [iovec]

Returns a mutable slice of iovecs that each describe a contiguous region of memory.
source§

impl<'a> Clone for VolatileSlice<'a>

source§

fn clone(&self) -> VolatileSlice<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for VolatileSlice<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<VolatileSlice<'_>> for VolatileSlice<'_>

source§

fn eq(&self, other: &VolatileSlice<'_>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> VolatileMemory for VolatileSlice<'a>

source§

fn get_slice( &self, offset: usize, count: usize ) -> VolatileMemoryResult<VolatileSlice<'_>>

Gets a slice of memory at offset that is count bytes in length and supports volatile access.
source§

impl<'a> Copy for VolatileSlice<'a>

source§

impl Eq for VolatileSlice<'_>

The PartialEq implementation for VolatileSlice is reflexive, symmetric, and transitive.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for VolatileSlice<'a>

§

impl<'a> Send for VolatileSlice<'a>

§

impl<'a> Sync for VolatileSlice<'a>

§

impl<'a> Unpin for VolatileSlice<'a>

§

impl<'a> !UnwindSafe for VolatileSlice<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.