Trait fmap::Functor

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

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

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

A generic type (e.g. Vec<A>) whose inner type can be mapped over (e.g. to Vec<B>)

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

Required Associated Types§

source

type Inner: 'a

Inner type (e.g. Inner = A for Vec<A>)

source

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

Self with inner type mapped to a different type

For example, <Vec<A> as Functor<'a, B>>::Mapped<'b, C> = Vec<C>. It is required that T::Mapped<'a, T::Inner> = T (which is ensured by the compiler).

Required Methods§

source

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

Replaces inner type and value by applying a mapping function

Provided Methods§

source

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

Specialized variant of fmap where the inner type isn’t changed

Opposed to fmap, this method returns Self instead of Self::Mapped<B>, which can help reducing unnecessary trait bounds. Its default implementation may be overriden where a more efficient implementation is available when Functor<B>::Inner and B are the same types.

Implementations on Foreign Types§

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

source§

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

§

type Inner = A

§

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

source§

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

Implementors§