drt_sc/types/managed/
managed_type_trait.rs

1use crate::api::{HandleConstraints, ManagedTypeApi, RawHandle};
2
3use super::ManagedRef;
4
5/// Commonalities between all managed types.
6pub trait ManagedType<M: ManagedTypeApi>: Sized {
7    type OwnHandle: HandleConstraints;
8
9    #[doc(hidden)]
10    fn from_handle(handle: Self::OwnHandle) -> Self;
11
12    fn get_handle(&self) -> Self::OwnHandle;
13
14    #[doc(hidden)]
15    fn from_raw_handle(handle: RawHandle) -> Self {
16        Self::from_handle(Self::OwnHandle::new(handle))
17    }
18
19    fn get_raw_handle(&self) -> RawHandle {
20        self.get_handle().cast_or_signal_error::<M, _>()
21    }
22
23    /// Implement carefully, since the underlying transmutation is an unsafe operation.
24    /// For types that wrap a handle to some VM-managed data,
25    /// make sure the type only contains the handle (plus ZSTs if necessary).
26    /// For types that just wrap another managed type it is easier, call for the wrapped object.
27    fn transmute_from_handle_ref(handle_ref: &Self::OwnHandle) -> &Self;
28
29    fn as_ref(&self) -> ManagedRef<'_, M, Self> {
30        ManagedRef::new(self)
31    }
32}