2019-09-07 12:16:18 +00:00
|
|
|
//! impl bool {}
|
|
|
|
|
|
|
|
#[lang = "bool"]
|
2019-09-07 14:49:27 +00:00
|
|
|
impl bool {
|
2021-07-06 02:50:26 +00:00
|
|
|
/// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
|
|
|
|
/// or `None` otherwise.
|
2019-09-07 14:49:27 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(bool_to_option)]
|
|
|
|
///
|
2019-12-06 12:18:32 +00:00
|
|
|
/// assert_eq!(false.then_some(0), None);
|
|
|
|
/// assert_eq!(true.then_some(0), Some(0));
|
2019-09-07 14:49:27 +00:00
|
|
|
/// ```
|
2021-07-10 22:25:01 +00:00
|
|
|
#[unstable(feature = "bool_to_option", issue = "80967")]
|
2019-09-07 14:49:27 +00:00
|
|
|
#[inline]
|
2019-12-06 12:18:32 +00:00
|
|
|
pub fn then_some<T>(self, t: T) -> Option<T> {
|
2019-12-07 04:18:12 +00:00
|
|
|
if self { Some(t) } else { None }
|
2019-09-07 14:49:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 02:50:26 +00:00
|
|
|
/// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
|
|
|
|
/// or `None` otherwise.
|
2019-09-07 14:49:27 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-12-06 12:18:32 +00:00
|
|
|
/// assert_eq!(false.then(|| 0), None);
|
|
|
|
/// assert_eq!(true.then(|| 0), Some(0));
|
2019-09-07 14:49:27 +00:00
|
|
|
/// ```
|
2020-11-22 13:25:19 +00:00
|
|
|
#[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
|
2019-09-07 14:49:27 +00:00
|
|
|
#[inline]
|
2019-12-06 12:18:32 +00:00
|
|
|
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
|
2019-12-07 04:18:12 +00:00
|
|
|
if self { Some(f()) } else { None }
|
2019-09-07 14:49:27 +00:00
|
|
|
}
|
|
|
|
}
|