summaryrefslogtreecommitdiffstats
path: root/core/src/closure.rs
blob: dc7b4fee2915fcb416a2076be4afae57337b0951 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Box closures with ease.
//!
//! These are just a bunch of types that wrap boxed closures with
//! blanket [`From`] implementations for easy conversions.
//!
//! Mainly, it allows functions to take `Into<T>` where `T` may end
//! up being a boxed closure.

/// A boxed closure that takes `A` by reference and produces `O`.
#[allow(missing_debug_implementations)]
pub struct Unary<'a, A, O>(pub Box<dyn Fn(&A) -> O + 'a>);

impl<'a, A, O, T> From<T> for Unary<'a, A, O>
where
    T: Fn(&A) -> O + 'a,
{
    fn from(f: T) -> Self {
        Self(Box::new(f))
    }
}

/// A boxed closure that takes `A` by reference and `B` by value and produces `O`.
#[allow(missing_debug_implementations)]
pub struct Binary<'a, A, B, O>(pub Box<dyn Fn(&A, B) -> O + 'a>);

impl<'a, A, B, O, T> From<T> for Binary<'a, A, B, O>
where
    T: Fn(&A, B) -> O + 'a,
{
    fn from(f: T) -> Self {
        Self(Box::new(f))
    }
}