[−][src]Trait vm_memory::bytes::Bytes
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
type E
Associated error codes
Required methods
fn write(&self, buf: &[u8], addr: A) -> Result<usize, Self::E>
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>
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>
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>
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,
&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,
&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,
&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,
&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>
&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>
Atomically load a value from the specified address.
Provided methods
fn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E>
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>
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 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 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.
let start_addr = GuestAddress(0x1000); let mut gm = GuestMemoryMmap::from_ranges(&vec![(start_addr, 0x400)]) .expect("Could not create guest memory"); let res = gm.write_slice(&[1, 2, 3, 4, 5], start_addr); assert!(res.is_ok());
fn read_slice(&self, buf: &mut [u8], addr: GuestAddress) -> Result<()>
[src]
Examples
- Read a slice of length 16 at guestaddress 0x1000.
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]; let res = gm.read_slice(buf, start_addr); assert!(res.is_ok());
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
let start_addr = GuestAddress(0x1000); let gm = GuestMemoryMmap::from_ranges(&vec![(start_addr, 0x400)]) .expect("Could not create guest memory"); let mut file = File::open(Path::new("/dev/urandom")) .expect("could not open /dev/urandom"); let addr = GuestAddress(0x1010); 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
let start_addr = GuestAddress(0x1000); let gm = GuestMemoryMmap::from_ranges(&vec![(start_addr, 1024)]) .expect("Could not create guest memory"); 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,
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>