1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use crate::translate::*; |
4 | |
5 | // rustdoc-stripper-ignore-next |
6 | /// Continue calling the closure in the future iterations or drop it. |
7 | /// |
8 | /// This is the return type of `idle_add` and `timeout_add` closures. |
9 | /// |
10 | /// `ControlFlow::Continue` keeps the closure assigned, to be rerun when appropriate. |
11 | /// |
12 | /// `ControlFlow::Break` disconnects and drops it. |
13 | #[derive (Copy, Clone, Debug, PartialEq, Eq)] |
14 | pub enum ControlFlow { |
15 | Continue, |
16 | Break, |
17 | } |
18 | |
19 | impl ControlFlow { |
20 | // rustdoc-stripper-ignore-next |
21 | /// Returns `true` if this is a `Continue` variant. |
22 | pub fn is_continue(&self) -> bool { |
23 | matches!(self, Self::Continue) |
24 | } |
25 | |
26 | // rustdoc-stripper-ignore-next |
27 | /// Returns `true` if this is a `Break` variant. |
28 | pub fn is_break(&self) -> bool { |
29 | matches!(self, Self::Break) |
30 | } |
31 | } |
32 | |
33 | impl From<std::ops::ControlFlow<()>> for ControlFlow { |
34 | fn from(c: std::ops::ControlFlow<()>) -> Self { |
35 | match c { |
36 | std::ops::ControlFlow::Break(_) => Self::Break, |
37 | std::ops::ControlFlow::Continue(_) => Self::Continue, |
38 | } |
39 | } |
40 | } |
41 | |
42 | impl From<ControlFlow> for std::ops::ControlFlow<()> { |
43 | fn from(c: ControlFlow) -> Self { |
44 | match c { |
45 | ControlFlow::Break => Self::Break(()), |
46 | ControlFlow::Continue => Self::Continue(()), |
47 | } |
48 | } |
49 | } |
50 | |
51 | impl From<bool> for ControlFlow { |
52 | fn from(c: bool) -> Self { |
53 | if c { |
54 | Self::Continue |
55 | } else { |
56 | Self::Break |
57 | } |
58 | } |
59 | } |
60 | |
61 | impl From<ControlFlow> for bool { |
62 | fn from(c: ControlFlow) -> Self { |
63 | match c { |
64 | ControlFlow::Break => false, |
65 | ControlFlow::Continue => true, |
66 | } |
67 | } |
68 | } |
69 | |
70 | #[doc (hidden)] |
71 | impl IntoGlib for ControlFlow { |
72 | type GlibType = ffi::gboolean; |
73 | |
74 | #[inline ] |
75 | fn into_glib(self) -> ffi::gboolean { |
76 | bool::from(self).into_glib() |
77 | } |
78 | } |
79 | |
80 | #[doc (hidden)] |
81 | impl FromGlib<ffi::gboolean> for ControlFlow { |
82 | #[inline ] |
83 | unsafe fn from_glib(value: ffi::gboolean) -> Self { |
84 | bool::from_glib(val:value).into() |
85 | } |
86 | } |
87 | |
88 | impl crate::ToValue for ControlFlow { |
89 | fn to_value(&self) -> crate::Value { |
90 | bool::from(*self).to_value() |
91 | } |
92 | |
93 | fn value_type(&self) -> crate::Type { |
94 | <bool as crate::StaticType>::static_type() |
95 | } |
96 | } |
97 | |
98 | impl From<ControlFlow> for crate::Value { |
99 | #[inline ] |
100 | fn from(v: ControlFlow) -> Self { |
101 | bool::from(v).into() |
102 | } |
103 | } |
104 | |