apply/
lib.rs

1#![no_std]
2#![deny(missing_docs)]
3
4//! A tiny library for chaining free functions into method call chains.
5
6#[cfg(test)]
7#[macro_use]
8extern crate std;
9
10mod also;
11
12pub use also::Also;
13
14/// Represents a type which can have functions applied to it (implemented
15/// by default for all types).
16pub trait Apply<Res> {
17    /// Apply a function which takes the parameter by value.
18    fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Res where Self: Sized {
19        f(self)
20    }
21
22    /// Apply a function which takes the parameter by reference.
23    fn apply_ref<F: FnOnce(&Self) -> Res>(&self, f: F) -> Res {
24        f(self)
25    }
26
27    /// Apply a function which takes the parameter by mutable reference.
28    fn apply_mut<F: FnOnce(&mut Self) -> Res>(&mut self, f: F) -> Res {
29        f(self)
30    }
31}
32
33impl<T: ?Sized, Res> Apply<Res> for T {
34    // use default definitions...
35}