Trait vm_memory::bytes::Bytes [−][src]
A container to host a range of bytes and access its content.
Candidates which may implement this trait include:
- anonymous memory areas
- mmapped memory areas
- data files
- a proxy to access memory on remote
Associated Types
Loading content...Required methods
fn write(&self, buf: &[u8], addr: A) -> Result<usize, Self::E>
[src]
Writes a slice into the container at addr
.
Returns the number of bytes written. The number of bytes written can be less than the length of the slice if there isn't enough room in the container.
fn read(&self, buf: &mut [u8], addr: A) -> Result<usize, Self::E>
[src]
Reads data from the container at addr
into a slice.
Returns the number of bytes read. The number of bytes read can be less than the length of the slice if there isn't enough data within the container.
fn write_slice(&self, buf: &[u8], addr: A) -> Result<(), Self::E>
[src]
Writes the entire content of a slice into the container at addr
.
Errors
Returns an error if there isn't enough space within the container to write the entire slice. Part of the data may have been copied nevertheless.
fn read_slice(&self, buf: &mut [u8], addr: A) -> Result<(), Self::E>
[src]
Reads data from the container at addr
to fill an entire slice.
Errors
Returns an error if there isn't enough data within the container to fill the entire slice. Part of the data may have been copied nevertheless.
fn read_from<F>(
&self,
addr: A,
src: &mut F,
count: usize
) -> Result<usize, Self::E> where
F: Read,
[src]
&self,
addr: A,
src: &mut F,
count: usize
) -> Result<usize, Self::E> where
F: Read,
Reads up to count
bytes from an object and writes them into the container at addr
.
Returns the number of bytes written into the container.
Arguments
addr
- Begin writing at this address.src
- Copy fromsrc
into the container.count
- Copycount
bytes fromsrc
into the container.
fn read_exact_from<F>(
&self,
addr: A,
src: &mut F,
count: usize
) -> Result<(), Self::E> where
F: Read,
[src]
&self,
addr: A,
src: &mut F,
count: usize
) -> Result<(), Self::E> where
F: Read,
Reads exactly count
bytes from an object and writes them into the container at addr
.
Errors
Returns an error if count
bytes couldn't have been copied from src
to the container.
Part of the data may have been copied nevertheless.
Arguments
addr
- Begin writing at this address.src
- Copy fromsrc
into the container.count
- Copy exactlycount
bytes fromsrc
into the container.
fn write_to<F>(
&self,
addr: A,
dst: &mut F,
count: usize
) -> Result<usize, Self::E> where
F: Write,
[src]
&self,
addr: A,
dst: &mut F,
count: usize
) -> Result<usize, Self::E> where
F: Write,
Reads up to count
bytes from the container at addr
and writes them it into an object.
Returns the number of bytes written into the object.
Arguments
addr
- Begin reading from this address.dst
- Copy from the container todst
.count
- Copycount
bytes from the container todst
.
fn write_all_to<F>(
&self,
addr: A,
dst: &mut F,
count: usize
) -> Result<(), Self::E> where
F: Write,
[src]
&self,
addr: A,
dst: &mut F,
count: usize
) -> Result<(), Self::E> where
F: Write,
Reads exactly count
bytes from the container at addr
and writes them into an object.
Errors
Returns an error if count
bytes couldn't have been copied from the container to dst
.
Part of the data may have been copied nevertheless.
Arguments
addr
- Begin reading from this address.dst
- Copy from the container todst
.count
- Copy exactlycount
bytes from the container todst
.
fn store<T: AtomicAccess>(
&self,
val: T,
addr: A,
order: Ordering
) -> Result<(), Self::E>
[src]
&self,
val: T,
addr: A,
order: Ordering
) -> Result<(), Self::E>
Atomically store a value at the specified address.
fn load<T: AtomicAccess>(&self, addr: A, order: Ordering) -> Result<T, Self::E>
[src]
Atomically load a value from the specified address.
Provided methods
fn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E>
[src]
Writes an object into the container at addr
.
Errors
Returns an error if the object doesn't fit inside the container.
fn read_obj<T: ByteValued>(&self, addr: A) -> Result<T, Self::E>
[src]
Reads an object from the container at addr
.
Reading from a volatile area isn't strictly safe as it could change mid-read. However, as long as the type T is plain old data and can handle random initialization, everything will be OK.
Errors
Returns an error if there's not enough data inside the container.
Implementors
impl Bytes<usize> for VolatileSlice<'_>
[src]
type E = Error
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 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);
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 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);
fn write_slice(&self, buf: &[u8], addr: usize) -> Result<()>
[src]
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(), ());
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); assert!(res.is_ok());
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")).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");
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")).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");
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 = 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");
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 = 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");
fn store<T: AtomicAccess>(
&self,
val: T,
addr: usize,
order: Ordering
) -> Result<()>
[src]
&self,
val: T,
addr: usize,
order: Ordering
) -> Result<()>
fn load<T: AtomicAccess>(&self, addr: usize, order: Ordering) -> Result<T>
[src]
impl<T: GuestMemory> Bytes<GuestAddress> for T
[src]
type E = Error
fn write(&self, buf: &[u8], addr: GuestAddress) -> Result<usize>
[src]
fn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize>
[src]
fn write_slice(&self, buf: &[u8], addr: GuestAddress) -> Result<()>
[src]
Examples
- Write a slice at guestaddress 0x1000. (uses the
backend-mmap
feature)
gm.write_slice(&[1, 2, 3, 4, 5], start_addr) .expect("Could not write slice to guest memory");
fn read_slice(&self, buf: &mut [u8], addr: GuestAddress) -> Result<()>
[src]
Examples
- Read a slice of length 16 at guestaddress 0x1000. (uses the
backend-mmap
feature)
let start_addr = GuestAddress(0x1000); let mut gm = GuestMemoryMmap::from_ranges(&vec![(start_addr, 0x400)]) .expect("Could not create guest memory"); let buf = &mut [0u8; 16]; gm.read_slice(buf, start_addr) .expect("Could not read slice from guest memory");
fn read_from<F>(
&self,
addr: GuestAddress,
src: &mut F,
count: usize
) -> Result<usize> where
F: Read,
[src]
&self,
addr: GuestAddress,
src: &mut F,
count: usize
) -> Result<usize> where
F: Read,
Examples
- Read bytes from /dev/urandom (uses the
backend-mmap
feature)
let mut file = File::open(Path::new("/dev/urandom")).expect("Could not open /dev/urandom"); gm.read_from(addr, &mut file, 128) .expect("Could not read from /dev/urandom into guest memory"); let read_addr = addr.checked_add(8).expect("Could not compute read address"); let rand_val: u32 = gm .read_obj(read_addr) .expect("Could not read u32 val from /dev/urandom");
fn read_exact_from<F>(
&self,
addr: GuestAddress,
src: &mut F,
count: usize
) -> Result<()> where
F: Read,
[src]
&self,
addr: GuestAddress,
src: &mut F,
count: usize
) -> Result<()> where
F: Read,
fn write_to<F>(
&self,
addr: GuestAddress,
dst: &mut F,
count: usize
) -> Result<usize> where
F: Write,
[src]
&self,
addr: GuestAddress,
dst: &mut F,
count: usize
) -> Result<usize> where
F: Write,
Examples
- Write 128 bytes to /dev/null (uses the
backend-mmap
feature)
let mut file = OpenOptions::new() .write(true) .open("/dev/null") .expect("Could not open /dev/null"); gm.write_to(start_addr, &mut file, 128) .expect("Could not write 128 bytes to the provided address");
fn write_all_to<F>(
&self,
addr: GuestAddress,
dst: &mut F,
count: usize
) -> Result<()> where
F: Write,
[src]
&self,
addr: GuestAddress,
dst: &mut F,
count: usize
) -> Result<()> where
F: Write,
Examples
- Write 128 bytes to /dev/null (uses the
backend-mmap
feature)
let mut file = OpenOptions::new() .write(true) .open("/dev/null") .expect("Could not open /dev/null"); gm.write_all_to(start_addr, &mut file, 128) .expect("Could not write 128 bytes to the provided address");
fn store<O: AtomicAccess>(
&self,
val: O,
addr: GuestAddress,
order: Ordering
) -> Result<()>
[src]
&self,
val: O,
addr: GuestAddress,
order: Ordering
) -> Result<()>
fn load<O: AtomicAccess>(
&self,
addr: GuestAddress,
order: Ordering
) -> Result<O>
[src]
&self,
addr: GuestAddress,
order: Ordering
) -> Result<O>