twizzler_object/
object.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::{marker::PhantomData, sync::Arc};

use twizzler_abi::object::ObjID;

use crate::slot::Slot;

/// A handle for an object with base type T.
pub struct Object<T> {
    pub(crate) slot: Arc<Slot>,
    pub(crate) _pd: PhantomData<T>,
}

impl<T> Clone for Object<T> {
    fn clone(&self) -> Self {
        Self {
            slot: self.slot.clone(),
            _pd: self._pd,
        }
    }
}

impl<T> Object<T> {
    /// Get the ID of this object.
    pub fn id(&self) -> ObjID {
        self.slot.id()
    }

    /// Get the slot of this object.
    pub fn slot(&self) -> &Arc<Slot> {
        &self.slot
    }

    /// Transmute the object of base type T to base type N.
    ///
    /// # Safety
    /// All the safely rules of using [core::mem::transmute] apply to the base type.
    pub unsafe fn transmute<N>(self) -> Object<N> {
        core::mem::transmute(self)
    }
}

impl<Base> From<Arc<Slot>> for Object<Base> {
    fn from(s: Arc<Slot>) -> Self {
        Self {
            slot: s,
            _pd: PhantomData,
        }
    }
}