Skip to main content

core/
bool.rs

1//! impl bool {}
2
3use crate::marker::Destruct;
4
5impl bool {
6    /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
7    /// or `None` otherwise.
8    ///
9    /// Arguments passed to `then_some` are eagerly evaluated; if you are
10    /// passing the result of a function call, it is recommended to use
11    /// [`then`], which is lazily evaluated.
12    ///
13    /// [`then`]: bool::then
14    ///
15    /// # Examples
16    ///
17    /// ```
18    /// assert_eq!(false.then_some(0), None);
19    /// assert_eq!(true.then_some(0), Some(0));
20    /// ```
21    ///
22    /// ```
23    /// let mut a = 0;
24    /// let mut function_with_side_effects = || { a += 1; };
25    ///
26    /// true.then_some(function_with_side_effects());
27    /// false.then_some(function_with_side_effects());
28    ///
29    /// // `a` is incremented twice because the value passed to `then_some` is
30    /// // evaluated eagerly.
31    /// assert_eq!(a, 2);
32    /// ```
33    #[stable(feature = "bool_to_option", since = "1.62.0")]
34    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
35    #[inline]
36    #[ferrocene::prevalidated]
37    pub const fn then_some<T: [const] Destruct>(self, t: T) -> Option<T> {
38        if self { Some(t) } else { None }
39    }
40
41    /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
42    /// or `None` otherwise.
43    ///
44    /// # Examples
45    ///
46    /// ```
47    /// assert_eq!(false.then(|| 0), None);
48    /// assert_eq!(true.then(|| 0), Some(0));
49    /// ```
50    ///
51    /// ```
52    /// let mut a = 0;
53    ///
54    /// true.then(|| { a += 1; });
55    /// false.then(|| { a += 1; });
56    ///
57    /// // `a` is incremented once because the closure is evaluated lazily by
58    /// // `then`.
59    /// assert_eq!(a, 1);
60    /// ```
61    #[doc(alias = "then_with")]
62    #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
63    #[rustc_diagnostic_item = "bool_then"]
64    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
65    #[inline]
66    #[ferrocene::prevalidated]
67    pub const fn then<T, F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> Option<T> {
68        if self { Some(f()) } else { None }
69    }
70
71    /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
72    /// or `Err(err)` otherwise.
73    ///
74    /// Arguments passed to `ok_or` are eagerly evaluated; if you are
75    /// passing the result of a function call, it is recommended to use
76    /// [`ok_or_else`], which is lazily evaluated.
77    ///
78    /// [`ok_or_else`]: bool::ok_or_else
79    ///
80    /// # Examples
81    ///
82    /// ```
83    /// #![feature(bool_to_result)]
84    ///
85    /// assert_eq!(false.ok_or(0), Err(0));
86    /// assert_eq!(true.ok_or(0), Ok(()));
87    /// ```
88    ///
89    /// ```
90    /// #![feature(bool_to_result)]
91    ///
92    /// let mut a = 0;
93    /// let mut function_with_side_effects = || { a += 1; };
94    ///
95    /// assert!(true.ok_or(function_with_side_effects()).is_ok());
96    /// assert!(false.ok_or(function_with_side_effects()).is_err());
97    ///
98    /// // `a` is incremented twice because the value passed to `ok_or` is
99    /// // evaluated eagerly.
100    /// assert_eq!(a, 2);
101    /// ```
102    #[unstable(feature = "bool_to_result", issue = "142748")]
103    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
104    #[inline]
105    #[ferrocene::prevalidated]
106    pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E> {
107        if self { Ok(()) } else { Err(err) }
108    }
109
110    /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
111    /// or `Err(f())` otherwise.
112    ///
113    /// # Examples
114    ///
115    /// ```
116    /// #![feature(bool_to_result)]
117    ///
118    /// assert_eq!(false.ok_or_else(|| 0), Err(0));
119    /// assert_eq!(true.ok_or_else(|| 0), Ok(()));
120    /// ```
121    ///
122    /// ```
123    /// #![feature(bool_to_result)]
124    ///
125    /// let mut a = 0;
126    ///
127    /// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
128    /// assert!(false.ok_or_else(|| { a += 1; }).is_err());
129    ///
130    /// // `a` is incremented once because the closure is evaluated lazily by
131    /// // `ok_or_else`.
132    /// assert_eq!(a, 1);
133    /// ```
134    #[unstable(feature = "bool_to_result", issue = "142748")]
135    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
136    #[inline]
137    #[ferrocene::prevalidated]
138    pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>(
139        self,
140        f: F,
141    ) -> Result<(), E> {
142        if self { Ok(()) } else { Err(f()) }
143    }
144}