pub struct CtOption<T> { /* private fields */ }Expand description
Equivalent of Option but predicated on a Choice with combinators that allow for
constant-time operations which always perform the same sequence of instructions regardless of
the value of is_some.
Unlike Option, CtOption always contains a value, and will use the contained value when
e.g. evaluating the callbacks of combinator methods, which unlike core it does unconditionally
in order to ensure constant-time operation. This approach stands in contrast to the lazy
evaluation similar methods on Option provide.
Implementations§
Source§impl<T> CtOption<T>
impl<T> CtOption<T>
Sourcepub const fn some(value: T) -> CtOption<T>
pub const fn some(value: T) -> CtOption<T>
Construct a new CtOption where self.is_some() is Choice::TRUE.
Sourcepub fn none() -> CtOption<T>where
T: Default,
pub fn none() -> CtOption<T>where
T: Default,
Construct a new CtOption with the Default value, and where self.is_some() is
Choice::FALSE.
Sourcepub const fn as_mut(&mut self) -> CtOption<&mut T>
pub const fn as_mut(&mut self) -> CtOption<&mut T>
Convert from a &mut CtOption<T> to CtOption<&mut T>.
Sourcepub fn as_deref(&self) -> CtOption<&T::Target>where
T: Deref,
pub fn as_deref(&self) -> CtOption<&T::Target>where
T: Deref,
Convert from CtOption<T> (or &CtOption<T>) to CtOption<&T::Target>, for types which
impl the Deref trait.
Sourcepub fn as_deref_mut(&mut self) -> CtOption<&mut T::Target>where
T: DerefMut,
pub fn as_deref_mut(&mut self) -> CtOption<&mut T::Target>where
T: DerefMut,
Convert from CtOption<T> (or &mut CtOption<T>) to CtOption<&mut T::Target>, for types
which impl the DerefMut trait.
Sourcepub fn expect(self, msg: &str) -> T
pub fn expect(self, msg: &str) -> T
Return the contained value, consuming the self value.
§Panics
In the event self.is_some() is Choice::FALSE, panics with a custom panic message
provided as the msg argument.
Sourcepub const fn expect_copied(self, msg: &str) -> Twhere
T: Copy,
pub const fn expect_copied(self, msg: &str) -> Twhere
T: Copy,
Return the contained value, consuming the self value, with const fn support.
Relies on a Copy bound which implies !Drop which is needed to be able to move out of
self in a const fn without feature(const_precise_live_drops).
§Panics
In the event self.is_some() is Choice::FALSE, panics with a custom panic message
provided as the msg argument.
Sourcepub const fn expect_ref(&self, msg: &str) -> &T
pub const fn expect_ref(&self, msg: &str) -> &T
Borrow the contained value.
§Panics
In the event self.is_some() is Choice::FALSE, panics with a custom panic message
provided as the msg argument.
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Convert the CtOption wrapper into an Option, depending on whether
CtOption::is_some is a truthy or falsy Choice.
This function exists to avoid ending up with ugly, verbose and/or bad handled conversions
from the CtOption wraps to an Option or Result.
It’s equivalent to the corresponding From impl, however this version is friendlier for
type inference.
This implementation doesn’t intend to be constant-time nor try to protect the leakage of the
T value since the Option will do it anyway.
Sourcepub const fn into_option_copied(self) -> Option<T>where
T: Copy,
pub const fn into_option_copied(self) -> Option<T>where
T: Copy,
Convert the CtOption wrapper into an Option in a const fn-friendly manner.
This is the equivalent of CtOption::into_option but is const fn-friendly by only
allowing Copy types which are implicitly !Drop and don’t run into problems with
const fn and destructors.
This implementation doesn’t intend to be constant-time nor try to protect the leakage of the
T value since the Option will do it anyway.
Sourcepub const fn is_some(&self) -> Choice
pub const fn is_some(&self) -> Choice
Returns Choice::TRUE if the option is the equivalent of a Some.
Sourcepub const fn is_none(&self) -> Choice
pub const fn is_none(&self) -> Choice
Returns Choice::TRUE if the option is the equivalent of a None.
Sourcepub fn and<U>(self, optb: CtOption<U>) -> CtOption<U>
pub fn and<U>(self, optb: CtOption<U>) -> CtOption<U>
Returns optb if self.is_some() is Choice::TRUE, otherwise returns a CtOption
where self.is_some() is Choice::FALSE.
Sourcepub fn and_then<U, F>(self, f: F) -> CtOption<U>
pub fn and_then<U, F>(self, f: F) -> CtOption<U>
Calls the provided callback with the wrapped inner value, returning the resulting
CtOption value in the event that self.is_some() is Choice::TRUE, or if not
returns a CtOption with self.is_none().
Unlike Option, the provided callback f is unconditionally evaluated to ensure
constant-time operation. This requires evaluating the function with “dummy” value of T
(e.g. if the CtOption was constructed with a supplied placeholder value and
Choice::FALSE, the placeholder value will be provided).
Sourcepub const fn as_inner_unchecked(&self) -> &T
pub const fn as_inner_unchecked(&self) -> &T
Obtain a reference to the inner value without first checking that self.is_some() is
Choice::TRUE.
This method is primarily intended for use in const fn scenarios where it’s not yet
possible to use the safe combinator methods, and returns a reference to avoid issues with
const fn destructors.
This method does not ensure the value is actually valid. Callers of this method should
take great care to ensure that self.is_some() is checked elsewhere.
Sourcepub fn filter<P>(self, predicate: P) -> Self
pub fn filter<P>(self, predicate: P) -> Self
Calls the provided callback with the wrapped inner value, which computes a Choice,
and updates self.is_some().
It updates it to be Choice::FALSE in the event the returned choice is also false.
If it was Choice::FALSE to begin with, it will unconditionally remain that way.
Sourcepub const fn filter_by(self, is_some: Choice) -> Self
pub const fn filter_by(self, is_some: Choice) -> Self
Apply an additional Choice requirement to is_some.
Sourcepub fn map<U, F>(self, f: F) -> CtOption<U>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, f: F) -> CtOption<U>where
F: FnOnce(T) -> U,
Maps a CtOption<T> to a CtOption<U> by unconditionally applying a function to the
contained value, but returning a new option value which inherits self.is_some().
Sourcepub fn map_or<U, F>(self, default: U, f: F) -> U
pub fn map_or<U, F>(self, default: U, f: F) -> U
Maps a CtOption<T> to a U value, eagerly evaluating the provided function, and returning
the supplied default in the event self.is_some() is Choice::FALSE.
Sourcepub fn map_or_default<U, F>(self, f: F) -> U
pub fn map_or_default<U, F>(self, f: F) -> U
Maps a CtOption<T> to a U value, eagerly evaluating the provided function, precomputing
U::default() using the Default trait, and returning it in the event self.is_some()
is Choice::FALSE.
Sourcepub fn ok_or<E>(self, err: E) -> Result<T, E>
pub fn ok_or<E>(self, err: E) -> Result<T, E>
Transforms a CtOption<T> into a Result<T, E>, mapping to Ok(T) if self.is_some() is
Choice::TRUE, or mapping to the provided err in the event self.is_some() is
Choice::FALSE.
This implementation doesn’t intend to be constant-time nor try to protect the leakage of the
T value since the Result will do it anyway.
Sourcepub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>where
F: FnOnce() -> E,
pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>where
F: FnOnce() -> E,
Transforms a CtOption<T> into a Result<T, E> by unconditionally calling the provided
callback value and using its result in the event self.is_some() is Choice::FALSE.
This implementation doesn’t intend to be constant-time nor try to protect the leakage of the
T value since the Result will do it anyway.
Sourcepub fn or(self, optb: CtOption<T>) -> CtOption<T>where
T: CtSelect,
pub fn or(self, optb: CtOption<T>) -> CtOption<T>where
T: CtSelect,
Returns self if self.is_some() is Choice::TRUE, otherwise returns optb.
Sourcepub const fn to_inner_unchecked(self) -> Twhere
T: Copy,
pub const fn to_inner_unchecked(self) -> Twhere
T: Copy,
Obtain a copy of the inner value without first checking that self.is_some() is
Choice::TRUE.
This method is primarily intended for use in const fn scenarios where it’s not yet
possible to use the safe combinator methods, and uses a Copy bound to avoid issues with
const fn destructors.
This method does not ensure the value is actually valid. Callers of this method should
take great care to ensure that self.is_some() is checked elsewhere.
Sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Return the contained value, consuming the self value.
Use of this function is discouraged due to panic potential. Instead, prefer non-panicking
alternatives such as unwrap_or or unwrap_or_default which operate in constant-time.
As the final step of a sequence of constant-time operations, or in the event you are dealing
with a CtOption in a non-secret context where constant-time does not matter, you can
also convert to Option using into_option or the From impl on Option. Note
this introduces a branch and with it a small amount of timing variability. If possible try
to avoid this branch when writing constant-time code (e.g. use implicit rejection instead
of Option/Result to handle errors)
§Panics
In the event self.is_some() is Choice::FALSE.
Sourcepub fn unwrap_or(self, default: T) -> Twhere
T: CtSelect,
pub fn unwrap_or(self, default: T) -> Twhere
T: CtSelect,
Return the contained value in the event self.is_some() is Choice::TRUE, or if not,
uses a provided default.
Sourcepub fn unwrap_or_default(self) -> T
pub fn unwrap_or_default(self) -> T
Unconditionally computes T::default() using the Default trait, then returns either
the contained value if self.is_some() is Choice::TRUE, or if it’s Choice::FALSE
returns the previously computed default.
Sourcepub fn xor(self, optb: CtOption<T>) -> CtOption<T>where
T: CtSelect,
pub fn xor(self, optb: CtOption<T>) -> CtOption<T>where
T: CtSelect,
Returns an “is some” CtOption with the contained value from either self or optb in
the event exactly one of them has self.is_some() set to Choice::TRUE, or else returns
a CtOption with self.is_some() set to Choice::FALSE.
Sourcepub fn zip<U>(self, other: CtOption<U>) -> CtOption<(T, U)>
pub fn zip<U>(self, other: CtOption<U>) -> CtOption<(T, U)>
Zips self with another CtOption.
If self.is_some() && other.is_some(), this method returns a new CtOption for a 2-tuple
of their contents where is_some() is Choice::TRUE.
Otherwise, a CtOption where is_some() is Choice::FALSE is returned.
Sourcepub fn zip_with<U, F, R>(self, other: CtOption<U>, f: F) -> CtOption<R>where
F: FnOnce(T, U) -> R,
pub fn zip_with<U, F, R>(self, other: CtOption<U>, f: F) -> CtOption<R>where
F: FnOnce(T, U) -> R,
Zips self and another CtOption with function f.
If self.is_some() && other.is_some(), this method returns a new CtOption for
the result of f applied to their inner values where is_some() is Choice::TRUE.
Otherwise, a CtOption where is_some() is Choice::FALSE is returned.
Trait Implementations§
Source§impl<T: CtSelect> CtSelect for CtOption<T>
impl<T: CtSelect> CtSelect for CtOption<T>
Source§impl<T> From<CtOption<T>> for CtOption<T>where
T: ConditionallySelectable + Default,
Available on crate feature subtle only.NOTE: in order to be able to unwrap the subtle::CtOption we rely on a Default bound in
order to have a placeholder value, and ConditionallySelectable to be able to use unwrap_or.
impl<T> From<CtOption<T>> for CtOption<T>where
T: ConditionallySelectable + Default,
subtle only.NOTE: in order to be able to unwrap the subtle::CtOption we rely on a Default bound in
order to have a placeholder value, and ConditionallySelectable to be able to use unwrap_or.
Source§impl<T> From<CtOption<T>> for Option<T>
Convert the CtOption wrapper into an Option, depending on whether
CtOption::is_some is a truthy or falsy Choice.
impl<T> From<CtOption<T>> for Option<T>
Convert the CtOption wrapper into an Option, depending on whether
CtOption::is_some is a truthy or falsy Choice.
This implementation doesn’t intend to be constant-time nor try to protect the leakage of the
T value since the Option will do it anyway.