chainer 0.1.0

A cursed crate that allows for global call chaining with access to chained function results
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented5 out of 5 items with examples
  • Ø build duration
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • WilliamVenner

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
}