]> git.lizzy.rs Git - rust.git/blob - library/core/src/bool.rs
diagnostics: tweak error message to give more rationale to unsafe Fn
[rust.git] / library / core / src / bool.rs
1 //! impl bool {}
2
3 use crate::marker::Destruct;
4
5 #[cfg_attr(bootstrap, lang = "bool")]
6 impl bool {
7     /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
8     /// or `None` otherwise.
9     ///
10     /// # Examples
11     ///
12     /// ```
13     /// #![feature(bool_to_option)]
14     ///
15     /// assert_eq!(false.then_some(0), None);
16     /// assert_eq!(true.then_some(0), Some(0));
17     /// ```
18     #[unstable(feature = "bool_to_option", issue = "80967")]
19     #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
20     #[inline]
21     #[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
22     pub const fn then_some<T>(self, t: T) -> Option<T>
23     where
24         T: ~const Drop + ~const Destruct,
25     {
26         if self { Some(t) } else { None }
27     }
28
29     /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
30     /// or `None` otherwise.
31     ///
32     /// # Examples
33     ///
34     /// ```
35     /// assert_eq!(false.then(|| 0), None);
36     /// assert_eq!(true.then(|| 0), Some(0));
37     /// ```
38     #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
39     #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
40     #[inline]
41     #[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
42     pub const fn then<T, F>(self, f: F) -> Option<T>
43     where
44         F: ~const FnOnce() -> T,
45         F: ~const Drop + ~const Destruct,
46     {
47         if self { Some(f()) } else { None }
48     }
49 }