GuestAddressSpace

Trait GuestAddressSpace 

Source
pub trait GuestAddressSpace: Clone {
    type M: GuestMemory;
    type T: Clone + Deref<Target = Self::M>;

    // Required method
    fn memory(&self) -> Self::T;
}
Expand description

GuestAddressSpace provides a way to retrieve a GuestMemoryBackend object. The vm-memory crate already provides trivial implementation for references to GuestMemoryBackend or reference-counted GuestMemoryBackend objects, but the trait can also be implemented by any other struct in order to provide temporary access to a snapshot of the memory map.

In order to support generic mutable memory maps, devices (or other things that access memory) should store the memory as a GuestAddressSpace<M>. This example shows that references can also be used as the GuestAddressSpace implementation, providing a zero-cost abstraction whenever immutable memory maps are sufficient.

§Examples (uses the backend-mmap and backend-atomic features)

pub struct VirtioDevice<AS: GuestAddressSpace> {
    mem: Option<AS>,
}

impl<AS: GuestAddressSpace> VirtioDevice<AS> {
    fn new() -> Self {
        VirtioDevice { mem: None }
    }
    fn activate(&mut self, mem: AS) {
        self.mem = Some(mem)
    }
}

fn get_mmap() -> GuestMemoryMmap<()> {
    let start_addr = GuestAddress(0x1000);
    GuestMemoryMmap::from_ranges(&vec![(start_addr, 0x400)])
        .expect("Could not create guest memory")
}

// Using `VirtioDevice` with an immutable GuestMemoryMmap:
let mut for_immutable_mmap = VirtioDevice::<&GuestMemoryMmap<()>>::new();
let mmap = get_mmap();
for_immutable_mmap.activate(&mmap);
let mut another = VirtioDevice::<&GuestMemoryMmap<()>>::new();
another.activate(&mmap);

// Using `VirtioDevice` with a mutable GuestMemoryMmap:
let mut for_mutable_mmap = VirtioDevice::<GuestMemoryAtomic<GuestMemoryMmap<()>>>::new();
let atomic = GuestMemoryAtomic::new(get_mmap());
for_mutable_mmap.activate(atomic.clone());
let mut another = VirtioDevice::<GuestMemoryAtomic<GuestMemoryMmap<()>>>::new();
another.activate(atomic.clone());

// atomic can be modified here...

Required Associated Types§

Source

type M: GuestMemory

The type that will be used to access guest memory.

Source

type T: Clone + Deref<Target = Self::M>

A type that provides access to the memory.

Required Methods§

Source

fn memory(&self) -> Self::T

Return an object (e.g. a reference or guard) that can be used to access memory through this address space. The object provides a consistent snapshot of the memory map.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<M: GuestMemory> GuestAddressSpace for &M

Source§

type M = M

Source§

type T = &M

Source§

fn memory(&self) -> Self

Source§

impl<M: GuestMemory> GuestAddressSpace for Rc<M>

Source§

type M = M

Source§

type T = Rc<M>

Source§

fn memory(&self) -> Self

Source§

impl<M: GuestMemory> GuestAddressSpace for Arc<M>

Source§

type M = M

Source§

type T = Arc<M>

Source§

fn memory(&self) -> Self

Implementors§

Source§

impl<M: GuestMemory> GuestAddressSpace for GuestMemoryAtomic<M>

Available on crate feature backend-atomic only.