2019-09-07 07:16:18 -05:00
|
|
|
//! impl bool {}
|
|
|
|
|
2022-03-21 00:52:41 -05:00
|
|
|
use crate::marker::Destruct;
|
|
|
|
|
2019-09-07 09:49:27 -05:00
|
|
|
impl bool {
|
2021-07-05 21:50:26 -05:00
|
|
|
/// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
|
|
|
|
/// or `None` otherwise.
|
2019-09-07 09:49:27 -05:00
|
|
|
///
|
2022-09-01 11:32:00 -05:00
|
|
|
/// Arguments passed to `then_some` are eagerly evaluated; if you are
|
|
|
|
/// passing the result of a function call, it is recommended to use
|
2022-09-01 10:09:25 -05:00
|
|
|
/// [`then`], which is lazily evaluated.
|
2022-09-01 11:32:00 -05:00
|
|
|
///
|
2022-09-01 10:09:25 -05:00
|
|
|
/// [`then`]: bool::then
|
2022-09-01 11:32:00 -05:00
|
|
|
///
|
2019-09-07 09:49:27 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-12-06 06:18:32 -06:00
|
|
|
/// assert_eq!(false.then_some(0), None);
|
|
|
|
/// assert_eq!(true.then_some(0), Some(0));
|
2019-09-07 09:49:27 -05:00
|
|
|
/// ```
|
2022-09-21 22:23:14 -05:00
|
|
|
///
|
2022-09-21 16:07:50 -05:00
|
|
|
/// ```
|
|
|
|
/// let mut a = 0;
|
|
|
|
/// let mut function_with_side_effects = || { a += 1; };
|
2022-09-21 22:23:14 -05:00
|
|
|
///
|
2022-09-21 16:07:50 -05:00
|
|
|
/// true.then_some(function_with_side_effects());
|
|
|
|
/// false.then_some(function_with_side_effects());
|
2022-09-21 22:23:14 -05:00
|
|
|
///
|
|
|
|
/// // `a` is incremented twice because the value passed to `then_some` is
|
|
|
|
/// // evaluated eagerly.
|
2022-09-21 16:07:50 -05:00
|
|
|
/// assert_eq!(a, 2);
|
|
|
|
/// ```
|
2022-05-02 03:12:35 -05:00
|
|
|
#[stable(feature = "bool_to_option", since = "1.62.0")]
|
2021-12-14 10:11:23 -06:00
|
|
|
#[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
|
2019-09-07 09:49:27 -05:00
|
|
|
#[inline]
|
2021-12-14 10:11:23 -06:00
|
|
|
pub const fn then_some<T>(self, t: T) -> Option<T>
|
|
|
|
where
|
2022-04-05 15:42:23 -05:00
|
|
|
T: ~const Destruct,
|
2021-12-14 10:11:23 -06:00
|
|
|
{
|
2019-12-06 22:18:12 -06:00
|
|
|
if self { Some(t) } else { None }
|
2019-09-07 09:49:27 -05:00
|
|
|
}
|
|
|
|
|
2021-07-05 21:50:26 -05:00
|
|
|
/// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
|
|
|
|
/// or `None` otherwise.
|
2019-09-07 09:49:27 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-12-06 06:18:32 -06:00
|
|
|
/// assert_eq!(false.then(|| 0), None);
|
|
|
|
/// assert_eq!(true.then(|| 0), Some(0));
|
2019-09-07 09:49:27 -05:00
|
|
|
/// ```
|
2022-09-21 22:23:14 -05:00
|
|
|
///
|
2022-09-21 16:07:50 -05:00
|
|
|
/// ```
|
|
|
|
/// let mut a = 0;
|
2022-09-21 22:23:14 -05:00
|
|
|
///
|
2022-09-21 16:07:50 -05:00
|
|
|
/// true.then(|| { a += 1; });
|
|
|
|
/// false.then(|| { a += 1; });
|
2022-09-21 22:23:14 -05:00
|
|
|
///
|
|
|
|
/// // `a` is incremented once because the closure is evaluated lazily by
|
2022-09-21 16:07:50 -05:00
|
|
|
/// // `then`.
|
2022-09-22 01:12:06 -05:00
|
|
|
/// assert_eq!(a, 1);
|
2022-09-21 16:07:50 -05:00
|
|
|
/// ```
|
2020-11-22 07:25:19 -06:00
|
|
|
#[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
|
2021-12-14 10:11:23 -06:00
|
|
|
#[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
|
2019-09-07 09:49:27 -05:00
|
|
|
#[inline]
|
2021-12-14 10:11:23 -06:00
|
|
|
pub const fn then<T, F>(self, f: F) -> Option<T>
|
|
|
|
where
|
|
|
|
F: ~const FnOnce() -> T,
|
2022-04-05 15:42:23 -05:00
|
|
|
F: ~const Destruct,
|
2021-12-14 10:11:23 -06:00
|
|
|
{
|
2019-12-06 22:18:12 -06:00
|
|
|
if self { Some(f()) } else { None }
|
2019-09-07 09:49:27 -05:00
|
|
|
}
|
|
|
|
}
|