Trait fmap::Functor

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

    // Required method
    fn fmap<'b, F>(self, f: F) -> Self::Mapped<'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>: Functor<'b, B> + Identity<Self::Map<'b, B>> where B: 'b, 'a: 'b

Self with inner type mapped to B (where B is a type parameter of Functor<'a, B>)

Both Functor::Mapped and Functor::Map are associated types being the same as Self but with the inner type (Functor::Inner) changed. Both Functor::Mapped and Functor::Map must be implemented consistent with each other.

This associated type (Mapped) replaces the inner type with the type parameter B of the trait. For example, <Vec<A> as Functor<'a, B>>::Mapped<'b> = Vec<B>.

When B is Self::Inner, then Mapped<'a> must be Self (which is ensured by the compiler).

source

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

Self with inner type mapped to C (where C is a type parameter of this GAT)

Both Functor::Mapped and Functor::Map are associated types being the same as Self but with the inner type (Functor::Inner) changed. Both Functor::Mapped and Functor::Map must be implemented consistent with each other.

This generic associated type (Map) replaces the inner type with a type parameter C that is given to this generic associated type. For example, <Vec<A> as Functor<'a, B>>::Map<'b, C> = Vec<C>.

Map<'a, Self::Inner> must be Self (which is ensured by the compiler).

Required Methods§

source

fn fmap<'b, F>(self, f: F) -> Self::Mapped<'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, 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, A, B> Functor<'a, B> for Box<dyn Iterator<Item = A> + 'a>where A: 'a, B: 'a,

§

type Inner = A

§

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

§

type Map<'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>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> where B: 'b, 'a: 'b = HashSet<B, RandomState>

§

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

source§

fn fmap<'b, F>(self, f: F) -> HashSet<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> where B: 'b, 'a: 'b = Option<B>

§

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

source§

fn fmap<'b, F>(self, f: F) -> Option<B>where F: Fn(A) -> B + '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> where B: 'b, 'a: 'b = VecDeque<B, Global>

§

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

source§

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

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> where B: 'b, 'a: 'b = HashMap<K, B, RandomState>

§

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

source§

fn fmap<'b, F>(self, f: F) -> HashMap<K, 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> where B: 'b, 'a: 'b = LinkedList<B, Global>

§

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

source§

fn fmap<'b, F>(self, f: F) -> LinkedList<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> where B: 'b, 'a: 'b = Vec<B, Global>

§

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

source§

fn fmap<'b, F>(self, f: F) -> Vec<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> where B: 'b, 'a: 'b = Result<B, E>

§

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

source§

fn fmap<'b, F>(self, f: F) -> Result<B, E>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> where B: 'b, 'a: 'b = BinaryHeap<B>

§

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

source§

fn fmap<'b, F>(self, f: F) -> BinaryHeap<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> where B: 'b, 'a: 'b = BTreeSet<B, Global>

§

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

source§

fn fmap<'b, F>(self, f: F) -> BTreeSet<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> where B: 'b, 'a: 'b = BTreeMap<K, B, Global>

§

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

source§

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

Implementors§