Skip to main content

Module dispatch

Module dispatch 

Source
Expand description

Dispatch infrastructure for unified free functions that route to either by-value or by-reference trait methods based on the closure’s argument type.

The dispatch system uses marker types (Val and Ref) to select the appropriate trait at compile time. The compiler infers the marker from the closure’s argument type: Fn(A) -> B resolves to Val, Fn(&A) -> B resolves to Ref.

The ClosureMode trait maps each marker to the corresponding dyn Fn trait object type, used by CloneFn and SendCloneFn to parameterize the Deref target of wrapped closures.

§Sub-modules

Each sub-module provides a dispatch trait and unified free function for a specific type class operation, mirroring the corresponding classes/ module:

  • functor: FunctorDispatch + map
  • semimonad: BindDispatch + bind
  • lift: Lift2Dispatch-Lift5Dispatch + lift2-lift5
  • foldable: FoldRightDispatch, FoldLeftDispatch, FoldMapDispatch + fold_right, fold_left, fold_map

§Examples

use fp_library::{
	brands::*,
	functions::explicit::*,
	types::*,
};

// Closure takes i32 -> dispatches to Functor::map
let y = map::<OptionBrand, _, _, _, _>(|x: i32| x * 2, Some(5));
assert_eq!(y, Some(10));

// Closure takes &i32 -> dispatches to RefFunctor::ref_map
let lazy = RcLazy::pure(10);
let mapped = map::<LazyBrand<RcLazyConfig>, _, _, _, _>(|x: &i32| *x * 2, &lazy);
assert_eq!(*mapped.evaluate(), 20);

Modules§

alt
Dispatch for Alt::alt and RefAlt::ref_alt.
apply_first
Dispatch for ApplyFirst::apply_first and RefApplyFirst::ref_apply_first.
apply_second
Dispatch for ApplySecond::apply_second and RefApplySecond::ref_apply_second.
bifoldable
Dispatch for bifoldable operations: Bifoldable and RefBifoldable.
bifunctor
Dispatch for Bifunctor::bimap and RefBifunctor::ref_bimap.
bitraversable
Dispatch for Bitraversable::bi_traverse and RefBitraversable::ref_bi_traverse.
compactable
Dispatch for compactable operations: Compactable and RefCompactable.
contravariant
Dispatch for Contravariant::contramap.
filterable
Dispatch for filterable operations: Filterable and RefFilterable.
filterable_with_index
Dispatch for filterable-with-index operations: FilterableWithIndex and RefFilterableWithIndex.
foldable
Dispatch for Foldable::fold_right, fold_left, fold_map, and their by-reference counterparts in RefFoldable.
foldable_with_index
Dispatch for foldable-with-index operations: FoldableWithIndex and RefFoldableWithIndex.
functor
Dispatch for Functor::map and RefFunctor::ref_map.
functor_with_index
Dispatch for FunctorWithIndex::map_with_index and RefFunctorWithIndex::ref_map_with_index.
lift
Dispatch for Lift::lift2 through explicit::lift5, and their by-reference counterparts RefLift::ref_lift2 etc.
semimonad
Dispatch for semimonad operations: Semimonad and RefSemimonad.
traversable
Dispatch for Traversable::traverse and RefTraversable::ref_traverse.
traversable_with_index
Dispatch for TraversableWithIndex::traverse_with_index and RefTraversableWithIndex::ref_traverse_with_index.
witherable
Dispatch for witherable operations: Witherable and RefWitherable.

Structs§

Ref
Marker type indicating the closure receives references.
Val
Marker type indicating the closure receives owned values.

Traits§

ClosureMode
Trait that maps a closure mode marker (Val or Ref) to the corresponding dyn Fn trait object type.