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>
impl<'a> VolatileSlice<'a>
sourcepub fn new(buf: &mut [u8]) -> VolatileSlice<'_> ⓘ
pub fn new(buf: &mut [u8]) -> VolatileSlice<'_> ⓘ
Creates a slice of raw memory that must support volatile access.
sourcepub unsafe fn from_raw_parts(addr: *mut u8, len: usize) -> VolatileSlice<'a> ⓘ
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
.
sourcepub fn as_mut_ptr(&self) -> *mut u8
pub fn as_mut_ptr(&self) -> *mut u8
Gets a mutable pointer to this slice’s memory.
sourcepub fn advance(&mut self, count: usize)
pub fn advance(&mut self, count: usize)
Advance the starting position of this slice.
Panics if count > self.size()
.
sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shorten the length of the slice.
Has no effect if len > self.size()
.
sourcepub fn as_iobufs<'mem, 'slice>(
iovs: &'slice [VolatileSlice<'mem>]
) -> &'slice [IoBufMut<'mem>]
pub fn as_iobufs<'mem, 'slice>( iovs: &'slice [VolatileSlice<'mem>] ) -> &'slice [IoBufMut<'mem>]
Converts a slice of VolatileSlice
s into a slice of IoBufMut
s
sourcepub fn as_iobufs_mut<'mem, 'slice>(
iovs: &'slice mut [VolatileSlice<'mem>]
) -> &'slice mut [IoBufMut<'mem>]
pub fn as_iobufs_mut<'mem, 'slice>( iovs: &'slice mut [VolatileSlice<'mem>] ) -> &'slice mut [IoBufMut<'mem>]
Converts a mutable slice of VolatileSlice
s into a mutable slice of IoBufMut
s
sourcepub fn offset(self, count: usize) -> VolatileMemoryResult<VolatileSlice<'a>>
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.
sourcepub fn sub_slice(
self,
offset: usize,
count: usize
) -> VolatileMemoryResult<VolatileSlice<'a>>
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.
sourcepub fn write_bytes(&self, value: u8)
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);
}
sourcepub fn copy_to<T>(&self, buf: &mut [T])where
T: FromBytes + AsBytes + Copy,
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);
}
sourcepub fn copy_to_volatile_slice(&self, slice: VolatileSlice<'_>)
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(|_| ())?);
sourcepub fn copy_from<T>(&self, buf: &[T])where
T: FromBytes + AsBytes,
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);
}
sourcepub fn is_all_zero(&self) -> bool
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>
impl<'a> AsIobuf for VolatileSlice<'a>
source§fn as_iobuf_slice(bufs: &[Self]) -> &[iovec]
fn as_iobuf_slice(bufs: &[Self]) -> &[iovec]
iovec
s that each describe a contiguous region of memory.source§fn as_iobuf_mut_slice(bufs: &mut [Self]) -> &mut [iovec]
fn as_iobuf_mut_slice(bufs: &mut [Self]) -> &mut [iovec]
iovecs
that each describe a contiguous region of memory.source§impl<'a> Clone for VolatileSlice<'a>
impl<'a> Clone for VolatileSlice<'a>
source§impl<'a> Debug for VolatileSlice<'a>
impl<'a> Debug for VolatileSlice<'a>
source§impl PartialEq<VolatileSlice<'_>> for VolatileSlice<'_>
impl PartialEq<VolatileSlice<'_>> for VolatileSlice<'_>
source§fn eq(&self, other: &VolatileSlice<'_>) -> bool
fn eq(&self, other: &VolatileSlice<'_>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> VolatileMemory for VolatileSlice<'a>
impl<'a> VolatileMemory for VolatileSlice<'a>
source§fn get_slice(
&self,
offset: usize,
count: usize
) -> VolatileMemoryResult<VolatileSlice<'_>>
fn get_slice( &self, offset: usize, count: usize ) -> VolatileMemoryResult<VolatileSlice<'_>>
offset
that is count
bytes in length and supports volatile
access.source§impl Write for VolatileSlice<'_>
impl Write for VolatileSlice<'_>
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)impl<'a> Copy for VolatileSlice<'a>
impl Eq for VolatileSlice<'_>
The PartialEq
implementation for VolatileSlice
is reflexive, symmetric, and transitive.