option_either_or 1.0.0

Option into Either conversion
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented0 out of 3 items with examples
  • Size
  • Source code size: 21.89 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.17 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • komar007/option_either_or
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • komar007

option_either_or

Crates.io License Crates.io Version GitHub branch check runs docs.rs

Convert Option<T> into Either<L, T>.

Overview

This crate introduces either_or and either_or_else to Option in order to facilitate conversions from Option to Either.

It is mainly useful as generalizations of unwrap_or/unwrap_or_else where the provided default value is of a different type than the Some variant, but both types implement some common trait.

Examples

You can take advantage of Either implementing Display to provide a default value to Option<String> without allocating another string...

use option_either_or::OptionEitherOr as _;
let x = Some(String::from("string"));
let y = x.either_or("default");
println!("{y}");

... or provide defaults to impl Display (or any other trait generically implemented by Either) in a generic context...

fn transform(x: Option<impl Display>) -> impl Display {
    use option_either_or::OptionEitherOr as _;

    x.either_or_else(|| "default")
}

... or even provide defaults to a generic Future, like so:

use std::{future::Pending, time::Duration};

use option_either_or::OptionEitherOr;

async fn await_or_sleep(f: Option<impl Future<Output = ()>>) {
    f.either_or_else(|| async move {
        println!("doing nothing by default and sleeping...");
        tokio::time::sleep(Duration::from_secs(1)).await;
        println!("...slept");
    })
    .await
}

#[tokio::main]
async fn main() {
    await_or_sleep(Some(async move {
        for _ in 0..4 {
            println!("something real");
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }))
    .await;

    println!("---");

    await_or_sleep(Option::<Pending<()>>::None).await;
}