eitherable

Trait Eitherable

Source
pub trait Eitherable {
    // Required methods
    fn either<L, R>(&self, left: L, right: R) -> Either<L, R> ;
    fn either_else<L, FL: FnOnce() -> L, R, FR: FnOnce() -> R>(
        &self,
        l_func: FL,
        r_func: FR,
    ) -> Either<L, R> ;
}
Expand description

Extention trait to create the Either type from a boolean. Can be implemented on any type.

Required Methods§

Source

fn either<L, R>(&self, left: L, right: R) -> Either<L, R>

If the boolean is true, returns Either::Left(left). If it is false, returns Either::Right(right). left and right are evaluated eagerly. For lazy evaluation, use either_else()

§Examples
use eitherable::*;

let x = true;
assert_eq!(x.either(1, "right"), Either::Left(1));

let x = false;
assert_eq!(x.either(1, "right"), Either::Right("right"));
Source

fn either_else<L, FL: FnOnce() -> L, R, FR: FnOnce() -> R>( &self, l_func: FL, r_func: FR, ) -> Either<L, R>

Same as either(), but the values are lazily evaluated.

§Examples
use eitherable::*;

let x = true;
assert_eq!(x.either_else(|| 1,|| "right"), Either::Left(1));

let x = false;
assert_eq!(x.either_else(|| 1,|| "right"), Either::Right("right"));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Eitherable for bool

Source§

fn either<L, R>(&self, left: L, right: R) -> Either<L, R>

Source§

fn either_else<L, FL: FnOnce() -> L, R, FR: FnOnce() -> R>( &self, l_func: FL, r_func: FR, ) -> Either<L, R>

Implementors§