option_either_or

Crates.iooption_either_or
lib.rsoption_either_or
version1.0.0
created_at2025-08-13 13:49:21.974855+00
updated_at2025-08-13 13:49:21.974855+00
descriptionOption into Either conversion
homepage
repositoryhttps://github.com/komar007/option_either_or
max_upload_size
id1793640
size22,392
Michal Trybus (komar007)

documentation

https://docs.rs/option_either_or

README

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;
}
Commit count: 0

cargo fmt