Trait Functor

Source
pub trait Functor<'a, B>
where Self: Sized, B: 'a,
{ type Inner: 'a; type Mapped: Functor<'a, B, Inner = B, Mapped = Self::Mapped> + Functor<'a, Self::Inner, Inner = B, Mapped = Self>; // Required method fn fmap<F>(self, f: F) -> Self::Mapped where F: 'a + Send + FnMut(Self::Inner) -> B; // Provided method fn fmap_fn_mutref<F>(self, f: F) -> Self where Self: FunctorSelf<'a, B>, F: 'a + Send + FnMut(&mut Self::Inner) { ... } }
Expand description

Generic type (e.g. T<A>) whose inner type can be mapped (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,
    B: 'a,
{
    type Inner = A;
    type Mapped = Option<B>;
    fn fmap<F>(self, f: F) -> Self::Mapped
    where
        F: 'a + Send + FnMut(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
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]);

Also see FunctorSelf.

Required Associated Types§

Source

type Inner: 'a

Inner type

For any functor T<A>, where values of type A are passed to the Functor::fmap function, set Inner = A.

Source

type Mapped: Functor<'a, B, Inner = B, Mapped = Self::Mapped> + Functor<'a, Self::Inner, Inner = B, Mapped = Self>

Self but with inner type mapped to B

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

Required Methods§

Source

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

Replaces inner type and value by applying a mapping function

Where Functor::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) -> Self
where Self: FunctorSelf<'a, B>, F: 'a + Send + FnMut(&mut Self::Inner),

Same as Functor::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 + Send + FnMut(&mut Self::Inner),
{
    self.fmap_mut(f);
    self
}

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.

Implementations on Foreign Types§

Source§

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

Source§

type Inner = A

Source§

type Mapped = Option<B>

Source§

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

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn Iterator<Item = B> + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn Iterator<Item = B> + Send + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnMut() -> B + Send + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnOnce() -> B + Send + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnMut() -> B + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnOnce() -> B + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = BinaryHeap<B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

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

Source§

type Inner = A

Source§

type Mapped = BTreeSet<B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

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

Source§

type Inner = A

Source§

type Mapped = LinkedList<B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Source§

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

Source§

type Inner = A

Source§

type Mapped = VecDeque<B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Source§

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

Source§

type Inner = A

Source§

type Mapped = Vec<B>

Source§

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

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Source§

impl<'a, A, B> Functor<'a, B> for Pin<Box<dyn Future<Output = A> + 'a>>
where A: 'a, B: 'a,

Source§

type Inner = A

Source§

type Mapped = Pin<Box<dyn Future<Output = B> + 'a>>

Source§

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

Source§

impl<'a, A, B> Functor<'a, B> for Pin<Box<dyn Future<Output = A> + Send + 'a>>
where A: 'a, B: 'a,

Source§

type Inner = A

Source§

type Mapped = Pin<Box<dyn Future<Output = B> + Send + 'a>>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = HashSet<B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

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

Source§

type Inner = A

Source§

type Mapped = Result<B, E>

Source§

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

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnMut(X) -> B + Send + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnOnce(X) -> B + Send + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnMut(X) -> B + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = Box<dyn FnOnce(X) -> B + 'a>

Source§

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

Source§

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

Source§

type Inner = A

Source§

type Mapped = BTreeMap<K, B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Source§

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

Source§

type Inner = A

Source§

type Mapped = HashMap<K, B>

Source§

fn fmap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> B,

Source§

fn fmap_fn_mutref<F>(self, f: F) -> Self
where F: 'a + Send + FnMut(&mut Self::Inner),

Implementors§