Skip to main content

fn_traits/fns/
control_flow_break_fn.rs

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