pub struct VolatileSlice<'a, B = ()> { /* private fields */ }
Expand description

A slice of raw memory that supports volatile access.

Implementations

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.

Creates a slice of raw memory that must support volatile access, and uses the provided bitmap object for dirty page tracking.

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.

Returns a pointer to the beginning of the slice. Mutable accesses performed using the resulting pointer are not automatically accounted for by the dirty bitmap tracking functionality.

Gets the size of this slice.

Checks if the slice is empty.

Borrows the inner BitmapSlice.

Divides one slice into two at an index.

Example
let vslice = mem_ref
    .get_slice(0, 32)
    .expect("Could not get VolatileSlice");

let (start, end) = vslice.split_at(8).expect("Could not split VolatileSlice");
assert_eq!(8, start.len());
assert_eq!(24, end.len());

Returns a subslice of this VolatileSlice starting at offset with count length.

The returned subslice is a copy of this slice with the address increased by offset bytes and the size set to count bytes.

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.

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)
    .expect("Could not get VolatileSlice");
let mut buf = [5u8; 16];
let res = vslice.copy_to(&mut buf[..]);

assert_eq!(16, res);
for &v in &buf[..] {
    assert_eq!(v, 0);
}

Copies as many bytes as possible from this slice to the provided slice.

The copies happen in an undefined order.

Examples
vslice.copy_to_volatile_slice(
    vslice
        .get_slice(16, 16)
        .expect("Could not get VolatileSlice"),
);

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)
    .expect("Could not get VolatileSlice");

let buf = [5u8; 64];
vslice.copy_from(&buf[..]);

for i in 0..4 {
    let val = vslice
        .get_ref::<u32>(i * 4)
        .expect("Could not get value")
        .load();
    assert_eq!(val, 0x05050505);
}

Trait Implementations

Examples
  • Write a slice of size 5 at offset 1020 of a 1024-byte VolatileSlice.
let mut mem = [0u8; 1024];
let mut mem_ref = &mut mem[..];
let vslice = mem_ref.as_volatile_slice();
let res = vslice.write(&[1, 2, 3, 4, 5], 1020);

assert!(res.is_ok());
assert_eq!(res.unwrap(), 4);
Examples
  • Read a slice of size 16 at offset 1010 of a 1024-byte VolatileSlice.
let mut mem = [0u8; 1024];
let mut mem_ref = &mut mem[..];
let vslice = mem_ref.as_volatile_slice();
let buf = &mut [0u8; 16];
let res = vslice.read(buf, 1010);

assert!(res.is_ok());
assert_eq!(res.unwrap(), 14);
Examples
  • Write a slice at offset 256.
let res = vslice.write_slice(&[1, 2, 3, 4, 5], 256);

assert!(res.is_ok());
assert_eq!(res.unwrap(), ());
Examples
  • Read a slice of size 16 at offset 256.
let buf = &mut [0u8; 16];
let res = vslice.read_slice(buf, 256);

assert!(res.is_ok());
Examples
  • Read bytes from /dev/urandom
let mut file = File::open(Path::new("/dev/urandom")).expect("Could not open /dev/urandom");

vslice
    .read_from(32, &mut file, 128)
    .expect("Could not read bytes from file into VolatileSlice");

let rand_val: u32 = vslice
    .read_obj(40)
    .expect("Could not read value from VolatileSlice");
Examples
  • Read bytes from /dev/urandom
let mut file = File::open(Path::new("/dev/urandom")).expect("Could not open /dev/urandom");

vslice
    .read_exact_from(32, &mut file, 128)
    .expect("Could not read bytes from file into VolatileSlice");

let rand_val: u32 = vslice
    .read_obj(40)
    .expect("Could not read value from VolatileSlice");
Examples
  • Write 128 bytes to /dev/null
let mut file = OpenOptions::new()
    .write(true)
    .open("/dev/null")
    .expect("Could not open /dev/null");

vslice
    .write_to(32, &mut file, 128)
    .expect("Could not write value from VolatileSlice to /dev/null");
Examples
  • Write 128 bytes to /dev/null
let mut file = OpenOptions::new()
    .write(true)
    .open("/dev/null")
    .expect("Could not open /dev/null");

vslice
    .write_all_to(32, &mut file, 128)
    .expect("Could not write value from VolatileSlice to /dev/null");

Associated error codes

Atomically store a value at the specified address.

Atomically load a value from the specified address.

Writes an object into the container at addr. Read more

Reads an object from the container at addr. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Type used for dirty memory tracking.

Gets the size of this slice.

Returns a VolatileSlice of count bytes starting at offset. Read more

Check whether the region is empty.

Gets a slice of memory for the entire region that supports volatile access.

Gets a VolatileRef at offset.

Returns a VolatileArrayRef of n elements starting at offset. Read more

Returns a reference to an instance of T at offset. Read more

Returns a mutable reference to an instance of T at offset. Mutable accesses performed using the resulting reference are not automatically accounted for by the dirty bitmap tracking functionality. Read more

Returns a reference to an instance of T at offset. Mutable accesses performed using the resulting reference are not automatically accounted for by the dirty bitmap tracking functionality. Read more

Returns the sum of base and offset if the resulting address is valid.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.