Skip to main content

CompactOption

Struct CompactOption 

Source
pub struct CompactOption<R, T: CompactRepr<R>> { /* private fields */ }
Expand description

Niche-packing optional: stores either Self::NONE or a Some(T) payload in exactly as much memory as raw R. T must be Copy (via the CompactRepr contract); the wrapper itself is Copy whenever R and T are.

§Layout checks

R and T must have identical size and alignment. The same layout assertions run when evaluating Self::NONE in a const context and when calling Self::some (so some cannot silently skip layout validation). A plain let _ = Self::NONE in non-const code may not const-evaluate Self::NONE; prefer const { CompactOption::<R, T>::NONE } or similar if you need the check guaranteed at compile time.

use compact_option::{CompactOption, CompactRepr};

#[derive(Clone, Copy)]
#[repr(C)]
struct Pair(u8, u8);

unsafe impl CompactRepr<u8> for Pair {
    const UNUSED_SENTINEL: u8 = 0xFF;
}

const _FORCE_LAYOUT: CompactOption<u8, Pair> = CompactOption::NONE;

fn main() {}

CompactRepr requires a Copy payload:

use compact_option::{CompactOption, CompactRepr};

#[derive(Clone)]
struct Opaque(u8);

unsafe impl CompactRepr<u8> for Opaque {
    const UNUSED_SENTINEL: u8 = 0xFF;
}

fn main() {
    let _ = CompactOption::<u8, Opaque>::NONE;
}

Implementations§

Source§

impl<R, T> CompactOption<R, T>
where R: Copy + PartialEq, T: CompactRepr<R>,

Source

pub const NONE: Self

Sentinel-backed empty value: the stored R equals CompactRepr::UNUSED_SENTINEL.

Layout of T and R is checked here (see struct-level Layout checks). Using NONE in a const context ensures that check runs; a plain let _ = Self::NONE in non-const main may not const-evaluate it.

Source

pub fn some(value: T) -> Self
where T: CompactRepr<R>, R: TransmuteFrom<T, { TRANSMUTATION_ASSUMPTION }>,

Construct a Some by transmuting TR using the same Assume bundle as try_unwrap / unwrap_unchecked.

Layout of T and R is asserted here (same as Self::NONE).

§Sentinel collisions

If value’s transmuted bit pattern equals CompactRepr::UNUSED_SENTINEL, this value is indistinguishable from Self::NONE: is_none may be true and try_unwrap returns None. A correct CompactRepr must rule that out for all stored T.

Not const because TransmuteFrom::transmute is not a const fn on this toolchain.

Source

pub const fn is_none(self) -> bool
where R: PartialEq,

Returns true when this value encodes Self::NONE (raw equals CompactRepr::UNUSED_SENTINEL).

Source

pub const fn is_some(self) -> bool
where R: PartialEq,

Returns true when this value encodes Some (raw differs from CompactRepr::UNUSED_SENTINEL).

Source

pub fn try_unwrap(self) -> Option<T>
where T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,

If this is Some, transmute the raw R back to T. If raw equals CompactRepr::UNUSED_SENTINEL, returns None (including sentinel-collision cases described on Self::some).

Source

pub fn unwrap(self) -> T
where T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,

Like Option::unwrap: returns the payload or panics if this is Self::NONE.

Source

pub fn expect(self, msg: &str) -> T
where T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,

Like Option::expect: returns the payload or panics with msg if this is Self::NONE.

Source

pub fn map<U, F>(self, f: F) -> Option<U>
where F: FnOnce(T) -> U, T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,

If Some, applies f to the payload; if Self::NONE, returns None without calling f.

Source

pub fn and_then<U, F>(self, f: F) -> Option<U>
where F: FnOnce(T) -> Option<U>, T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,

If Some, runs f on the payload; if Self::NONE, returns None without calling f.

Source

pub unsafe fn unwrap_unchecked(self) -> T
where T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,

§Safety

self must not be NONE, and self.raw_value must satisfy the CompactRepr encoding invariant for T.

Trait Implementations§

Source§

impl<R: Clone, T: Clone + CompactRepr<R>> Clone for CompactOption<R, T>

Source§

fn clone(&self) -> CompactOption<R, T>

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<R: Debug, T: Debug + CompactRepr<R>> Debug for CompactOption<R, T>

Source§

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

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

impl<R: Hash, T: Hash + CompactRepr<R>> Hash for CompactOption<R, T>

Source§

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

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<R: PartialEq, T: PartialEq + CompactRepr<R>> PartialEq for CompactOption<R, T>

Source§

fn eq(&self, other: &CompactOption<R, T>) -> 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<R: Copy, T: CompactRepr<R> + Copy> Copy for CompactOption<R, T>

Source§

impl<R: Eq, T: Eq + CompactRepr<R>> Eq for CompactOption<R, T>

Source§

impl<R, T: CompactRepr<R>> StructuralPartialEq for CompactOption<R, T>

Auto Trait Implementations§

§

impl<R, T> Freeze for CompactOption<R, T>
where R: Freeze,

§

impl<R, T> RefUnwindSafe for CompactOption<R, T>

§

impl<R, T> Send for CompactOption<R, T>
where R: Send, T: Send,

§

impl<R, T> Sync for CompactOption<R, T>
where R: Sync, T: Sync,

§

impl<R, T> Unpin for CompactOption<R, T>
where R: Unpin, T: Unpin,

§

impl<R, T> UnsafeUnpin for CompactOption<R, T>
where R: UnsafeUnpin,

§

impl<R, T> UnwindSafe for CompactOption<R, T>
where R: UnwindSafe, T: 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> 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.