Struct base::sys::unix::file_traits::lib::VolatileSlice
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§
§impl<'a> VolatileSlice<'a>
impl<'a> VolatileSlice<'a>
pub fn new(buf: &mut [u8]) -> VolatileSlice<'_>
pub fn new(buf: &mut [u8]) -> VolatileSlice<'_>
Creates a slice of raw memory that must support volatile access.
pub 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.
pub fn as_mut_ptr(&self) -> *mut u8
pub fn as_mut_ptr(&self) -> *mut u8
Gets a mutable pointer to this slice’s memory.
pub 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().
pub 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().
pub 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 VolatileSlices into a slice of IoBufMuts
pub fn offset(
self,
count: usize
) -> Result<VolatileSlice<'a>, VolatileMemoryError>
pub fn offset(
self,
count: usize
) -> Result<VolatileSlice<'a>, VolatileMemoryError>
Creates a copy of this slice with the address increased by count bytes, and the size
reduced by count bytes.
pub fn sub_slice(
self,
offset: usize,
count: usize
) -> Result<VolatileSlice<'a>, VolatileMemoryError>
pub fn sub_slice(
self,
offset: usize,
count: usize
) -> Result<VolatileSlice<'a>, VolatileMemoryError>
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.
pub 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);
}pub 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);
}pub 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(|_| ())?);pub 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);
}pub 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>
§impl<'a> Clone for VolatileSlice<'a>
impl<'a> Clone for VolatileSlice<'a>
§fn clone(&self) -> VolatileSlice<'a>
fn clone(&self) -> VolatileSlice<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<'a> Debug for VolatileSlice<'a>
impl<'a> Debug for VolatileSlice<'a>
§impl PartialEq<VolatileSlice<'_>> for VolatileSlice<'_>
impl PartialEq<VolatileSlice<'_>> for VolatileSlice<'_>
§fn eq(&self, other: &VolatileSlice<'_>) -> bool
fn eq(&self, other: &VolatileSlice<'_>) -> bool
self and other values to be equal, and is used
by ==.§impl<'a> VolatileMemory for VolatileSlice<'a>
impl<'a> VolatileMemory for VolatileSlice<'a>
impl<'a> Copy for VolatileSlice<'a>
impl Eq for VolatileSlice<'_>
The PartialEq implementation for VolatileSlice is reflexive, symmetric, and transitive.