eitherable 0.1.0

Extension trait to create the `either` type from booleans. (and maybe other types)
Documentation
  • Coverage
  • 75%
    3 out of 4 items documented2 out of 3 items with examples
  • Size
  • Source code size: 4.24 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.69 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • asibahi/eitherable
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • asibahi

eitherable

Info

This crate extends bool with a simple way to create the Either type, from the either crate.

It is a simple convenience trait and method, and also a way for me to learn about how crates.io and cargo work.

Why is this not in the either crate

The either crate emphasies that the two sides of Either, Left nd Right, have no element of truthiness or falsiness. However, the trait here immediately assigns Left to be the truthy value. So this is not quite suitable for the either crate.

The reason I chose Left to be the truthy value is simple: it retains the dame order as the if statement. The two following statements are equivalent.

let fst_example = if my_cond { Either::Left(left) } else { Either::Right(right) };
let snd_example = my_cond.either(left, right);

If you think Right should be the truthy value, you can .flip().

Usage

Dependency

Add the library as a dependency to your project by inserting

eitherable = "0.1.0"

into the [dependencies] section of your Cargo.toml file.

Example

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"));

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"));