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§

source

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.

source

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§

source

fn contramap<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(A) -> Self::Inner,

Returns an adapted version of Self with Contravariant::Inner replaced

This method uses an adaption function f: FnMut(A) -> B to replace Self::ContramapOut = B with A.

Provided Methods§

source

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

Implementations on Foreign Types§

source§

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

§

type Inner = B

§

type Mapped = Box<dyn FnOnce(A) -> R + 'a, Global>

source§

fn contramap<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(A) -> Self::Inner,

source§

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

§

type Inner = B

§

type Mapped = Box<dyn FnMut(A) -> R + 'a, Global>

source§

fn contramap<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(A) -> Self::Inner,

source§

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

§

type Inner = B

§

type Mapped = Box<dyn FnMut(A) -> R + Send + 'a, Global>

source§

fn contramap<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(A) -> Self::Inner,

source§

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

§

type Inner = B

§

type Mapped = Box<dyn FnOnce(A) -> R + Send + 'a, Global>

source§

fn contramap<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(A) -> Self::Inner,

Implementors§