Skip to main content

SentinelOption

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>

pub const NONE: Self

The None value.

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

Total constructor: v == T::NONE becomes None, else Some(v). Use when v may legitimately be the sentinel; prefer Self::some otherwise.

pub fn get(self) -> Option<T>

Recover the Option<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 is_some(self) -> bool

Whether a value is present — Option::is_some parity.

pub fn is_none(self) -> bool

Whether this is the reserved sentinel — Option::is_none parity.

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

Option::unwrap parity — panics on None. For sites that know a position is set (the lexer always sets all six).

pub fn expect(self, msg: &str) -> T

Option::expect parity — panics with msg on None. #[track_caller] (like std’s) so the panic reports the CALLER’s line, not this wrapper’s — without it every expect in the workspace blames one line in this file.

pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U>

Option::map parity. Returns Option<U>, NOT SentinelOption<U>: U has no reserved value, so this is the one method that leaves the sentinel domain.

Trait Implementations§

§

impl<T: Clone + Sentinel> Clone for SentinelOption<T>

§

fn clone(&self) -> SentinelOption<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
§

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

§

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

Formats the value using the given formatter. Read more
§

impl<T: Sentinel> Default for SentinelOption<T>

§

fn default() -> Self

Returns the “default value” for a type. Read more
§

impl<T: Eq + Sentinel> Eq for SentinelOption<T>

§

impl<T: Sentinel> From<Option<T>> for SentinelOption<T>

§

fn from(o: Option<T>) -> Self

Converts to this type from the input type.
§

impl<T: Sentinel> From<SentinelOption<T>> for Option<T>

§

fn from(s: SentinelOption<T>) -> Self

Converts to this type from the input type.
§

impl<T: Hash + Sentinel> Hash for SentinelOption<T>

§

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
§

impl<T: PartialEq + Sentinel> PartialEq for SentinelOption<T>

§

fn eq(&self, other: &SentinelOption<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
§

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

§

fn eq(&self, other: &Option<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
§

impl<T: PartialEq + Sentinel> StructuralPartialEq for SentinelOption<T>

Auto Trait Implementations§

§

impl<T> Freeze for SentinelOption<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for SentinelOption<T>
where T: RefUnwindSafe,

§

impl<T> Send for SentinelOption<T>
where T: Send,

§

impl<T> Sync for SentinelOption<T>
where T: Sync,

§

impl<T> Unpin for SentinelOption<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SentinelOption<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for SentinelOption<T>
where 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 = !

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.