Skip to main content

Device

Trait Device 

Source
pub trait Device:
    Send
    + Sync
    + Any {
    // Required methods
    fn name(&self) -> &str;
    fn resources(&self) -> &[Resource];
    fn handle(&self, access: &BusAccess) -> Result<BusResponse, DeviceError>;
    fn as_any(&self) -> &dyn Any;

    // Provided methods
    fn reset(&mut self) -> Result<(), DeviceError> { ... }
    fn suspend(&mut self) -> Result<(), DeviceError> { ... }
    fn resume(&mut self) -> Result<(), DeviceError> { ... }
}
Expand description

The unified device trait.

Every emulated device (interrupt controller, UART, virtio-blk, …) implements this trait. The device manager calls resources at registration time for conflict detection and handle on the hot path whenever a vCPU exit is dispatched to this device.

§Downcasting

Device extends Any so callers can downcast to a concrete device type via as_any:

if let Some(vgic) = device.as_any().downcast_ref::<VGicD>() {
    vgic.assign_irq(32, cpu_id, (0, 0, 0, cpu_id));
}

Required Methods§

Source

fn name(&self) -> &str

Returns a human-readable name for this device (used in logging and diagnostics).

Source

fn resources(&self) -> &[Resource]

Returns the resources (MMIO windows, port ranges, system registers) this device requires.

The returned slice is a stable snapshot computed at device construction time. Callers may read it on both the registration path and the hot path without allocation.

Source

fn handle(&self, access: &BusAccess) -> Result<BusResponse, DeviceError>

Handles a single bus access.

This is the hot-path entry point called from BusRouter::dispatch.

Source

fn as_any(&self) -> &dyn Any

Returns a reference to self as &dyn Any for downcasting.

Provided Methods§

Source

fn reset(&mut self) -> Result<(), DeviceError>

Resets the device to its power-on state.

Source

fn suspend(&mut self) -> Result<(), DeviceError>

Puts the device into a low-power or suspended state.

Source

fn resume(&mut self) -> Result<(), DeviceError>

Restores the device from a suspended state.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§