Skip to main content

Arrow

Trait Arrow 

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

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

A trait for composable function wrappers with Category and Strong instances.

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.

Unlike CloneFn, which provides cloneable wrappers for use in applicative contexts, Arrow provides composable wrappers for use in the optics system.

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: 'a, B: 'a>: 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 arrow<'a, A: 'a, B: 'a>( f: impl 'a + Fn(A) -> B, ) -> <Self as Arrow>::Of<'a, A, B>

Lifts a pure function into an arrow.

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

§Type Signature

forall A B. (A -> B) -> Self 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 lift into an arrow.
§Returns

The wrapped function.

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

let f = arrow::<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: ToDynCloneFn> Arrow for FnBrand<P>

§Type Parameters
  • P: The reference-counted pointer type.
Source§

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