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>
impl<R, T> CompactOption<R, T>
Sourcepub const NONE: Self
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.
Sourcepub fn some(value: T) -> Selfwhere
T: CompactRepr<R>,
R: TransmuteFrom<T, { TRANSMUTATION_ASSUMPTION }>,
pub fn some(value: T) -> Selfwhere
T: CompactRepr<R>,
R: TransmuteFrom<T, { TRANSMUTATION_ASSUMPTION }>,
Construct a Some by transmuting T → R 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.
Sourcepub const fn is_none(self) -> boolwhere
R: PartialEq,
pub const fn is_none(self) -> boolwhere
R: PartialEq,
Returns true when this value encodes Self::NONE (raw equals CompactRepr::UNUSED_SENTINEL).
Sourcepub const fn is_some(self) -> boolwhere
R: PartialEq,
pub const fn is_some(self) -> boolwhere
R: PartialEq,
Returns true when this value encodes Some (raw differs from CompactRepr::UNUSED_SENTINEL).
Sourcepub fn try_unwrap(self) -> Option<T>where
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
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).
Sourcepub fn unwrap(self) -> Twhere
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
pub fn unwrap(self) -> Twhere
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
Like Option::unwrap: returns the payload or panics if this is Self::NONE.
Sourcepub fn expect(self, msg: &str) -> Twhere
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
pub fn expect(self, msg: &str) -> Twhere
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
Like Option::expect: returns the payload or panics with msg if this is Self::NONE.
Sourcepub fn map<U, F>(self, f: F) -> Option<U>where
F: FnOnce(T) -> U,
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
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.
Sourcepub fn and_then<U, F>(self, f: F) -> Option<U>
pub fn and_then<U, F>(self, f: F) -> Option<U>
If Some, runs f on the payload; if Self::NONE, returns None without calling f.
Sourcepub unsafe fn unwrap_unchecked(self) -> Twhere
T: TransmuteFrom<R, { TRANSMUTATION_ASSUMPTION }>,
pub unsafe fn unwrap_unchecked(self) -> Twhere
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>
impl<R: Clone, T: Clone + CompactRepr<R>> Clone for CompactOption<R, T>
Source§fn clone(&self) -> CompactOption<R, T>
fn clone(&self) -> CompactOption<R, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more