Skip to content

Commit a477754

Browse files
authored
Unrolled build for #142749
Rollup merge of #142749 - LimpSquid:bool_to_result, r=scottmcm Add methods for converting bool to `Result<(), E>` ## Tracking Issue #142748 ## ACP rust-lang/libs-team#606
2 parents 837c5dd + 19352e9 commit a477754

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

library/core/src/bool.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,71 @@ impl bool {
6161
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
6262
if self { Some(f()) } else { None }
6363
}
64+
65+
/// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
66+
/// or `Err(err)` otherwise.
67+
///
68+
/// Arguments passed to `ok_or` are eagerly evaluated; if you are
69+
/// passing the result of a function call, it is recommended to use
70+
/// [`ok_or_else`], which is lazily evaluated.
71+
///
72+
/// [`ok_or_else`]: bool::ok_or_else
73+
///
74+
/// # Examples
75+
///
76+
/// ```
77+
/// #![feature(bool_to_result)]
78+
///
79+
/// assert_eq!(false.ok_or(0), Err(0));
80+
/// assert_eq!(true.ok_or(0), Ok(()));
81+
/// ```
82+
///
83+
/// ```
84+
/// #![feature(bool_to_result)]
85+
///
86+
/// let mut a = 0;
87+
/// let mut function_with_side_effects = || { a += 1; };
88+
///
89+
/// assert!(true.ok_or(function_with_side_effects()).is_ok());
90+
/// assert!(false.ok_or(function_with_side_effects()).is_err());
91+
///
92+
/// // `a` is incremented twice because the value passed to `ok_or` is
93+
/// // evaluated eagerly.
94+
/// assert_eq!(a, 2);
95+
/// ```
96+
#[unstable(feature = "bool_to_result", issue = "142748")]
97+
#[inline]
98+
pub fn ok_or<E>(self, err: E) -> Result<(), E> {
99+
if self { Ok(()) } else { Err(err) }
100+
}
101+
102+
/// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
103+
/// or `Err(f())` otherwise.
104+
///
105+
/// # Examples
106+
///
107+
/// ```
108+
/// #![feature(bool_to_result)]
109+
///
110+
/// assert_eq!(false.ok_or_else(|| 0), Err(0));
111+
/// assert_eq!(true.ok_or_else(|| 0), Ok(()));
112+
/// ```
113+
///
114+
/// ```
115+
/// #![feature(bool_to_result)]
116+
///
117+
/// let mut a = 0;
118+
///
119+
/// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
120+
/// assert!(false.ok_or_else(|| { a += 1; }).is_err());
121+
///
122+
/// // `a` is incremented once because the closure is evaluated lazily by
123+
/// // `ok_or_else`.
124+
/// assert_eq!(a, 1);
125+
/// ```
126+
#[unstable(feature = "bool_to_result", issue = "142748")]
127+
#[inline]
128+
pub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E> {
129+
if self { Ok(()) } else { Err(f()) }
130+
}
64131
}

library/coretests/tests/bool.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ fn test_bool_to_option() {
105105
assert_eq!(D, Some(0));
106106
*/
107107
}
108+
109+
#[test]
110+
fn test_bool_to_result() {
111+
assert_eq!(false.ok_or(0), Err(0));
112+
assert_eq!(true.ok_or(0), Ok(()));
113+
assert_eq!(false.ok_or_else(|| 0), Err(0));
114+
assert_eq!(true.ok_or_else(|| 0), Ok(()));
115+
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(async_iter_from_iter)]
1212
#![feature(async_iterator)]
1313
#![feature(bigint_helper_methods)]
14+
#![feature(bool_to_result)]
1415
#![feature(bstr)]
1516
#![feature(cfg_target_has_reliable_f16_f128)]
1617
#![feature(char_max_len)]

0 commit comments

Comments
 (0)