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 {
59            Self::Continue
60        } else {
61            Self::Break
62        }
63    }
64}
65
66impl From<ControlFlow> for bool {
67    fn from(c: ControlFlow) -> Self {
68        match c {
69            ControlFlow::Break => false,
70            ControlFlow::Continue => true,
71        }
72    }
73}
74
75#[doc(hidden)]
76impl IntoGlib for ControlFlow {
77    type GlibType = ffi::gboolean;
78
79    #[inline]
80    fn into_glib(self) -> ffi::gboolean {
81        bool::from(self).into_glib()
82    }
83}
84
85#[doc(hidden)]
86impl FromGlib<ffi::gboolean> for ControlFlow {
87    #[inline]
88    unsafe fn from_glib(value: ffi::gboolean) -> Self {
89        bool::from_glib(value).into()
90    }
91}
92
93impl crate::value::ToValue for ControlFlow {
94    fn to_value(&self) -> crate::Value {
95        bool::from(*self).to_value()
96    }
97
98    fn value_type(&self) -> crate::Type {
99        <bool as StaticType>::static_type()
100    }
101}
102
103impl From<ControlFlow> for crate::Value {
104    #[inline]
105    fn from(v: ControlFlow) -> Self {
106        bool::from(v).into()
107    }
108}