twizzler_driver/device/
info.rs

1use twizzler::object::{ObjID, Object, RawObject};
2use twizzler_abi::device::SubObjectType;
3use twizzler_rt_abi::{object::MapFlags, Result};
4
5use super::Device;
6
7/// A handle to an info subobject.
8pub struct InfoObject<T> {
9    obj: Object<T>,
10}
11
12impl<T> InfoObject<T> {
13    fn new(id: ObjID) -> Result<Self> {
14        Ok(Self {
15            obj: unsafe { Object::map_unchecked(id, MapFlags::READ | MapFlags::WRITE) }?,
16        })
17    }
18
19    /// Get a reference to the data contained within an info type subobject.
20    pub fn get_data(&self) -> &T {
21        unsafe { self.obj.base_ptr::<T>().as_ref().unwrap() }
22    }
23}
24
25impl Device {
26    /// Get an indexed info object for a device.
27    /// # Safety
28    /// The type T is not verified in any way, so the caller must ensure that T is correct
29    /// for the underlying data.
30    pub unsafe fn get_info<T>(&self, idx: u8) -> Option<InfoObject<T>> {
31        let id = self.get_subobj(SubObjectType::Info.into(), idx)?;
32        InfoObject::new(id).ok()
33    }
34}