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§
Required Methods§
sourcefn write(&self, buf: &[u8], addr: A) -> Result<usize, Self::E>
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.
sourcefn read(&self, buf: &mut [u8], addr: A) -> Result<usize, Self::E>
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.
sourcefn write_slice(&self, buf: &[u8], addr: A) -> Result<(), Self::E>
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.
sourcefn read_slice(&self, buf: &mut [u8], addr: A) -> Result<(), Self::E>
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.
sourcefn read_from<F>(
&self,
addr: A,
src: &mut F,
count: usize,
) -> Result<usize, Self::E>where
F: Read,
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 fromsrc
into the container.count
- Copycount
bytes fromsrc
into the container.
sourcefn read_exact_from<F>(
&self,
addr: A,
src: &mut F,
count: usize,
) -> Result<(), Self::E>where
F: Read,
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 fromsrc
into the container.count
- Copy exactlycount
bytes fromsrc
into the container.
sourcefn write_to<F>(
&self,
addr: A,
dst: &mut F,
count: usize,
) -> Result<usize, Self::E>where
F: Write,
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 todst
.count
- Copycount
bytes from the container todst
.
sourcefn write_all_to<F>(
&self,
addr: A,
dst: &mut F,
count: usize,
) -> Result<(), Self::E>where
F: Write,
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 todst
.count
- Copy exactlycount
bytes from the container todst
.
Provided Methods§
sourcefn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E>
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.
sourcefn read_obj<T: ByteValued>(&self, addr: A) -> Result<T, Self::E>
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.