#chaining #chain #chainer

chainer

A cursed crate that allows for global call chaining with access to chained function results

2 releases

Uses new Rust 2021

0.1.1 Mar 3, 2022
0.1.0 Mar 3, 2022

#436 in Rust patterns

Download history 26/week @ 2022-02-26 10/week @ 2022-03-05 9/week @ 2022-03-12 9/week @ 2022-03-19 1/week @ 2022-03-26 2/week @ 2022-04-02 2/week @ 2022-04-09 2/week @ 2022-04-23 4/week @ 2022-04-30 14/week @ 2022-05-07 34/week @ 2022-05-14 7/week @ 2022-05-21 14/week @ 2022-05-28 14/week @ 2022-06-04

69 downloads per month

MIT license

16KB
191 lines

crates.io docs.rs license

chainer

A cursed crate that allows for global call chaining with access to chained function results

Currently the results feature requires the min_specialization Rust Nightly feature.

Usage

[dependencies]
chainer = "*"

# or, with a nightly compiler

[dependencies]
chainer = { version = "*", features = ["results"] }

Examples

Basic usage (default)

Immutable call chaining

use chainer::*;

struct HelloWorld;
impl HelloWorld {
    fn print(&self) {
        println!("Hello, world!");
    }
}

fn main() {
    HelloWorld
        .chain(HelloWorld::print)
        .chain(HelloWorld::print)
        .chain(HelloWorld::print);

    // Hello, world!
    // Hello, world!
    // Hello, world!
}

Mutable call chaining

use chainer::*;

struct Counter { value: i32 }
impl Counter {
    fn increment(&mut self) {
        self.value += 1;
        println!("{}", self.value);
    }
}

fn main() {
    Counter { value: 0 }
        .chain_mut(Counter::increment)
        .chain_mut(Counter::increment)
        .chain_mut(Counter::increment);

    // 1
    // 2
    // 3
}

features = ["results"]

Immutable call chaining

use chainer::*;

struct HelloWorld;
impl HelloWorld {
    fn print(&self) -> &'static str {
        println!("Hello, world!");
        "It works!"
    }
}

fn main() {
    let value: &'static str = HelloWorld
        .chain(HelloWorld::print)
        .chain(HelloWorld::print)
        .chain(HelloWorld::print)
        .result;

    // Hello, world!
    // Hello, world!
    // Hello, world!
    // It works!
}

Mutable call chaining

use chainer::*;

struct Counter { value: i32 }
impl Counter {
    fn increment(&mut self) -> i32 {
        self.value += 1;
        println!("{}", self.value);
        self.value
    }
}

fn main() {
    let value: i32 = Counter { value: 0 }
        .chain_mut(Counter::increment)
        .chain_mut(Counter::increment)
        .chain_mut(Counter::increment)
        .result;

    println!("{value}");

    // 1
    // 2
    // 3
    // 3
}

No runtime deps

Features

  • results