simple_ref_fn/
static_ref_function.rs

1use crate::ref_fn::RefFn;
2use crate::ref_sync_fn::RefSyncFn;
3
4/// A trait for defining a static function will be type erased.
5pub trait StaticRefFunction<'a, D, T> {
6    /// Return type of the defined function.
7    type Output;
8
9    /// Function definition.
10    fn call(data: &'a D, arg: T) -> Self::Output;
11
12    /// A helper function to create a [`RefFn`] object with the defined function.
13    fn bind(data: &'a D) -> RefFn<'a, T, Self::Output> {
14        RefFn::new::<Self, D>(data)
15    }
16
17    /// A helper function to create a [`RefSyncFn`] object with the defined function.
18    fn bind_sync(data: &'a D) -> RefSyncFn<'a, T, Self::Output>
19    where
20        D: Sync,
21    {
22        RefSyncFn::new::<Self, D>(data)
23    }
24}
25
26impl<'a, T, F, R> StaticRefFunction<'a, F, T> for F
27where
28    F: Fn(T) -> R,
29{
30    type Output = R;
31
32    fn call(data: &'a Self, arg: T) -> Self::Output {
33        (*data)(arg)
34    }
35}