Struct fbox::FBox

source ·
pub struct FBox<FIn, FOut> { /* private fields */ }
Expand description

FBox is a generic wrapper of a unary function. FBox lets you compose unary functions via a user friendly syntax and in a type-safe manner. FBox works with functions that take ownership over their argument. This way you can exploit the ownership rules of Rust. FBox is lazy. It only calls its function when you explicitly tell it to do so. You can call apply or apply_drop with a single argument, the type of which corresponds to the FIn type parameter of the FBox. The return type will correspond to the FOut type parameter.

Implementations

Creates a new FBox from a unary function.

Examples
 fn inc(n: i32) -> i32 {
     n + 1
 }

 let fbox1 = FBox::new(|x| x + 1);
 let fbox2 = FBox::new(inc);

 assert_eq!(
     fbox1.apply(3),
     fbox2.apply(3)
 );

You can compose a function f(x) inside an FBox with another function g. The result is a new function f(g(x)) wrapped in a new FBox. The output type of g must match the input type of f.

Examples
 assert_eq!(
     10,
     FBox::new(|x| x + 1).compose(|x| x * x).apply(3),
 );

Similar to compose, except the result is g(f(x)) in a new FBox. The output type of f must match the input type of g.

Examples
 assert_eq!(
     16,
     FBox::new(|x| x + 1).and_then(|x| x * x).apply(3),
 );

Similar to compose, except its argument is another FBox with a function g.

Examples
 assert_eq!(
     10,
     FBox::new(|x| x + 1).compose_b(FBox::new(|x| x * x)).apply(3)
 );

Similar to and_then, except its argument is another FBox with a function g.

Examples
 assert_eq!(
     16,
     FBox::new(|x| x + 1).and_then_b(FBox::new(|x| x * x)).apply(3)
 );

Trait Implementations

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.