twizzler_driver/device/
info.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use twizzler_abi::device::SubObjectType;
use twizzler_object::{ObjID, Object, ObjectInitError, ObjectInitFlags, Protections};

use super::Device;

/// A handle to an info subobject.
pub struct InfoObject<T> {
    obj: Object<T>,
}

impl<T> InfoObject<T> {
    fn new(id: ObjID) -> Result<Self, ObjectInitError> {
        Ok(Self {
            obj: Object::init_id(id, Protections::READ, ObjectInitFlags::empty())?,
        })
    }

    /// Get a reference to the data contained within an info type subobject.
    pub fn get_data(&self) -> &T {
        unsafe { self.obj.base_unchecked() }
    }
}

impl Device {
    /// Get an indexed info object for a device.
    /// # Safety
    /// The type T is not verified in any way, so the caller must ensure that T is correct
    /// for the underlying data.
    pub unsafe fn get_info<T>(&self, idx: u8) -> Option<InfoObject<T>> {
        let id = self.get_subobj(SubObjectType::Info.into(), idx)?;
        InfoObject::new(id).ok()
    }
}