simple_ref_fn/
static_ref_mut_function.rs

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