Struct base::VolatileSlice

source ·
#[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 Write for VolatileSlice<'_>

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn flush(&mut self) -> Result<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
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 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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where 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 T
where 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 T
where 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 T
where 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.
§

impl<W> WriteBytesExt for W
where W: Write + ?Sized,

§

fn write_u8(&mut self, n: u8) -> Result<(), Error>

Writes an unsigned 8 bit integer to the underlying writer. Read more
§

fn write_i8(&mut self, n: i8) -> Result<(), Error>

Writes a signed 8 bit integer to the underlying writer. Read more
§

fn write_u16<T>(&mut self, n: u16) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned 16 bit integer to the underlying writer. Read more
§

fn write_i16<T>(&mut self, n: i16) -> Result<(), Error>
where T: ByteOrder,

Writes a signed 16 bit integer to the underlying writer. Read more
§

fn write_u24<T>(&mut self, n: u32) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned 24 bit integer to the underlying writer. Read more
§

fn write_i24<T>(&mut self, n: i32) -> Result<(), Error>
where T: ByteOrder,

Writes a signed 24 bit integer to the underlying writer. Read more
§

fn write_u32<T>(&mut self, n: u32) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned 32 bit integer to the underlying writer. Read more
§

fn write_i32<T>(&mut self, n: i32) -> Result<(), Error>
where T: ByteOrder,

Writes a signed 32 bit integer to the underlying writer. Read more
§

fn write_u48<T>(&mut self, n: u64) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned 48 bit integer to the underlying writer. Read more
§

fn write_i48<T>(&mut self, n: i64) -> Result<(), Error>
where T: ByteOrder,

Writes a signed 48 bit integer to the underlying writer. Read more
§

fn write_u64<T>(&mut self, n: u64) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned 64 bit integer to the underlying writer. Read more
§

fn write_i64<T>(&mut self, n: i64) -> Result<(), Error>
where T: ByteOrder,

Writes a signed 64 bit integer to the underlying writer. Read more
§

fn write_u128<T>(&mut self, n: u128) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned 128 bit integer to the underlying writer.
§

fn write_i128<T>(&mut self, n: i128) -> Result<(), Error>
where T: ByteOrder,

Writes a signed 128 bit integer to the underlying writer.
§

fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned n-bytes integer to the underlying writer. Read more
§

fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error>
where T: ByteOrder,

Writes a signed n-bytes integer to the underlying writer. Read more
§

fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error>
where T: ByteOrder,

Writes an unsigned n-bytes integer to the underlying writer. Read more
§

fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error>
where T: ByteOrder,

Writes a signed n-bytes integer to the underlying writer. Read more
§

fn write_f32<T>(&mut self, n: f32) -> Result<(), Error>
where T: ByteOrder,

Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer. Read more
§

fn write_f64<T>(&mut self, n: f64) -> Result<(), Error>
where T: ByteOrder,

Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer. Read more