Trait fmap::Functor

source ·
pub trait Functor<'a, B> {
    type Inner: 'a;
    type Mapped<'b>
       where B: 'b,
             'a: 'b;

    // Required method
    fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>
       where B: 'b,
             F: 'b + Fn(Self::Inner) -> B,
             'a: 'b;

    // Provided method
    fn fmap_fn_mutref<F>(self, f: F) -> Self
       where Self: FunctorSelf<'a, B>,
             B: 'a,
             F: 'a + Fn(&mut Self::Inner) { ... }
}
Expand description

Generic type (e.g. T<A>) whose inner type can be mapped over (e.g. resulting in T<B>)

Type parameter B specifies the new inner type after the fmap operation.

Examples

Implementing Functor

impl<'a, A, B> Functor<'a, B> for Option<A>
where
    A: 'a,
{
    type Inner = A;
    type Mapped<'b> = Option<B>
    where
        'a: 'b,
        B: 'b;
    fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>
    where
        'a: 'b,
        B: 'b,
        F: 'b + Fn(Self::Inner) -> B,
    {
        self.map(f)
    }
}

Using Functor::fmap

use fmap::Functor;

let ok: Result<i32, i32> = Ok(2);
assert_eq!(ok.fmap(|x| x + 1), Ok(3));

let err: Result<i32, i32> = Err(0);
assert_eq!(err.fmap(|x| x + 1), Err(0));

let int_vec: Vec<i32> = vec![2, 3, 5];
let float_vec: Vec<f64> = int_vec.fmap(Into::into);
assert_eq!(float_vec, vec![2.0, 3.0, 5.0]);

fn convert_inner<'a, T, A, B>(outer: T) -> T::Mapped<'a>
where
    // NOTE: `A` and `B` can be different types. Where `A` and `B`
    // are always the same type, `FunctorSelf` should be used.
    T: Functor<'a, B, Inner = A>,
    A: 'a + Into<B>,
{
    outer.fmap(Into::into)
}

let int_vec2: Vec<i32> = vec![7, 11, 13];
let float_vec2: Vec<f64> = convert_inner(int_vec2);
assert_eq!(float_vec2, vec![7.0, 11.0, 13.0]);

Required Associated Types§

source

type Inner: 'a

Inner type (before mapping)

For any functor T, define like: <T<A> as Functor<'a, B>>::Inner = A.

source

type Mapped<'b> where B: 'b, 'a: 'b

Self but with inner type mapped to B

For any lifetime-free functor T, define like: <T<A> as Functor<'a, B>>::Mapped<'b> = T<B>.

If T has a lifetime parameter, then define like: <T<'a, A> as Functor<'a, B>>::Mapped<'b> = T<'b, B>. This allows to shorten the lifetime after lazy mapping operations where the mapping closure needs to live at least as long as 'b.

Required Methods§

source

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

Replaces inner type and value by applying a mapping function

Where Self::Inner and B are the same type, consider using Functor::fmap_fn_mutref or FunctorMut::fmap_mut, which might provide specialized implementations that are more efficient.

Provided Methods§

source

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere Self: FunctorSelf<'a, B>, B: 'a, F: 'a + Fn(&mut Self::Inner),

Same as fmap but uses a mapping function that takes a mutable reference

This method has a default implementation that can be overridden if there is a more efficient way of mapping inner values in place. See also FunctorMut::fmap_mut, which works on &mut self.

For types which implement FunctorMut and where fmap_mut’s implementation doesn’t use fmap_fn_mutref, consider to provide the following implementation:

fn fmap_fn_mutref<F>(mut self, f: F) -> Self
where
    F: 'a + Fn(&mut Self::Inner),
{
    self.fmap_mut(f);
    self
}

Implementations on Foreign Types§

source§

impl<'a, A, B, E> Functor<'a, B> for Result<A, E>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Result<B, E>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, A, B> Functor<'a, B> for HashSet<A>where A: Eq + Hash + 'a, B: Eq + Hash,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = HashSet<B, RandomState>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for Vec<A>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Vec<B, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, A, B, X> Functor<'a, B> for Box<dyn FnMut(X) -> A + 'a>where A: 'a, X: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn FnMut(X) -> B + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for Box<dyn FnOnce() -> A + 'a>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn FnOnce() -> B + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B, X> Functor<'a, B> for Box<dyn Fn(X) -> A + 'a>where A: 'a, X: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn Fn(X) -> B + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for Box<dyn FnMut() -> A + 'a>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn FnMut() -> B + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for Option<A>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Option<B>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, K, A, B> Functor<'a, B> for BTreeMap<K, A>where K: Ord, A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = BTreeMap<K, B, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, A, B> Functor<'a, B> for LinkedList<A>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = LinkedList<B, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, A, B> Functor<'a, B> for BTreeSet<A>where A: Ord + 'a, B: Ord,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = BTreeSet<B, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for VecDeque<A>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = VecDeque<B, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, A, B> Functor<'a, B> for BinaryHeap<A>where A: Ord + 'a, B: Ord,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = BinaryHeap<B>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for Box<dyn Iterator<Item = A> + 'a>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn Iterator<Item = B> + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, A, B> Functor<'a, B> for Box<dyn Fn() -> A + 'a>where A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn Fn() -> B + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

impl<'a, K, A, B> Functor<'a, B> for HashMap<K, A>where K: Eq + Hash, A: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = HashMap<K, B, RandomState>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

source§

fn fmap_fn_mutref<F>(self, f: F) -> Selfwhere F: 'a + Fn(&mut Self::Inner),

source§

impl<'a, A, B, X> Functor<'a, B> for Box<dyn FnOnce(X) -> A + 'a>where A: 'a, X: 'a,

§

type Inner = A

§

type Mapped<'b> where B: 'b, 'a: 'b = Box<dyn FnOnce(X) -> B + 'b, Global>

source§

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b>where B: 'b, F: 'b + Fn(Self::Inner) -> B, 'a: 'b,

Implementors§