Trait 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::Mapped
where 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) -> Self
where 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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

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

Source§

type Inner = B

Source§

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

Source§

fn contramap<F>(self, f: F) -> Self::Mapped
where 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,

Source§

type Inner = B

Source§

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

Source§

fn contramap<F>(self, f: F) -> Self::Mapped
where 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,

Source§

type Inner = B

Source§

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

Source§

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

Source§

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

Source§

type Inner = B

Source§

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

Source§

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

Implementors§