Trait vm_memory::bytes::Bytes

source ·
pub trait Bytes<A> {
    type E;

    // Required methods
    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>;

    // Provided methods
    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> { ... }
}
Expand description

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

Required Associated Types§

source

type E

Associated error codes

Required Methods§

source

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.

source

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.

source

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.

source

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.

source

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.
source

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.
source

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.
source

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.
source

fn store<T: AtomicAccess>( &self, val: T, addr: A, order: Ordering ) -> Result<(), Self::E>

Atomically store a value at the specified address.

source

fn load<T: AtomicAccess>(&self, addr: A, order: Ordering) -> Result<T, Self::E>

Atomically load a value from the specified address.

Provided Methods§

source

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.

source

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.

Object Safety§

This trait is not object safe.

Implementors§