#![feature(derive_default_enum)]
#![feature(try_trait_v2)]
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Hash, Default)]
pub enum Probably<T> {
#[default]
Nothing,
Something(T),
}
pub use Probably::{Nothing, Something};
impl<T> std::process::Termination for Probably<T> {
fn report(self) -> std::process::ExitCode {
match self {
Nothing => std::process::ExitCode::FAILURE,
_ => std::process::ExitCode::SUCCESS,
}
}
}
impl<T> std::ops::FromResidual for Probably<T> {
fn from_residual(residual: Probably<std::convert::Infallible>) -> Self {
match residual {
Nothing => Nothing,
Something(_) => todo!(),
}
}
}
impl<T> std::ops::Try for Probably<T> {
type Output = T;
type Residual = Probably<std::convert::Infallible>;
fn from_output(output: Self::Output) -> Self {
Something(output)
}
fn branch(self) -> std::ops::ControlFlow<Self::Residual, Self::Output> {
match self {
Something(v) => std::ops::ControlFlow::Continue(v),
Nothing => std::ops::ControlFlow::Break(Nothing),
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}