pub unsafe trait MemOps {
type Error;
// Required methods
fn read(&self, addr: u64, dst: &mut [u8]) -> Result<(), Self::Error>;
fn write(&self, addr: u64, src: &[u8]) -> Result<(), Self::Error>;
fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>;
fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>;
unsafe fn as_slice(
&self,
addr: u64,
len: usize,
) -> Result<&[u8], Self::Error>;
unsafe fn as_mut_slice(
&self,
addr: u64,
len: usize,
) -> Result<&mut [u8], Self::Error>;
// Provided methods
fn read_val<T: Pod>(&self, addr: u64) -> Result<T, Self::Error> { ... }
fn write_val<T: Pod>(&self, addr: u64, val: T) -> Result<(), Self::Error> { ... }
}Expand description
Backend-provided memory access for virtqueue.
§Safety
Implementations must ensure that:
- Addresses accepted by these methods are translated according to the backend’s memory model.
- Invalid or inaccessible addresses are reported with
Self::Errorrather than causing undefined behavior. - Memory ordering guarantees are upheld as documented.
- Typed reads/writes and atomic operations honor alignment and initialized memory requirements for the translated addresses.
Required Associated Types§
Required Methods§
Sourcefn read(&self, addr: u64, dst: &mut [u8]) -> Result<(), Self::Error>
fn read(&self, addr: u64, dst: &mut [u8]) -> Result<(), Self::Error>
Read bytes from physical memory.
Used for reading buffer contents pointed to by descriptors.
§Arguments
addr- Guest physical address to read fromdst- Destination buffer to fill
Implementations must return an error if addr cannot be read for
at least dst.len() bytes.
Sourcefn write(&self, addr: u64, src: &[u8]) -> Result<(), Self::Error>
fn write(&self, addr: u64, src: &[u8]) -> Result<(), Self::Error>
Write bytes to physical memory.
§Arguments
addr- address to write tosrc- Source data to write
Implementations must return an error if addr cannot be written for
at least src.len() bytes.
Sourcefn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>
fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>
Load a u16 with acquire semantics.
Implementations must return an error if addr does not translate to a
valid, aligned AtomicU16 in shared memory.
Sourcefn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>
fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>
Store a u16 with release semantics.
Implementations must return an error if addr does not translate to a
valid, aligned AtomicU16 in shared memory.
Sourceunsafe fn as_slice(&self, addr: u64, len: usize) -> Result<&[u8], Self::Error>
unsafe fn as_slice(&self, addr: u64, len: usize) -> Result<&[u8], Self::Error>
Get a direct read-only slice into shared memory.
§Safety
The caller must ensure:
addris valid and points to at leastlenbytes.- The memory region is not concurrently modified for the lifetime of the returned slice. Caller must uphold this via protocol-level synchronisation, e.g. descriptor ownership transfer.
See also [BufferOwner]: super::BufferOwner
Sourceunsafe fn as_mut_slice(
&self,
addr: u64,
len: usize,
) -> Result<&mut [u8], Self::Error>
unsafe fn as_mut_slice( &self, addr: u64, len: usize, ) -> Result<&mut [u8], Self::Error>
Get a direct mutable slice into shared memory.
§Safety
The caller must ensure:
addris valid and points to at leastlenbytes.- No other references (shared or mutable) to this memory region exist for the lifetime of the returned slice.
- Protocol-level synchronisation (e.g. descriptor ownership) guarantees exclusive access.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".