Skip to content

[Core] Add is_xxx_or for Result and Option #118090

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

Closed
Closed
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
26 changes: 26 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,32 @@ impl<T> Option<T> {
!self.is_some()
}

/// Returns `true` if the option is a [`None`] value or the value inside the [`Some`] matches a predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_none_or)]
/// let x: Option<u32> = Some(2);
/// assert_eq!(x.is_none_or(|x| x > 1), true);
///
/// let x: Option<u32> = None;
/// assert_eq!(x.is_none_or(|x| x > 1), true);
///
/// let x: Option<u32> = Some(0);
/// assert_eq!(x.is_none_or(|x| x > 1), false);
/// ```
#[must_use = "if you intended to assert that there's no value or the value matches \
a predicate, considier use `and_then` with a `panic` instead"]
#[inline]
#[unstable(feature = "is_none_or", issue = "none")]
pub fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool {
match self {
None => true,
Some(x) => f(x),
}
}

/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
Expand Down
52 changes: 52 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,32 @@ impl<T, E> Result<T, E> {
}
}

/// Returns `true` if the result is [`Ok`] or the value inside of the [`Err`] matches a predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_none_or)]
/// let x: Result<&'static str, u32> = Err(5);
/// assert_eq!(x.is_ok_or(|x| x > 1), true);
///
/// let x: Result<&'static str, u32> = Err(0);
/// assert_eq!(x.is_ok_or(|x| x > 1), false);
///
/// let x: Result<&'static str, u32> = Ok("some result");
/// assert_eq!(x.is_ok_or(|x| x > 1), true);
/// ```
#[must_use = "if you intended to assert that this is an ok or the error value matches \
a predicate, considier use `and_then` with a `panic` instead"]
#[inline]
#[unstable(feature = "is_none_or", issue = "none")]
pub fn is_ok_or(self, f: impl FnOnce(E) -> bool) -> bool {
match self {
Ok(_) => true,
Err(e) => f(e),
}
}

/// Returns `true` if the result is [`Err`].
///
/// # Examples
Expand Down Expand Up @@ -608,6 +634,32 @@ impl<T, E> Result<T, E> {
}
}

/// Returns `true` if the result is [`Err`] or the value inside of the [`Ok`] matches a predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_none_or)]
/// let x: Result<u32, &'static str> = Err("some error");
/// assert_eq!(x.is_err_or(|x| x > 1), true);
///
/// let x: Result<u32, &'static str> = Ok(0);
/// assert_eq!(x.is_err_or(|x| x > 1), false);
///
/// let x: Result<u32, &'static str> = Ok(5);
/// assert_eq!(x.is_err_or(|x| x > 1), true);
/// ```
#[must_use = "if you intended to assert that this is an error or the ok value matches \
a predicate, considier use `and_then` with a `panic` instead"]
#[inline]
#[unstable(feature = "is_none_or", issue = "none")]
pub fn is_err_or(self, f: impl FnOnce(T) -> bool) -> bool {
match self {
Ok(x) => f(x),
Err(_) => true,
}
}

/////////////////////////////////////////////////////////////////////////
// Adapter for each variant
/////////////////////////////////////////////////////////////////////////
Expand Down