Skip to content
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

Add Option::is_some_with and Result::is_{ok,err}_with #93051

Merged
merged 5 commits into from
Jan 19, 2022
Merged
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
23 changes: 23 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,29 @@ impl<T> Option<T> {
matches!(*self, Some(_))
}

/// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_some_with)]
///
/// let x: Option<u32> = Some(2);
/// assert_eq!(x.is_some_with(|&x| x > 1), true);
///
/// let x: Option<u32> = Some(0);
/// assert_eq!(x.is_some_with(|&x| x > 1), false);
///
/// let x: Option<u32> = None;
/// assert_eq!(x.is_some_with(|&x| x > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_some_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
matches!(self, Some(x) if f(x))
}

/// Returns `true` if the option is a [`None`] value.
///
/// # Examples
Expand Down
47 changes: 47 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,29 @@ impl<T, E> Result<T, E> {
matches!(*self, Ok(_))
}

/// Returns `true` if the result is [`Ok`] wrapping a value matching the predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_some_with)]
///
/// let x: Result<u32, &str> = Ok(2);
/// assert_eq!(x.is_ok_with(|&x| x > 1), true);
///
/// let x: Result<u32, &str> = Ok(0);
/// assert_eq!(x.is_ok_with(|&x| x > 1), false);
///
/// let x: Result<u32, &str> = Err("hey");
/// assert_eq!(x.is_ok_with(|&x| x > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_ok_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
matches!(self, Ok(x) if f(x))
}

/// Returns `true` if the result is [`Err`].
///
/// # Examples
Expand All @@ -563,6 +586,30 @@ impl<T, E> Result<T, E> {
!self.is_ok()
}

/// Returns `true` if the result is [`Err`] wrapping a value matching the predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_some_with)]
/// use std::io::{Error, ErrorKind};
///
/// let x: Result<u32, Error> = Err(Error::new(ErrorKind::NotFound, "!"));
/// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), true);
///
/// let x: Result<u32, Error> = Err(Error::new(ErrorKind::PermissionDenied, "!"));
/// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false);
///
/// let x: Result<u32, Error> = Ok(123);
/// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_err_with(&self, f: impl FnOnce(&E) -> bool) -> bool {
matches!(self, Err(x) if f(x))
}

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