Skip to content

Commit d5bc168

Browse files
authored
Rollup merge of rust-lang#93051 - m-ou-se:is-some-with, r=yaahc
Add Option::is_some_with and Result::is_{ok,err}_with See rust-lang#62358 (comment)
2 parents 0b9056c + 5fee3e7 commit d5bc168

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Diff for: library/core/src/option.rs

+23
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,29 @@ impl<T> Option<T> {
551551
matches!(*self, Some(_))
552552
}
553553

554+
/// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate.
555+
///
556+
/// # Examples
557+
///
558+
/// ```
559+
/// #![feature(is_some_with)]
560+
///
561+
/// let x: Option<u32> = Some(2);
562+
/// assert_eq!(x.is_some_with(|&x| x > 1), true);
563+
///
564+
/// let x: Option<u32> = Some(0);
565+
/// assert_eq!(x.is_some_with(|&x| x > 1), false);
566+
///
567+
/// let x: Option<u32> = None;
568+
/// assert_eq!(x.is_some_with(|&x| x > 1), false);
569+
/// ```
570+
#[must_use]
571+
#[inline]
572+
#[unstable(feature = "is_some_with", issue = "93050")]
573+
pub fn is_some_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
574+
matches!(self, Some(x) if f(x))
575+
}
576+
554577
/// Returns `true` if the option is a [`None`] value.
555578
///
556579
/// # Examples

Diff for: library/core/src/result.rs

+47
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,29 @@ impl<T, E> Result<T, E> {
542542
matches!(*self, Ok(_))
543543
}
544544

545+
/// Returns `true` if the result is [`Ok`] wrapping a value matching the predicate.
546+
///
547+
/// # Examples
548+
///
549+
/// ```
550+
/// #![feature(is_some_with)]
551+
///
552+
/// let x: Result<u32, &str> = Ok(2);
553+
/// assert_eq!(x.is_ok_with(|&x| x > 1), true);
554+
///
555+
/// let x: Result<u32, &str> = Ok(0);
556+
/// assert_eq!(x.is_ok_with(|&x| x > 1), false);
557+
///
558+
/// let x: Result<u32, &str> = Err("hey");
559+
/// assert_eq!(x.is_ok_with(|&x| x > 1), false);
560+
/// ```
561+
#[must_use]
562+
#[inline]
563+
#[unstable(feature = "is_some_with", issue = "93050")]
564+
pub fn is_ok_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
565+
matches!(self, Ok(x) if f(x))
566+
}
567+
545568
/// Returns `true` if the result is [`Err`].
546569
///
547570
/// # Examples
@@ -563,6 +586,30 @@ impl<T, E> Result<T, E> {
563586
!self.is_ok()
564587
}
565588

589+
/// Returns `true` if the result is [`Err`] wrapping a value matching the predicate.
590+
///
591+
/// # Examples
592+
///
593+
/// ```
594+
/// #![feature(is_some_with)]
595+
/// use std::io::{Error, ErrorKind};
596+
///
597+
/// let x: Result<u32, Error> = Err(Error::new(ErrorKind::NotFound, "!"));
598+
/// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), true);
599+
///
600+
/// let x: Result<u32, Error> = Err(Error::new(ErrorKind::PermissionDenied, "!"));
601+
/// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false);
602+
///
603+
/// let x: Result<u32, Error> = Ok(123);
604+
/// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false);
605+
/// ```
606+
#[must_use]
607+
#[inline]
608+
#[unstable(feature = "is_some_with", issue = "93050")]
609+
pub fn is_err_with(&self, f: impl FnOnce(&E) -> bool) -> bool {
610+
matches!(self, Err(x) if f(x))
611+
}
612+
566613
/////////////////////////////////////////////////////////////////////////
567614
// Adapter for each variant
568615
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)