#chaining #pipelines #tap #pipe

no-std tailsome

Blanket traits providing .into_ok(), .into_err(), and .into_some() for happier method chaining

2 releases (1 stable)

1.0.0 Mar 16, 2023
0.1.0 Jan 21, 2022

#576 in Rust patterns

Download history 7/week @ 2023-01-20 5/week @ 2023-01-27 2/week @ 2023-02-03 3/week @ 2023-02-10 107/week @ 2023-02-17 156/week @ 2023-02-24 4/week @ 2023-03-03 14/week @ 2023-03-10 18/week @ 2023-03-17 128/week @ 2023-03-24 5/week @ 2023-03-31 10/week @ 2023-04-07 5/week @ 2023-04-14 1/week @ 2023-04-21 85/week @ 2023-04-28 7/week @ 2023-05-05

99 downloads per month

MIT/Apache

5KB

tailsome

Blanket traits providing methods for turning anything into an Option or Result.

Inspired by tap::Pipe, which provides a pipe method that allows the user to call any bare function in the middle of a method chain.

The most useful of these traits' methods is probably IntoResult::into_ok. Instead of wrapping a long method-chain expression in a call to Ok, try tacking an .into_ok() at the end. This can be especially pleasing in functions that return a Result and make judicious use of method chains and the ? operator.

use tailsome::IntoResult;

build_pipeline().unwrap();

fn build_pipeline() -> Result<Example, Error> {
    Builder::new()
        .set_option(42)
        .try_set_option("some flag")?
        .set_option("another option")
        .build()
        .into_ok()
}

struct Example;

struct Builder;
impl Builder {
    fn new() -> Self { Self }
    fn set_option<T>(self, t: T) -> Self { self }
    fn try_set_option<T>(self, t: T) -> Result<Self, Error> { Ok(self) }
    fn build(self) -> Example { Example }
}

#[derive(Debug)]
struct Error;

No runtime deps