[][src]Trait vm_memory::bytes::Bytes

pub trait Bytes<A> {
    type E;
    fn write(&self, buf: &[u8], addr: A) -> Result<usize, Self::E>;
fn read(&self, buf: &mut [u8], addr: A) -> Result<usize, Self::E>;
fn write_slice(&self, buf: &[u8], addr: A) -> Result<(), Self::E>;
fn read_slice(&self, buf: &mut [u8], addr: A) -> Result<(), Self::E>;
fn read_from<F>(
        &self,
        addr: A,
        src: &mut F,
        count: usize
    ) -> Result<usize, Self::E>
    where
        F: Read
;
fn read_exact_from<F>(
        &self,
        addr: A,
        src: &mut F,
        count: usize
    ) -> Result<(), Self::E>
    where
        F: Read
;
fn write_to<F>(
        &self,
        addr: A,
        dst: &mut F,
        count: usize
    ) -> Result<usize, Self::E>
    where
        F: Write
;
fn write_all_to<F>(
        &self,
        addr: A,
        dst: &mut F,
        count: usize
    ) -> Result<(), Self::E>
    where
        F: Write
;
fn store<T: AtomicAccess>(
        &self,
        val: T,
        addr: A,
        order: Ordering
    ) -> Result<(), Self::E>;
fn load<T: AtomicAccess>(
        &self,
        addr: A,
        order: Ordering
    ) -> Result<T, Self::E>; fn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E> { ... }
fn read_obj<T: ByteValued>(&self, addr: A) -> Result<T, Self::E> { ... } }

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

Loading content...

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

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 from src into the container.
  • count - Copy count bytes from src into the container.

fn read_exact_from<F>(
    &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 from src into the container.
  • count - Copy exactly count bytes from src into the container.

fn write_to<F>(
    &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 to dst.
  • count - Copy count bytes from the container to dst.

fn write_all_to<F>(
    &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 to dst.
  • count - Copy exactly count bytes from the container to dst.

fn store<T: AtomicAccess>(
    &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.

Loading content...

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.

Loading content...

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]

Examples

  • Write a slice at offset 256.
    let res = vslice.write_slice(&[1,2,3,4,5], 256);

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]

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]

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]

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]

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(|_| ())?;

impl<T: GuestMemory> Bytes<GuestAddress> for T[src]

type E = Error

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]

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 write_to<F>(
    &self,
    addr: GuestAddress,
    dst: &mut F,
    count: usize
) -> Result<usize> where
    F: Write
[src]

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");
Loading content...