Struct 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§

Source§

impl<FIn: 'static, FOut: 'static> FBox<FIn, FOut>

Source

pub fn new(f: impl Fn(FIn) -> FOut + 'static) -> FBox<FIn, FOut>

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)
 );
Source

pub fn compose<GIn: 'static>( self, g: impl Fn(GIn) -> FIn + 'static, ) -> FBox<GIn, FOut>

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),
 );
Source

pub fn and_then<GOut: 'static>( self, g: impl Fn(FOut) -> GOut + 'static, ) -> FBox<FIn, GOut>

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),
 );
Source

pub fn compose_b<GIn: 'static>(self, other: FBox<GIn, FIn>) -> FBox<GIn, FOut>

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)
 );
Source

pub fn and_then_b<GOut: 'static>( self, other: FBox<FOut, GOut>, ) -> FBox<FIn, GOut>

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§

Source§

impl<FIn, FOut> Apply for FBox<FIn, FOut>

Source§

type In = FIn

Source§

type Out = FOut

Source§

fn apply(&self, a: FIn) -> FOut

Source§

impl<FIn, FOut> ApplyDrop for FBox<FIn, FOut>

Source§

type In = FIn

Source§

type Out = FOut

Source§

fn apply_drop(self, a: FIn) -> FOut

Auto Trait Implementations§

§

impl<FIn, FOut> Freeze for FBox<FIn, FOut>

§

impl<FIn, FOut> !RefUnwindSafe for FBox<FIn, FOut>

§

impl<FIn, FOut> !Send for FBox<FIn, FOut>

§

impl<FIn, FOut> !Sync for FBox<FIn, FOut>

§

impl<FIn, FOut> Unpin for FBox<FIn, FOut>

§

impl<FIn, FOut> !UnwindSafe for FBox<FIn, FOut>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.