Trait fmap::Contravariant
source · pub trait Contravariant<'a, A>where
Self: Sized,
A: 'a,{
type Inner: 'a;
type Mapped: Contravariant<'a, A, Inner = A, Mapped = Self::Mapped> + Contravariant<'a, Self::Inner, Inner = A, Mapped = Self>;
// Required method
fn contramap<F>(self, f: F) -> Self::Mapped
where F: 'a + Send + FnMut(A) -> Self::Inner;
// Provided method
fn contramap_fn_mutref<F>(self, f: F) -> Self
where Self: ContravariantSelf<'a, A>,
F: 'a + Send + FnMut(&mut Self::Inner) { ... }
}
Expand description
Contravariant functor
Examples
use fmap::Contravariant;
let mut output = String::new();
{
let mut string_printer: Box<dyn FnMut(String)> =
Box::new(|s| {
output.push_str(&s);
});
(string_printer)("Hello: ".to_string());
let mut int_printer: Box<dyn FnMut(i32)> =
string_printer.contramap(|n| format!("number {n}"));
(int_printer)(13);
}
assert_eq!(output, "Hello: number 13".to_string());
Required Associated Types§
sourcetype Inner: 'a
type Inner: 'a
Inner type
For any contravariant functor T<B>
, where values of type B
are
returned by the Contravariant::contramap
function, set
Inner = B
.
sourcetype Mapped: Contravariant<'a, A, Inner = A, Mapped = Self::Mapped> + Contravariant<'a, Self::Inner, Inner = A, Mapped = Self>
type Mapped: Contravariant<'a, A, Inner = A, Mapped = Self::Mapped> + Contravariant<'a, Self::Inner, Inner = A, Mapped = Self>
Self
but consuming A
instead of Contravariant::Inner
Required Methods§
Provided Methods§
sourcefn contramap_fn_mutref<F>(self, f: F) -> Selfwhere
Self: ContravariantSelf<'a, A>,
F: 'a + Send + FnMut(&mut Self::Inner),
fn contramap_fn_mutref<F>(self, f: F) -> Selfwhere Self: ContravariantSelf<'a, A>, F: 'a + Send + FnMut(&mut Self::Inner),
Same as Contravariant::contramap
but uses a mapping function that
takes a mutable reference