core/bool.rs
1//! impl bool {}
2
3use crate::intrinsics;
4use crate::marker::Destruct;
5use crate::ub_checks::assert_unsafe_precondition;
6
7impl bool {
8 /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
9 /// or `None` otherwise.
10 ///
11 /// Arguments passed to `then_some` are eagerly evaluated; if you are
12 /// passing the result of a function call, it is recommended to use
13 /// [`then`], which is lazily evaluated.
14 ///
15 /// [`then`]: bool::then
16 ///
17 /// # Examples
18 ///
19 /// ```
20 /// assert_eq!(false.then_some(0), None);
21 /// assert_eq!(true.then_some(0), Some(0));
22 /// ```
23 ///
24 /// ```
25 /// let mut a = 0;
26 /// let mut function_with_side_effects = || { a += 1; };
27 ///
28 /// true.then_some(function_with_side_effects());
29 /// false.then_some(function_with_side_effects());
30 ///
31 /// // `a` is incremented twice because the value passed to `then_some` is
32 /// // evaluated eagerly.
33 /// assert_eq!(a, 2);
34 /// ```
35 #[stable(feature = "bool_to_option", since = "1.62.0")]
36 #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
37 #[inline]
38 #[ferrocene::prevalidated]
39 pub const fn then_some<T: [const] Destruct>(self, t: T) -> Option<T> {
40 if self { Some(t) } else { None }
41 }
42
43 /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
44 /// or `None` otherwise.
45 ///
46 /// # Examples
47 ///
48 /// ```
49 /// assert_eq!(false.then(|| 0), None);
50 /// assert_eq!(true.then(|| 0), Some(0));
51 /// ```
52 ///
53 /// ```
54 /// let mut a = 0;
55 ///
56 /// true.then(|| { a += 1; });
57 /// false.then(|| { a += 1; });
58 ///
59 /// // `a` is incremented once because the closure is evaluated lazily by
60 /// // `then`.
61 /// assert_eq!(a, 1);
62 /// ```
63 #[doc(alias = "then_with")]
64 #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
65 #[rustc_diagnostic_item = "bool_then"]
66 #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
67 #[inline]
68 #[ferrocene::prevalidated]
69 pub const fn then<T, F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> Option<T> {
70 if self { Some(f()) } else { None }
71 }
72
73 /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
74 /// or `Err(err)` otherwise.
75 ///
76 /// Arguments passed to `ok_or` are eagerly evaluated; if you are
77 /// passing the result of a function call, it is recommended to use
78 /// [`ok_or_else`], which is lazily evaluated.
79 ///
80 /// [`ok_or_else`]: bool::ok_or_else
81 ///
82 /// # Examples
83 ///
84 /// ```
85 /// assert_eq!(false.ok_or(0), Err(0));
86 /// assert_eq!(true.ok_or(0), Ok(()));
87 /// ```
88 ///
89 /// ```
90 /// let mut a = 0;
91 /// let mut function_with_side_effects = || { a += 1; };
92 ///
93 /// assert!(true.ok_or(function_with_side_effects()).is_ok());
94 /// assert!(false.ok_or(function_with_side_effects()).is_err());
95 ///
96 /// // `a` is incremented twice because the value passed to `ok_or` is
97 /// // evaluated eagerly.
98 /// assert_eq!(a, 2);
99 /// ```
100 #[stable(feature = "bool_to_result", since = "1.98.0")]
101 #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
102 #[inline]
103 #[ferrocene::prevalidated]
104 pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E> {
105 if self { Ok(()) } else { Err(err) }
106 }
107
108 /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
109 /// or `Err(f())` otherwise.
110 ///
111 /// # Examples
112 ///
113 /// ```
114 /// assert_eq!(false.ok_or_else(|| 0), Err(0));
115 /// assert_eq!(true.ok_or_else(|| 0), Ok(()));
116 /// ```
117 ///
118 /// ```
119 /// let mut a = 0;
120 ///
121 /// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
122 /// assert!(false.ok_or_else(|| { a += 1; }).is_err());
123 ///
124 /// // `a` is incremented once because the closure is evaluated lazily by
125 /// // `ok_or_else`.
126 /// assert_eq!(a, 1);
127 /// ```
128 #[stable(feature = "bool_to_result", since = "1.98.0")]
129 #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
130 #[inline]
131 #[ferrocene::prevalidated]
132 pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>(
133 self,
134 f: F,
135 ) -> Result<(), E> {
136 if self { Ok(()) } else { Err(f()) }
137 }
138
139 /// Toggles `self` in-place.
140 ///
141 /// - If `self` is [`true`], sets `self` to [`false`].
142 /// - If `self` is [`false`], sets `self` to [`true`].
143 ///
144 /// Equivalent to `value = !value`.
145 ///
146 /// # Examples
147 ///
148 /// ```
149 /// let mut boolean = false;
150 ///
151 /// boolean.toggle();
152 /// assert_eq!(boolean, true);
153 ///
154 /// boolean.toggle();
155 /// assert_eq!(boolean, false);
156 /// ```
157 ///
158 /// [`true`]: ../std/keyword.true.html
159 /// [`false`]: ../std/keyword.false.html
160 #[stable(feature = "bool_toggle", since = "1.100.0")]
161 #[rustc_const_stable(feature = "bool_toggle", since = "1.100.0")]
162 #[inline]
163 pub const fn toggle(&mut self) {
164 *self = !*self;
165 }
166
167 /// Disjoint, bitwise or. Computes `self | rhs`, assuming at most one, true value.
168 ///
169 /// Practically, this requires that `self | rhs` and `self ^ rhs` both yield the
170 /// same result, allowing for any of the two to be emitted in codegen -- depending
171 /// on whichever is cheapest.
172 ///
173 /// # Examples
174 ///
175 /// ```
176 /// #![feature(disjoint_bitor)]
177 ///
178 /// assert_eq!(
179 /// // SAFETY: Only one, true value is passed.
180 /// unsafe { false.unchecked_disjoint_bitor(true) },
181 /// true,
182 /// );
183 ///
184 /// assert_eq!(
185 /// // SAFETY: No true values are passed.
186 /// unsafe { false.unchecked_disjoint_bitor(false) },
187 /// false,
188 /// );
189 /// ```
190 ///
191 /// # Safety
192 ///
193 /// This results in undefined behaviour if both `self` and `rhs` are true.
194 #[unstable(feature = "disjoint_bitor", issue = "135758")]
195 #[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")]
196 #[must_use = "this returns the result of the operation, \
197 without modifying the original"]
198 #[inline]
199 pub const unsafe fn unchecked_disjoint_bitor(self, rhs: Self) -> Self {
200 assert_unsafe_precondition!(
201 check_language_ub,
202 "bool::unchecked_disjoint_bitor cannot bitor two truths",
203 (
204 lhs: bool = self,
205 rhs: bool = rhs,
206 ) => !(lhs & rhs),
207 );
208
209 // SAFETY: Same precondition.
210 unsafe { intrinsics::disjoint_bitor(self, rhs) }
211 }
212}