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§
sourcetype Mapped<'b>: Functor<'b, B> + Identity<Self::Map<'b, B>>
where
B: 'b,
'a: 'b
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).
sourcetype Map<'b, C>
where
C: 'a,
'a: 'b
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§
Provided Methods§
sourcefn fmap_same<F>(self, f: F) -> Selfwhere
Self: FunctorSelf<'a, B>,
F: 'a + Fn(Self::Inner) -> Self::Inner,
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.