glib/
control_flow.rs

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