Struct SentinelOption
pub struct SentinelOption<T: Sentinel>(/* private fields */);Expand description
Option<T> niche-packed into T::NONE. #[repr(transparent)] so it is
layout-identical to T (4 B for u32, vs 8 B for Option<u32>). The point
is the size, not a compiler niche — all the None/Some logic is explicit.
Equality is Option-like: two NONEs compare equal, NONE != some(x),
some(x) == some(x) — the derived comparison over the inner value gives
exactly that because NONE is a single reserved value.
Implementations§
§impl<T: Sentinel> SentinelOption<T>
impl<T: Sentinel> SentinelOption<T>
pub const NONE: Self
pub const NONE: Self
The None value.
pub fn some(v: T) -> Self
pub fn some(v: T) -> Self
Wrap a present value. Debug-asserts v is not the reserved sentinel — a
caller passing T::NONE as a Some is a bug (the value would read back
as None).
pub fn new(v: T) -> Self
pub fn new(v: T) -> Self
Total constructor: v == T::NONE becomes None, else Some(v). Use when
v may legitimately be the sentinel; prefer Self::some otherwise.
pub fn raw(self) -> T
pub fn raw(self) -> T
The raw inner value, T::NONE included — the inverse of Self::new,
not of Self::get. For a fixed-width wire that reserves the same value
for “unset” (no room for a null), raw() is the whole conversion, and
get().unwrap_or(T::NONE) would be a round trip that cancels.
Callers take on the sentinel convention: this yields T::NONE for an absent
value rather than panicking or defaulting, so feeding THAT result back to
Self::some is the bug that constructor asserts against. Round-tripping a
present value through some is fine.
pub fn unwrap_or(self, default: T) -> T
pub fn unwrap_or(self, default: T) -> T
Option::unwrap_or parity. Note unwrap_or(T::NONE) is exactly
Self::raw — the round trip cancels.
pub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Option::unwrap parity — panics on None. For sites that know a position
is set (the lexer always sets all six).
Trait Implementations§
§impl<T: Clone + Sentinel> Clone for SentinelOption<T>
impl<T: Clone + Sentinel> Clone for SentinelOption<T>
§fn clone(&self) -> SentinelOption<T>
fn clone(&self) -> SentinelOption<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<T: Copy + Sentinel> Copy for SentinelOption<T>
§impl<T: Sentinel + Debug> Debug for SentinelOption<T>
Prints as None / Some(x) — identical to the Option<u32> it replaces, so
Token’s derived Debug output is unchanged.
impl<T: Sentinel + Debug> Debug for SentinelOption<T>
Prints as None / Some(x) — identical to the Option<u32> it replaces, so
Token’s derived Debug output is unchanged.
§impl<T: Sentinel> Default for SentinelOption<T>
impl<T: Sentinel> Default for SentinelOption<T>
impl<T: Eq + Sentinel> Eq for SentinelOption<T>
§impl<T: Sentinel> From<Option<T>> for SentinelOption<T>
impl<T: Sentinel> From<Option<T>> for SentinelOption<T>
§impl<T: Sentinel> From<SentinelOption<T>> for Option<T>
impl<T: Sentinel> From<SentinelOption<T>> for Option<T>
§fn from(s: SentinelOption<T>) -> Self
fn from(s: SentinelOption<T>) -> Self
§impl<T: Hash + Sentinel> Hash for SentinelOption<T>
impl<T: Hash + Sentinel> Hash for SentinelOption<T>
§impl<T: PartialEq + Sentinel> PartialEq for SentinelOption<T>
impl<T: PartialEq + Sentinel> PartialEq for SentinelOption<T>
§impl<T: Sentinel> PartialEq<Option<T>> for SentinelOption<T>
Compare directly against a plain Option<T> — a SentinelOption is the
Option it stands for, so call sites and tests can write
tok.start_pos == Some(2) without an explicit .get().
impl<T: Sentinel> PartialEq<Option<T>> for SentinelOption<T>
Compare directly against a plain Option<T> — a SentinelOption is the
Option it stands for, so call sites and tests can write
tok.start_pos == Some(2) without an explicit .get().