Skip to content

Add methods for converting bool to Result<(), E> #142749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions library/core/src/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,71 @@ impl bool {
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
if self { Some(f()) } else { None }
}

/// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
/// or `Err(err)` otherwise.
///
/// Arguments passed to `ok_or` are eagerly evaluated; if you are
/// passing the result of a function call, it is recommended to use
/// [`ok_or_else`], which is lazily evaluated.
///
/// [`ok_or_else`]: bool::ok_or_else
///
/// # Examples
///
/// ```
/// #![feature(bool_to_result)]
///
/// assert_eq!(false.ok_or(0), Err(0));
/// assert_eq!(true.ok_or(0), Ok(()));
/// ```
///
/// ```
/// #![feature(bool_to_result)]
///
/// let mut a = 0;
/// let mut function_with_side_effects = || { a += 1; };
///
/// assert!(true.ok_or(function_with_side_effects()).is_ok());
/// assert!(false.ok_or(function_with_side_effects()).is_err());
///
/// // `a` is incremented twice because the value passed to `ok_or` is
/// // evaluated eagerly.
/// assert_eq!(a, 2);
/// ```
#[unstable(feature = "bool_to_result", issue = "142748")]
#[inline]
pub fn ok_or<E>(self, err: E) -> Result<(), E> {
if self { Ok(()) } else { Err(err) }
}

/// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
/// or `Err(f())` otherwise.
///
/// # Examples
///
/// ```
/// #![feature(bool_to_result)]
///
/// assert_eq!(false.ok_or_else(|| 0), Err(0));
/// assert_eq!(true.ok_or_else(|| 0), Ok(()));
/// ```
///
/// ```
/// #![feature(bool_to_result)]
///
/// let mut a = 0;
///
/// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
/// assert!(false.ok_or_else(|| { a += 1; }).is_err());
///
/// // `a` is incremented once because the closure is evaluated lazily by
/// // `ok_or_else`.
/// assert_eq!(a, 1);
/// ```
#[unstable(feature = "bool_to_result", issue = "142748")]
#[inline]
pub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E> {
if self { Ok(()) } else { Err(f()) }
}
}
8 changes: 8 additions & 0 deletions library/coretests/tests/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ fn test_bool_to_option() {
assert_eq!(D, Some(0));
*/
}

#[test]
fn test_bool_to_result() {
assert_eq!(false.ok_or(0), Err(0));
assert_eq!(true.ok_or(0), Ok(()));
assert_eq!(false.ok_or_else(|| 0), Err(0));
assert_eq!(true.ok_or_else(|| 0), Ok(()));
}
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(async_iter_from_iter)]
#![feature(async_iterator)]
#![feature(bigint_helper_methods)]
#![feature(bool_to_result)]
#![feature(bstr)]
#![feature(cfg_target_has_reliable_f16_f128)]
#![feature(char_max_len)]
Expand Down
Loading