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 /// assert_eq!(false.ok_or(0), Err(0));
84 /// assert_eq!(true.ok_or(0), Ok(()));
85 /// ```
86 ///
87 /// ```
88 /// let mut a = 0;
89 /// let mut function_with_side_effects = || { a += 1; };
90 ///
91 /// assert!(true.ok_or(function_with_side_effects()).is_ok());
92 /// assert!(false.ok_or(function_with_side_effects()).is_err());
93 ///
94 /// // `a` is incremented twice because the value passed to `ok_or` is
95 /// // evaluated eagerly.
96 /// assert_eq!(a, 2);
97 /// ```
98 #[stable(feature = "bool_to_result", since = "CURRENT_RUSTC_VERSION")]
99 #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
100 #[inline]
101 #[ferrocene::prevalidated]
102 pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E> {
103 if self { Ok(()) } else { Err(err) }
104 }
105
106 /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
107 /// or `Err(f())` otherwise.
108 ///
109 /// # Examples
110 ///
111 /// ```
112 /// assert_eq!(false.ok_or_else(|| 0), Err(0));
113 /// assert_eq!(true.ok_or_else(|| 0), Ok(()));
114 /// ```
115 ///
116 /// ```
117 /// let mut a = 0;
118 ///
119 /// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
120 /// assert!(false.ok_or_else(|| { a += 1; }).is_err());
121 ///
122 /// // `a` is incremented once because the closure is evaluated lazily by
123 /// // `ok_or_else`.
124 /// assert_eq!(a, 1);
125 /// ```
126 #[stable(feature = "bool_to_result", since = "CURRENT_RUSTC_VERSION")]
127 #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
128 #[inline]
129 #[ferrocene::prevalidated]
130 pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>(
131 self,
132 f: F,
133 ) -> Result<(), E> {
134 if self { Ok(()) } else { Err(f()) }
135 }
136}