Skip to main content

Function

Trait Function 

Source
pub trait Function: Category {
    type Of<'a, A, B>: Deref<Target = dyn Fn(A) -> B + 'a>;

    // Required method
    fn new<'a, A, B>(
        f: impl 'a + Fn(A) -> B,
    ) -> <Self as Function>::Of<'a, A, B>;
}
Expand description

A trait for wrappers over closures, allowing for generic handling of functions in higher-kinded contexts.

This trait is implemented by “Brand” types (like ArcFnBrand and RcFnBrand) to provide a way to construct and type-check wrappers over closures (Arc<dyn Fn...>, Rc<dyn Fn...>, etc.) in a generic context, allowing library users to choose between implementations at function call sites.

The lifetime 'a ensures the function doesn’t outlive referenced data, while generic types A and B represent the input and output types, respectively.

Required Associated Types§

Source

type Of<'a, A, B>: Deref<Target = dyn Fn(A) -> B + 'a>

The type of the function wrapper.

This associated type represents the concrete type of the wrapper (e.g., Rc<dyn Fn(A) -> B>) that dereferences to the underlying closure.

Required Methods§

Source

fn new<'a, A, B>(f: impl 'a + Fn(A) -> B) -> <Self as Function>::Of<'a, A, B>

Creates a new function wrapper.

This function wraps the provided closure f into a function wrapper.

§Type Signature

forall a b. (a -> b) -> a -> b

§Type Parameters
  • 'a: The lifetime of the function and its captured data.
  • A: The input type of the function.
  • B: The output type of the function.
§Parameters
  • f: The closure to wrap.
  • _: The input value to the function.
§Returns

The wrapped function.

§Examples
use fp_library::{brands::*, functions::*};

let f = fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
assert_eq!(f(5), 10);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<P: UnsizedCoercible> Function for FnBrand<P>

Source§

type Of<'a, A, B> = <FnBrand<P> as Kind_140eb1e35dc7afb3>::Of<'a, A, B>