Struct Union2

Source
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>

Source

pub fn new_a(a: A) -> Result<Self, A>

Construct a varaint of this union with a dynamic alignment check.

Source

pub fn is_a(&self) -> bool

Check if the union is this variant.

Source

pub fn into_a(self) -> Result<A, Self>

Extract this variant from the union.

Returns the union on error.

Source

pub fn with_a<R>(&self, f: impl FnOnce(&A) -> R) -> Option<R>

Run a closure with this variant.

Source

pub fn a(&self) -> Option<&A::Target>
where A: Deref,

Get a reference to this variant’s target.

Source

pub fn clone_a(&self) -> Option<A>
where A: Clone,

Clone this variant out of the union.

Source

pub fn copy_a(&self) -> Option<A>
where A: Copy,

Copy this variant out of the union.

Source

pub fn new_b(b: B) -> Result<Self, B>

Construct a varaint of this union with a dynamic alignment check.

Source

pub fn is_b(&self) -> bool

Check if the union is this variant.

Source

pub fn into_b(self) -> Result<B, Self>

Extract this variant from the union.

Returns the union on error.

Source

pub fn with_b<R>(&self, f: impl FnOnce(&B) -> R) -> Option<R>

Run a closure with this variant.

Source

pub fn b(&self) -> Option<&B::Target>
where B: Deref,

Get a reference to this variant’s target.

Source

pub fn clone_b(&self) -> Option<B>
where B: Clone,

Clone this variant out of the union.

Source

pub fn copy_b(&self) -> Option<B>
where B: Copy,

Copy this variant out of the union.

Source

pub fn unpack(self) -> Enum2<A, B>

Unpack this union into an enum.

Source

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).

Source

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.

Source

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.

Source

pub fn as_untagged_ptr(&self) -> ErasedPtr

Get the raw type-erased untagged pointer to the payload.

Source

pub fn try_deref<'a>(&'a self) -> Option<Union2<&'a A::Target, &'a B::Target>>
where A: Deref, B: Deref, &'a A::Target: ErasablePtr, &'a B::Target: ErasablePtr,

Dereference the current pointer.

Performs a dynamic alignment check on the dereferenced pointer.

Trait Implementations§

Source§

impl<A, B> Clone for Union2<A, B>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A, B> Debug for Union2<A, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: ErasablePtr, B: ErasablePtr> Drop for Union2<A, B>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<A: ErasablePtr, B: ErasablePtr> ErasablePtr for Union2<A, B>

Source§

fn erase(this: Self) -> ErasedPtr

Turn this erasable pointer into an erased pointer. Read more
Source§

unsafe fn unerase(this: ErasedPtr) -> Self

Unerase this erased pointer. Read more
Source§

impl<A, B> Hash for Union2<A, B>
where A: Hash + ErasablePtr, B: Hash + ErasablePtr,

Source§

fn hash<Hasher>(&self, state: &mut Hasher)
where Hasher: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A, B> PartialEq for Union2<A, B>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, B> Eq for Union2<A, B>
where A: Eq + ErasablePtr, B: Eq + ErasablePtr,

Source§

impl<A, B> Send for Union2<A, B>
where A: Send + ErasablePtr, B: Send + ErasablePtr,

Source§

impl<A, B> Sync for Union2<A, B>
where A: Sync + ErasablePtr, B: Sync + ErasablePtr,

Auto Trait Implementations§

§

impl<A, B> Freeze for Union2<A, B>

§

impl<A, B> RefUnwindSafe for Union2<A, B>

§

impl<A, B> Unpin for Union2<A, B>
where A: Unpin, B: Unpin,

§

impl<A, B> UnwindSafe for Union2<A, B>
where A: UnwindSafe, B: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Erasable for T

Source§

const ACK_1_1_0: bool = true

Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more
Source§

unsafe fn unerase(this: NonNull<Erased>) -> NonNull<T>

Unerase this erased pointer. Read more
Source§

fn erase(this: NonNull<Self>) -> NonNull<Erased>

Turn this erasable pointer into an erased pointer. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.