simple_ref_fn/
ref_fn_mut.rs

1use crate::static_ref_mut_function::StaticRefMutFunction;
2use core::marker::PhantomData;
3use core::ptr::NonNull;
4
5unsafe fn call_mut_fn<'a, F, D, T>(data: NonNull<()>, arg: T) -> F::Output
6where
7    F: StaticRefMutFunction<'a, D, T> + ?Sized,
8    D: 'a,
9{
10    F::call_mut(unsafe { data.cast().as_mut() }, arg)
11}
12
13/// A simple function wrapper that behaves like a [`&mut dyn FnMut(T) -> R`](`FnMut`) type, but does not require a
14/// virtual table.
15pub struct RefFnMut<'a, T, R> {
16    data: NonNull<()>,
17    call_mut_fn: unsafe fn(NonNull<()>, T) -> R,
18    _phantom: PhantomData<&'a mut ()>,
19}
20
21impl<'a, T, R> RefFnMut<'a, T, R> {
22    /// Create a [`RefFnMut`] object by binding [`F::call_mut`](`StaticRefMutFunction::call_mut`) function with `data`.
23    pub fn new<F, D>(data: &'a mut D) -> Self
24    where
25        F: StaticRefMutFunction<'a, D, T, Output = R> + ?Sized,
26    {
27        Self {
28            data: NonNull::from(data).cast(),
29            call_mut_fn: call_mut_fn::<'a, F, D, T>,
30            _phantom: PhantomData,
31        }
32    }
33
34    /// Create a [`RefFnMut`] object from a function reference.
35    pub fn from_fn_mut<F>(f: &'a mut F) -> Self
36    where
37        F: FnMut(T) -> R,
38    {
39        Self::new::<F, F>(f)
40    }
41
42    /// Call the wrapped function.
43    pub fn call_mut(&mut self, arg: T) -> R {
44        unsafe { (self.call_mut_fn)(self.data, arg) }
45    }
46}
47
48impl<'a, F, T, R> From<&'a mut F> for RefFnMut<'a, T, R>
49where
50    F: FnMut(T) -> R,
51{
52    fn from(value: &'a mut F) -> Self {
53        Self::from_fn_mut(value)
54    }
55}
56
57/// Unconditionally `Sync` because this type only provides mutable access, so sharing non-mutable reference is safe.
58unsafe impl<T, R> Sync for RefFnMut<'_, T, R> {}
59
60#[cfg(test)]
61mod tests {
62    use super::RefFnMut;
63    use crate::static_ref_mut_function::StaticRefMutFunction;
64
65    static_assertions::assert_impl_all!(RefFnMut<'static, (), ()>: From<&'static mut fn(())>, Sync);
66    static_assertions::assert_not_impl_any!(RefFnMut<'static, (), ()>: Send);
67
68    #[test]
69    fn test_ref_fn_mut_new() {
70        struct F;
71
72        impl StaticRefMutFunction<'_, u32, u32> for F {
73            type Output = u32;
74
75            fn call_mut(data: &mut u32, arg: u32) -> Self::Output {
76                let old_value = *data;
77
78                *data += arg;
79
80                old_value
81            }
82        }
83
84        let mut data = 2;
85        let mut f: RefFnMut<u32, u32> = F::bind(&mut data);
86
87        assert_eq!(f.call_mut(3), 2);
88        assert_eq!(f.call_mut(5), 5);
89        assert_eq!(f.call_mut(7), 10);
90
91        assert_eq!(data, 17);
92    }
93
94    #[test]
95    fn test_ref_fn_mut_from() {
96        let mut data = 2_u32;
97
98        let mut closure = |arg: u32| {
99            let old_value = data;
100
101            data += arg;
102
103            old_value
104        };
105
106        let mut f: RefFnMut<u32, u32> = RefFnMut::from(&mut closure);
107
108        assert_eq!(f.call_mut(3), 2);
109        assert_eq!(f.call_mut(5), 5);
110        assert_eq!(f.call_mut(7), 10);
111
112        assert_eq!(data, 17);
113    }
114}