Skip to main content

fn_traits/fns/
control_flow_continue_fn.rs

1use crate::{Fn, FnMut, FnOnce};
2use core::marker::PhantomData;
3use core::ops::ControlFlow;
4
5/// [`ControlFlow::Continue`] function.
6pub struct ControlFlowContinueFn<B> {
7    phantom: PhantomData<fn() -> B>,
8}
9
10impl<B> Clone for ControlFlowContinueFn<B> {
11    fn clone(&self) -> Self {
12        *self
13    }
14}
15
16impl<B> Copy for ControlFlowContinueFn<B> {}
17
18impl<B> Default for ControlFlowContinueFn<B> {
19    fn default() -> Self {
20        Self { phantom: PhantomData }
21    }
22}
23
24impl<C, B> FnOnce<(C,)> for ControlFlowContinueFn<B> {
25    type Output = ControlFlow<B, C>;
26
27    fn call_once(self, args: (C,)) -> Self::Output {
28        ControlFlow::Continue(args.0)
29    }
30}
31
32impl<C, B> FnMut<(C,)> for ControlFlowContinueFn<B> {
33    type Output = ControlFlow<B, C>;
34
35    fn call_mut(&mut self, args: (C,)) -> Self::Output {
36        self.call_once(args)
37    }
38}
39
40impl<C, B> Fn<(C,)> for ControlFlowContinueFn<B> {
41    type Output = ControlFlow<B, C>;
42
43    fn call(&self, args: (C,)) -> Self::Output {
44        self.call_once(args)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::super::tests::{into_std_fn, into_std_fn_mut, into_std_fn_once};
51    use super::ControlFlowContinueFn;
52    use core::ops::ControlFlow;
53
54    #[test]
55    fn test_control_flow_continue_fn() {
56        let f = ControlFlowContinueFn::<()>::default();
57
58        assert_eq!(into_std_fn_once(Clone::clone(&f))(2), ControlFlow::Continue(2));
59        assert_eq!(into_std_fn_mut(Clone::clone(&f))(2), ControlFlow::Continue(2));
60        assert_eq!(into_std_fn(Clone::clone(&f))(2), ControlFlow::Continue(2));
61    }
62}