summaryrefslogtreecommitdiffstats
path: root/library/core/src/bool.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /library/core/src/bool.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/core/src/bool.rs')
-rw-r--r--library/core/src/bool.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs
new file mode 100644
index 000000000..f7a8aa0d9
--- /dev/null
+++ b/library/core/src/bool.rs
@@ -0,0 +1,44 @@
+//! impl bool {}
+
+use crate::marker::Destruct;
+
+impl bool {
+ /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
+ /// or `None` otherwise.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert_eq!(false.then_some(0), None);
+ /// assert_eq!(true.then_some(0), Some(0));
+ /// ```
+ #[stable(feature = "bool_to_option", since = "1.62.0")]
+ #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
+ #[inline]
+ pub const fn then_some<T>(self, t: T) -> Option<T>
+ where
+ T: ~const Destruct,
+ {
+ if self { Some(t) } else { None }
+ }
+
+ /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
+ /// or `None` otherwise.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert_eq!(false.then(|| 0), None);
+ /// assert_eq!(true.then(|| 0), Some(0));
+ /// ```
+ #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
+ #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
+ #[inline]
+ pub const fn then<T, F>(self, f: F) -> Option<T>
+ where
+ F: ~const FnOnce() -> T,
+ F: ~const Destruct,
+ {
+ if self { Some(f()) } else { None }
+ }
+}