[−][src]Struct vm_memory::volatile_memory::VolatileSlice
A slice of raw memory that supports volatile access.
Implementations
impl<'a> VolatileSlice<'a>
[src]
pub unsafe fn new(addr: *mut u8, size: usize) -> VolatileSlice<'a>
[src]
Creates a slice of raw memory that must support volatile access.
Safety
To use this safely, the caller must guarantee that the memory at addr
is size
bytes long
and is available for the duration of the lifetime of the new VolatileSlice
. The caller
must also guarantee that all other users of the given chunk of memory are using volatile
accesses.
pub fn as_ptr(&self) -> *mut u8
[src]
Returns a pointer to the beginning of the slice.
pub fn len(&self) -> usize
[src]
Gets the size of this slice.
pub fn is_empty(&self) -> bool
[src]
Checks if the slice is empty.
pub fn offset(self, count: usize) -> Result<VolatileSlice<'a>>
[src]
Returns a subslice of this VolatileSlice
starting at
offset
.
The returned subslice is a copy of this slice with the address increased by count
bytes
and the size reduced by count
bytes.
pub fn copy_to<T>(&self, buf: &mut [T]) -> usize where
T: ByteValued,
[src]
T: ByteValued,
Copies as many elements of type T
as possible from this slice to buf
.
Copies self.len()
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 mem_ref = &mut mem[..]; let vslice = mem_ref.get_slice(0, 32).map_err(|_| ())?; 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)
[src]
Copies as many bytes as possible from this slice to the provided slice
.
The copies happen in an undefined order.
Examples
let mut mem = [0u8; 32]; let mem_ref = &mut mem[..]; let vslice = mem_ref.get_slice(0, 32).map_err(|_| ())?; vslice.copy_to_volatile_slice(vslice.get_slice(16, 16).map_err(|_| ())?);
pub fn copy_from<T>(&self, buf: &[T]) where
T: ByteValued,
[src]
T: ByteValued,
Copies as many elements of type T
as possible from buf
to this slice.
The copy happens from smallest to largest address in T
sized chunks using volatile writes.
Examples
let mut mem = [0u8; 32]; let mem_ref = &mut mem[..]; let vslice = mem_ref.get_slice(0, 32).map_err(|_| ())?; let buf = [5u8; 64]; vslice.copy_from(&buf[..]); for i in 0..4 { assert_eq!(vslice.get_ref::<u32>(i * 4).map_err(|_| ())?.load(), 0x05050505); }
Trait Implementations
impl<'_> Bytes<usize> for VolatileSlice<'_>
[src]
type E = Error
Associated error codes
fn write(&self, buf: &[u8], addr: usize) -> Result<usize>
[src]
Examples
- Write a slice of size 5 at offset 1020 of a 1024-byte VolatileSlice.
let res = vslice.write(&[1,2,3,4,5], 1020); assert!(res.is_ok()); assert_eq!(res.unwrap(), 4);
fn read(&self, buf: &mut [u8], addr: usize) -> Result<usize>
[src]
Examples
- Read a slice of size 16 at offset 1010 of a 1024-byte VolatileSlice.
let buf = &mut [0u8; 16]; let res = vslice.read(buf, 1010); assert!(res.is_ok()); assert_eq!(res.unwrap(), 14);
fn write_slice(&self, buf: &[u8], addr: usize) -> Result<()>
[src]
fn read_slice(&self, buf: &mut [u8], addr: usize) -> Result<()>
[src]
Examples
- Read a slice of size 16 at offset 256.
let buf = &mut [0u8; 16]; let res = vslice.read_slice(buf, 256);
fn read_from<F>(&self, addr: usize, src: &mut F, count: usize) -> Result<usize> where
F: Read,
[src]
F: Read,
Examples
- Read bytes from /dev/urandom
let mut file = File::open(Path::new("/dev/urandom")).map_err(|_| ())?; vslice.read_from(32, &mut file, 128).map_err(|_| ())?; let rand_val: u32 = vslice.read_obj(40).map_err(|_| ())?;
fn read_exact_from<F>(
&self,
addr: usize,
src: &mut F,
count: usize
) -> Result<()> where
F: Read,
[src]
&self,
addr: usize,
src: &mut F,
count: usize
) -> Result<()> where
F: Read,
Examples
- Read bytes from /dev/urandom
let mut file = File::open(Path::new("/dev/urandom")).map_err(|_| ())?; vslice.read_exact_from(32, &mut file, 128).map_err(|_| ())?; let rand_val: u32 = vslice.read_obj(40).map_err(|_| ())?;
fn write_to<F>(&self, addr: usize, dst: &mut F, count: usize) -> Result<usize> where
F: Write,
[src]
F: Write,
Examples
- Write 128 bytes to /dev/null
let mut file = File::open(Path::new("/dev/null")).map_err(|_| ())?; vslice.write_to(32, &mut file, 128).map_err(|_| ())?;
fn write_all_to<F>(&self, addr: usize, dst: &mut F, count: usize) -> Result<()> where
F: Write,
[src]
F: Write,
Examples
- Write 128 bytes to /dev/null
let mut file = File::open(Path::new("/dev/null")).map_err(|_| ())?; vslice.write_all_to(32, &mut file, 128).map_err(|_| ())?;
fn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E>
[src]
fn read_obj<T: ByteValued>(&self, addr: A) -> Result<T, Self::E>
[src]
impl<'a> Clone for VolatileSlice<'a>
[src]
fn clone(&self) -> VolatileSlice<'a>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<'a> Copy for VolatileSlice<'a>
[src]
impl<'a> Debug for VolatileSlice<'a>
[src]
impl<'a> From<VolatileSlice<'a>> for VolatileArrayRef<'a, u8>
[src]
fn from(slice: VolatileSlice<'a>) -> Self
[src]
impl<'_> VolatileMemory for VolatileSlice<'_>
[src]
fn len(&self) -> usize
[src]
fn get_slice(&self, offset: usize, count: usize) -> Result<VolatileSlice>
[src]
fn is_empty(&self) -> bool
[src]
fn as_volatile_slice(&self) -> VolatileSlice
[src]
fn get_ref<T: ByteValued>(&self, offset: usize) -> Result<VolatileRef<T>>
[src]
fn get_array_ref<T: ByteValued>(
&self,
offset: usize,
n: usize
) -> Result<VolatileArrayRef<T>>
[src]
&self,
offset: usize,
n: usize
) -> Result<VolatileArrayRef<T>>
unsafe fn aligned_as_ref<T: ByteValued>(&self, offset: usize) -> Result<&T>
[src]
unsafe fn aligned_as_mut<T: ByteValued>(&self, offset: usize) -> Result<&mut T>
[src]
fn get_atomic_ref<T: AtomicValued>(&self, offset: usize) -> Result<&T>
[src]
fn compute_end_offset(&self, base: usize, offset: usize) -> Result<usize>
[src]
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,