Crates.io | chainer |
lib.rs | chainer |
version | 0.1.1 |
source | src |
created_at | 2022-03-03 16:15:16.913073 |
updated_at | 2022-03-03 16:16:26.645468 |
description | A cursed crate that allows for global call chaining with access to chained function results |
homepage | |
repository | https://github.com/WilliamVenner/chainer |
max_upload_size | |
id | 542941 |
size | 17,616 |
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.
[dependencies]
chainer = "*"
# or, with a nightly compiler
[dependencies]
chainer = { version = "*", features = ["results"] }
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!
}
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"]
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!
}
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
}