Trait eso::maybe::Maybe[][src]

pub trait Maybe: Sized + __SealMaybe {
    type Inner;
    fn inner(&self) -> &Self::Inner;
fn inner_mut(&mut self) -> &mut Self::Inner;
fn unwrap(self) -> Self::Inner;
fn unwrap_try<F, T>(self, f: F) -> Result<T, Self>
    where
        F: FnOnce(Self::Inner) -> Result<T, Self::Inner>
;
fn unwrap_try_x<F, T, E>(self, f: F) -> Result<T, (Self, E)>
    where
        F: FnOnce(Self::Inner) -> Result<T, (Self::Inner, E)>
; fn map<F, NewInner>(self, f: F) -> Self::Out
    where
        Self: MaybeMap<NewInner>,
        F: FnOnce(Self::Inner) -> NewInner
, { ... } }
Expand description

A type-level optional value.

This trait is sealed and is only implemented by two types from this crate:

  • An<T> denotes a value that is present on the type-level
  • No<T> denotes the absence of any value, as in there can never be an expression that is executed at run-time that yields a value of this type.

Note that since no value of No can ever exist, all functions in this trait may safely assume that they will only be called on an An.

pub enum X {
    Yup(An<i32>),
    Nope(No<String>),
}

// Since no value of X can be of the `Nope` variant, this is
// well-typed and cannot panic:
fn unwrap_x(x: X) -> i32 {
    match x {
        X::Yup(An(i)) => i,
        _ => unreachable!()
    }
}

Associated Types

type Inner[src]

The type whose presence or absence is in question

Required methods

fn inner(&self) -> &Self::Inner[src]

Yield a reference to the inner value.

fn inner_mut(&mut self) -> &mut Self::Inner[src]

Yield a mutable reference to the inner value.

fn unwrap(self) -> Self::Inner[src]

Recover the inner value from the wrapper.

fn unwrap_try<F, T>(self, f: F) -> Result<T, Self> where
    F: FnOnce(Self::Inner) -> Result<T, Self::Inner>, 
[src]

Run a function on the inner value that either succeeds or gives back the inner value

fn unwrap_try_x<F, T, E>(self, f: F) -> Result<T, (Self, E)> where
    F: FnOnce(Self::Inner) -> Result<T, (Self::Inner, E)>, 
[src]

Run a function on the inner value that either succeeds or gives back the inner value, plus an error value

Provided methods

fn map<F, NewInner>(self, f: F) -> Self::Out where
    Self: MaybeMap<NewInner>,
    F: FnOnce(Self::Inner) -> NewInner, 
[src]

Apply a function to the contained value while keeping the result contained.

Implementors

impl<A> Maybe for An<A>[src]

type Inner = A

fn inner(&self) -> &A[src]

fn inner_mut(&mut self) -> &mut A[src]

fn unwrap(self) -> A[src]

fn unwrap_try<F, T>(self, f: F) -> Result<T, Self> where
    F: FnOnce(Self::Inner) -> Result<T, A>, 
[src]

fn unwrap_try_x<F, T, E>(self, f: F) -> Result<T, (Self, E)> where
    F: FnOnce(Self::Inner) -> Result<T, (Self::Inner, E)>, 
[src]

fn map<F, B>(self, f: F) -> Self::Out where
    Self: MaybeMap<B>,
    F: FnOnce(Self::Inner) -> B, 
[src]

impl<A> Maybe for No<A>[src]

type Inner = A

fn inner(&self) -> &A[src]

fn inner_mut(&mut self) -> &mut Self::Inner[src]

fn unwrap(self) -> A[src]

fn unwrap_try<F, T>(self, _f: F) -> Result<T, Self> where
    F: FnOnce(Self::Inner) -> Result<T, A>, 
[src]

fn unwrap_try_x<F, T, E>(self, _f: F) -> Result<T, (Self, E)> where
    F: FnOnce(Self::Inner) -> Result<T, (Self::Inner, E)>, 
[src]

fn map<F, NewInner>(self, _: F) -> Self::Out where
    Self: MaybeMap<NewInner>,
    F: FnOnce(Self::Inner) -> NewInner, 
[src]