pub struct Union2<A: ErasablePtr, B: ErasablePtr> { /* private fields */ }
Expand description
A pointer union of two pointer types.
This is a tagged union of two pointer types such as Box
, Arc
, or &
that is only as big as a single pointer. This is accomplished by storing
the tag in the alignment bits of the pointer.
As such, the pointer must be aligned to at least u16
(align(2)
).
This is enforced through the use of Builder2
.
Implementations§
source§impl<A: ErasablePtr, B: ErasablePtr> Union2<A, B>
impl<A: ErasablePtr, B: ErasablePtr> Union2<A, B>
sourcepub fn new_a(a: A) -> Result<Self, A>
pub fn new_a(a: A) -> Result<Self, A>
Construct a varaint of this union with a dynamic alignment check.
sourcepub fn into_a(self) -> Result<A, Self>
pub fn into_a(self) -> Result<A, Self>
Extract this variant from the union.
Returns the union on error.
sourcepub fn new_b(b: B) -> Result<Self, B>
pub fn new_b(b: B) -> Result<Self, B>
Construct a varaint of this union with a dynamic alignment check.
sourcepub fn into_b(self) -> Result<B, Self>
pub fn into_b(self) -> Result<B, Self>
Extract this variant from the union.
Returns the union on error.
sourcepub fn ptr_eq(&self, other: &Self) -> bool
pub fn ptr_eq(&self, other: &Self) -> bool
Check if two unions are the same variant and point to the same value (not that the values compare as equal).
sourcepub fn as_deref<'a>(
&'a self,
builder: Builder2<&'a A::Target, &'a B::Target>
) -> Union2<&'a A::Target, &'a B::Target>where
A: Deref,
B: Deref,
&'a A::Target: ErasablePtr,
&'a B::Target: ErasablePtr,
pub fn as_deref<'a>(
&'a self,
builder: Builder2<&'a A::Target, &'a B::Target>
) -> Union2<&'a A::Target, &'a B::Target>where
A: Deref,
B: Deref,
&'a A::Target: ErasablePtr,
&'a B::Target: ErasablePtr,
Dereference the current pointer.
sourcepub unsafe fn as_deref_unchecked<'a>(
&'a self
) -> Union2<&'a A::Target, &'a B::Target>where
A: Deref,
B: Deref,
&'a A::Target: ErasablePtr,
&'a B::Target: ErasablePtr,
pub unsafe fn as_deref_unchecked<'a>(
&'a self
) -> Union2<&'a A::Target, &'a B::Target>where
A: Deref,
B: Deref,
&'a A::Target: ErasablePtr,
&'a B::Target: ErasablePtr,
Dereference the current pointer.
Safety
The reference produced must be properly aligned. Note that only the actually produced reference is restricted, not the result of dereferencing any of the other types in this union.
sourcepub fn as_untagged_ptr(&self) -> ErasedPtr
pub fn as_untagged_ptr(&self) -> ErasedPtr
Get the raw type-erased untagged pointer to the payload.