Skip to content

Rename bool::ok_or[_else] to bool::then_ok_or[_else] to avoid confusion with Option::ok_or[_else] #144037

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
30 changes: 15 additions & 15 deletions library/core/src/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ impl bool {
/// 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
/// Arguments passed to `then_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.
/// [`then_ok_or_else`], which is lazily evaluated.
///
/// [`ok_or_else`]: bool::ok_or_else
/// [`then_ok_or_else`]: bool::then_ok_or_else
///
/// # Examples
///
/// ```
/// #![feature(bool_to_result)]
///
/// assert_eq!(false.ok_or(0), Err(0));
/// assert_eq!(true.ok_or(0), Ok(()));
/// assert_eq!(false.then_ok_or(0), Err(0));
/// assert_eq!(true.then_ok_or(0), Ok(()));
/// ```
///
/// ```
Expand All @@ -86,16 +86,16 @@ impl bool {
/// 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());
/// assert!(true.then_ok_or(function_with_side_effects()).is_ok());
/// assert!(false.then_ok_or(function_with_side_effects()).is_err());
///
/// // `a` is incremented twice because the value passed to `ok_or` is
/// // `a` is incremented twice because the value passed to `then_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> {
pub fn then_ok_or<E>(self, err: E) -> Result<(), E> {
if self { Ok(()) } else { Err(err) }
}

Expand All @@ -107,25 +107,25 @@ impl bool {
/// ```
/// #![feature(bool_to_result)]
///
/// assert_eq!(false.ok_or_else(|| 0), Err(0));
/// assert_eq!(true.ok_or_else(|| 0), Ok(()));
/// assert_eq!(false.then_ok_or_else(|| 0), Err(0));
/// assert_eq!(true.then_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());
/// assert!(true.then_ok_or_else(|| { a += 1; }).is_ok());
/// assert!(false.then_ok_or_else(|| { a += 1; }).is_err());
///
/// // `a` is incremented once because the closure is evaluated lazily by
/// // `ok_or_else`.
/// // `then_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> {
pub fn then_ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E> {
if self { Ok(()) } else { Err(f()) }
}
}
8 changes: 4 additions & 4 deletions library/coretests/tests/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ fn test_bool_to_option() {

#[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(()));
assert_eq!(false.then_ok_or(0), Err(0));
assert_eq!(true.then_ok_or(0), Ok(()));
assert_eq!(false.then_ok_or_else(|| 0), Err(0));
assert_eq!(true.then_ok_or_else(|| 0), Ok(()));
}
Loading