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
34
35
use crate::ref_fn::RefFn;
use crate::ref_sync_fn::RefSyncFn;

/// A trait for defining a static function will be type erased.
pub trait StaticRefFunction<'a, D, T> {
    /// Return type of the defined function.
    type Output;

    /// Function definition.
    fn call(data: &'a D, arg: T) -> Self::Output;

    /// A helper function to create a [`RefFn`] object with the defined function.
    fn bind(data: &'a D) -> RefFn<'a, T, Self::Output> {
        RefFn::new::<Self, D>(data)
    }

    /// A helper function to create a [`RefSyncFn`] object with the defined function.
    fn bind_sync(data: &'a D) -> RefSyncFn<'a, T, Self::Output>
    where
        D: Sync,
    {
        RefSyncFn::new::<Self, D>(data)
    }
}

impl<'a, T, F, R> StaticRefFunction<'a, F, T> for F
where
    F: Fn(T) -> R,
{
    type Output = R;

    fn call(data: &'a Self, arg: T) -> Self::Output {
        (*data)(arg)
    }
}