pub enum Unsure<T> {
Reject,
Nothing,
Just(T),
}
use Unsure::*;
impl<T> Unsure<T> {
pub const fn is_reject(&self) -> bool {
matches!(self, Reject)
}
pub const fn is_nothing(&self) -> bool {
matches!(self, Nothing)
}
pub const fn is_just(&self) -> bool {
matches!(self, Just(_))
}
pub const fn as_ref(&self) -> Option<&T> {
match self {
Just(value) => Some(value),
_ => None,
}
}
pub const fn as_mut(&mut self) -> Option<&mut T> {
match self {
Just(value) => Some(value),
_ => None,
}
}
pub fn unwrap(self) -> T {
match self {
Just(value) => value,
Nothing => panic!("tried to unwrap nothing; can't unwrap a `Nothing` value"),
Reject => panic!("tried to unwrap a rejection; can't unwrap a `Reject` value"),
}
}
pub fn unwrap_or(self, default: T) -> T {
match self {
Just(value) => value,
Nothing => default,
Reject => default,
}
}
pub fn unwrap_or_else(self, f: impl FnOnce() -> T) -> T {
match self {
Just(value) => value,
Nothing => f(),
Reject => f(),
}
}
pub fn unwrap_or_default(self) -> T
where
T: Default,
{
match self {
Just(value) => value,
Nothing => T::default(),
Reject => panic!(
"tried to obtain default of `Reject`; rejections are failures and cannot be recovered from"
),
}
}
}
impl<T> From<Option<T>> for Unsure<T> {
fn from(opt: Option<T>) -> Self {
match opt {
Some(value) => Just(value),
None => Nothing,
}
}
}