chainer/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#![no_std]
#![deny(missing_docs)]
#![cfg_attr(feature = "results", feature(min_specialization))]
//! 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
//!
//! ```toml
//! [dependencies]
//! chainer = "*"
//!
//! ## or, with a nightly compiler
//!
//! [dependencies]
//! chainer = { version = "*", features = ["results"] }
//! ```
//!
//! # Examples
//!
//! ## Basic usage (default)
//!
//! ### Immutable call chaining
//!
//! ```rust
//! 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
//!
//! ```rust
//! 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
//!
//! ```ignore
//! 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
//!
//! ```ignore
//! 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
//! }
//! ```
#[cfg(feature = "results")]
mod results;
#[cfg(feature = "results")]
pub use results::*;
#[cfg(not(feature = "results"))]
mod basic;
#[cfg(not(feature = "results"))]
pub use basic::*;
#[cfg(test)]
mod tests;