1use crate::{Fn, FnMut, FnOnce};
2use core::marker::PhantomData;
3
4#[derive(Clone, Copy, Default)]
6pub struct CopyFn {
7 _phantom: PhantomData<()>,
8}
9
10impl<'a, T> FnOnce<(&'a T,)> for CopyFn
11where
12 T: Copy,
13{
14 type Output = T;
15
16 fn call_once(self, args: (&'a T,)) -> Self::Output {
17 *args.0
18 }
19}
20
21impl<'a, T> FnMut<(&'a T,)> for CopyFn
22where
23 T: Copy,
24{
25 type Output = T;
26
27 fn call_mut(&mut self, args: (&'a T,)) -> Self::Output {
28 self.call_once(args)
29 }
30}
31
32impl<'a, T> Fn<(&'a T,)> for CopyFn
33where
34 T: Copy,
35{
36 type Output = T;
37
38 fn call(&self, args: (&'a T,)) -> Self::Output {
39 self.call_once(args)
40 }
41}
42
43impl<'a, T> FnOnce<(&'a mut T,)> for CopyFn
44where
45 T: Copy,
46{
47 type Output = T;
48
49 fn call_once(self, args: (&'a mut T,)) -> Self::Output {
50 self.call_once((&*args.0,))
51 }
52}
53
54impl<'a, T> FnMut<(&'a mut T,)> for CopyFn
55where
56 T: Copy,
57{
58 type Output = T;
59
60 fn call_mut(&mut self, args: (&'a mut T,)) -> Self::Output {
61 self.call_once(args)
62 }
63}
64
65impl<'a, T> Fn<(&'a mut T,)> for CopyFn
66where
67 T: Copy,
68{
69 type Output = T;
70
71 fn call(&self, args: (&'a mut T,)) -> Self::Output {
72 self.call_once(args)
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::super::tests::{into_std_fn, into_std_fn_mut, into_std_fn_once};
79 use super::CopyFn;
80
81 #[test]
82 fn test_copy_fn() {
83 let f = CopyFn::default();
84 let mut x = 2;
85
86 assert_eq!(into_std_fn_once(Clone::clone(&f))(&x), 2);
87 assert_eq!(into_std_fn_mut(Clone::clone(&f))(&x), 2);
88 assert_eq!(into_std_fn(Clone::clone(&f))(&x), 2);
89
90 assert_eq!(into_std_fn_once(Clone::clone(&f))(&mut x), 2);
91 assert_eq!(into_std_fn_mut(Clone::clone(&f))(&mut x), 2);
92 assert_eq!(into_std_fn(Clone::clone(&f))(&mut x), 2);
93 }
94}