pub unsafe trait OtCastable: Sized {
type OtType: Sized;
// Required methods
fn as_ot_ptr(&self) -> *const Self::OtType;
fn as_ot_mut_ptr(&mut self) -> *mut Self::OtType;
unsafe fn ref_from_ot_ptr<'a>(ptr: *const Self::OtType) -> Option<&'a Self>;
unsafe fn mut_from_ot_mut_ptr<'a>(
ptr: *mut Self::OtType,
) -> Option<&'a mut Self>;
// Provided methods
fn as_ot_ref(&self) -> &Self::OtType { ... }
fn as_ot_mut(&mut self) -> &mut Self::OtType { ... }
fn ref_from_ot_ref(x: &Self::OtType) -> &Self { ... }
}
Expand description
Trait used to indicate that the implementing type can be efficiently
converted into a reference to the original OpenThread type identified by
Self::OtType
.
Types which implement this trait may be opaque, but, unlike [Boxable
], these
types are not necessarily opaque and not necessarily ownable. If a type is
not opaque then it will also implement the related trait Transparent
,
allowing it to be used by value.
§Safety
This trait is unsafe because it is making an assertion about the data’s representation that must be verified by code review.
In order to safely implement this trait you must verify that the implementing
type is guaranteed to always be fundamentally identical in its in-memory
representation to <Self as OtCastable>::OtType
.
For example, this would be the case if Self
is #[repr(transparent)]
and
has only a single member of <Self as OtCastable>::OtType
.
Required Associated Types§
Required Methods§
Sourcefn as_ot_ptr(&self) -> *const Self::OtType
fn as_ot_ptr(&self) -> *const Self::OtType
Returns a pointer to the underlying Self::OtType
instance.
Sourcefn as_ot_mut_ptr(&mut self) -> *mut Self::OtType
fn as_ot_mut_ptr(&mut self) -> *mut Self::OtType
Returns a mutable pointer to the underlying Self::OtType
instance.
Sourceunsafe fn ref_from_ot_ptr<'a>(ptr: *const Self::OtType) -> Option<&'a Self>
unsafe fn ref_from_ot_ptr<'a>(ptr: *const Self::OtType) -> Option<&'a Self>
Creates a reference from a pointer to an Self::OtType
.
§Safety
This method is unsafe because unchecked conversion of pointers to references is generally unsafe. The following assumptions need to be verified to avoid undefined behavior:
ptr
MUST point to a valid instance ofSelf::OtType
that will remain valid over the given lifetime.
Sourceunsafe fn mut_from_ot_mut_ptr<'a>(
ptr: *mut Self::OtType,
) -> Option<&'a mut Self>
unsafe fn mut_from_ot_mut_ptr<'a>( ptr: *mut Self::OtType, ) -> Option<&'a mut Self>
Creates a mut reference from a mut pointer to an Self::OtType
.
§Safety
This method is unsafe because unchecked conversion of pointers to references is generally unsafe. The following assumptions need to be verified to avoid undefined behavior:
ptr
MUST point to a valid instance ofSelf::OtType
that will remain valid over the given lifetime.
Provided Methods§
Sourcefn as_ot_ref(&self) -> &Self::OtType
fn as_ot_ref(&self) -> &Self::OtType
Returns a reference to the original OpenThread type Self::OtType
.
Sourcefn as_ot_mut(&mut self) -> &mut Self::OtType
fn as_ot_mut(&mut self) -> &mut Self::OtType
Returns a mutable reference to the original OpenThread type Self::OtType
.
Sourcefn ref_from_ot_ref(x: &Self::OtType) -> &Self
fn ref_from_ot_ref(x: &Self::OtType) -> &Self
Casts a reference to the original OpenThread type to a reference to Self
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.