diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-01-19 10:42:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-19 10:42:20 +0100 |
| commit | d5bc168d65dd5d0cbdf138797fe23306867a0e67 (patch) | |
| tree | 0ee230724c10223e30a4fe628748d36b1578475a | |
| parent | 0b9056c38aef4c4ffc3140802c70d6c3513dec06 (diff) | |
| parent | 5fee3e7a9c65445e3585eb4a162c774c788a02d5 (diff) | |
| download | rust-d5bc168d65dd5d0cbdf138797fe23306867a0e67.tar.gz rust-d5bc168d65dd5d0cbdf138797fe23306867a0e67.zip | |
Rollup merge of #93051 - m-ou-se:is-some-with, r=yaahc
Add Option::is_some_with and Result::is_{ok,err}_with
See https://github.com/rust-lang/rust/issues/62358#issuecomment-1015827777
| -rw-r--r-- | library/core/src/option.rs | 23 | ||||
| -rw-r--r-- | library/core/src/result.rs | 47 |
2 files changed, 70 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 8adfb6f4bcf..611f4ab38ab 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -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 diff --git a/library/core/src/result.rs b/library/core/src/result.rs index b8f0d84746c..fbd6d419236 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -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 @@ -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 ///////////////////////////////////////////////////////////////////////// |
