From 282224edf19fb8a511c5a9e279f18b804ff61b25 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 18 Jan 2022 22:17:34 +0100 Subject: [PATCH 1/5] Add Option::is_some_with. --- library/core/src/option.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 8adfb6f4bcf52..d9ee289f216b0 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -551,6 +551,27 @@ impl Option { matches!(*self, Some(_)) } + /// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate. + /// + /// # Examples + /// + /// ``` + /// let x: Option = Some(2); + /// assert_eq!(x.is_some_with(|x| x > 1), true); + /// + /// let x: Option = Some(0); + /// assert_eq!(x.is_some_with(|x| x > 1), false); + /// + /// let x: Option = None; + /// assert_eq!(x.is_some_with(|x| x > 1), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "is_some_with", issue = "none")] + 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 From aaebae973f7a4fafd24cb6c9fad7a6beba205b74 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 18 Jan 2022 22:17:44 +0100 Subject: [PATCH 2/5] Add Result::{is_ok_with, is_err_with}. --- library/core/src/result.rs | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index b8f0d84746cce..f4f3d84e62bfd 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -542,6 +542,27 @@ impl Result { matches!(*self, Ok(_)) } + /// Returns `true` if the result is [`Ok`] wrapping a value matching the predicate. + /// + /// # Examples + /// + /// ``` + /// let x: Result = Ok(2); + /// assert_eq!(x.is_ok_with(|x| x > 1), true); + /// + /// let x: Result = Ok(0); + /// assert_eq!(x.is_ok_with(|x| x > 1), false); + /// + /// let x: Result = Err("hey"); + /// assert_eq!(x.is_ok_with(|x| x > 1), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "is_some_with", issue = "none")] + 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 +584,27 @@ impl Result { !self.is_ok() } + /// Returns `true` if the result is [`Err`] wrapping a value matching the predicate. + /// + /// # Examples + /// + /// ``` + /// let x: Result = Err("abc"); + /// assert_eq!(x.is_err_with(|x| x.len() > 1), true); + /// + /// let x: Result = Err(""); + /// assert_eq!(x.is_ok_with(|x| x.len() > 1), false); + /// + /// let x: Result = Ok(123); + /// assert_eq!(x.is_ok_with(|x| x.len() > 1), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "is_some_with", issue = "none")] + pub fn is_err_with(&self, f: impl FnOnce(&E) -> bool) -> bool { + matches!(self, Err(x) if f(x)) + } + ///////////////////////////////////////////////////////////////////////// // Adapter for each variant ///////////////////////////////////////////////////////////////////////// From 148234ff73aa649178f92f529b9f5b0fc5adf3ed Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 18 Jan 2022 22:18:16 +0100 Subject: [PATCH 3/5] Add is_some_with tracking issue number. --- library/core/src/option.rs | 2 +- library/core/src/result.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index d9ee289f216b0..e8850176f7cd3 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -567,7 +567,7 @@ impl Option { /// ``` #[must_use] #[inline] - #[unstable(feature = "is_some_with", issue = "none")] + #[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)) } diff --git a/library/core/src/result.rs b/library/core/src/result.rs index f4f3d84e62bfd..82d99cef45860 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -558,7 +558,7 @@ impl Result { /// ``` #[must_use] #[inline] - #[unstable(feature = "is_some_with", issue = "none")] + #[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)) } @@ -600,7 +600,7 @@ impl Result { /// ``` #[must_use] #[inline] - #[unstable(feature = "is_some_with", issue = "none")] + #[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)) } From 45dee47fec4b04fc642361cbf1f2f9fc0e0ad53d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 18 Jan 2022 22:53:43 +0100 Subject: [PATCH 4/5] Improve is_err_with example. --- library/core/src/result.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 82d99cef45860..9991112e7f553 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -589,14 +589,16 @@ impl Result { /// # Examples /// /// ``` - /// let x: Result = Err("abc"); - /// assert_eq!(x.is_err_with(|x| x.len() > 1), true); + /// use std::io::{Error, ErrorKind}; /// - /// let x: Result = Err(""); - /// assert_eq!(x.is_ok_with(|x| x.len() > 1), false); + /// let x: Result = Err(Error::new(ErrorKind::NotFound, "!")); + /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), true); /// - /// let x: Result = Ok(123); - /// assert_eq!(x.is_ok_with(|x| x.len() > 1), false); + /// let x: Result = Err(Error::new(ErrorKind::PermissionDenied, "!")); + /// assert_eq!(x.is_ok_with(|x| x.kind() == ErrorKind::NotFound), false); + /// + /// let x: Result = Ok(123); + /// assert_eq!(x.is_ok_with(|x| x.kind() == ErrorKind::NotFound), false); /// ``` #[must_use] #[inline] From 5fee3e7a9c65445e3585eb4a162c774c788a02d5 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 19 Jan 2022 00:09:59 +0100 Subject: [PATCH 5/5] Fix is_some_with tests. --- library/core/src/option.rs | 8 +++++--- library/core/src/result.rs | 13 ++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index e8850176f7cd3..611f4ab38ab33 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -556,14 +556,16 @@ impl Option { /// # Examples /// /// ``` + /// #![feature(is_some_with)] + /// /// let x: Option = Some(2); - /// assert_eq!(x.is_some_with(|x| x > 1), true); + /// assert_eq!(x.is_some_with(|&x| x > 1), true); /// /// let x: Option = Some(0); - /// assert_eq!(x.is_some_with(|x| x > 1), false); + /// assert_eq!(x.is_some_with(|&x| x > 1), false); /// /// let x: Option = None; - /// assert_eq!(x.is_some_with(|x| x > 1), false); + /// assert_eq!(x.is_some_with(|&x| x > 1), false); /// ``` #[must_use] #[inline] diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 9991112e7f553..fbd6d419236ae 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -547,14 +547,16 @@ impl Result { /// # Examples /// /// ``` + /// #![feature(is_some_with)] + /// /// let x: Result = Ok(2); - /// assert_eq!(x.is_ok_with(|x| x > 1), true); + /// assert_eq!(x.is_ok_with(|&x| x > 1), true); /// /// let x: Result = Ok(0); - /// assert_eq!(x.is_ok_with(|x| x > 1), false); + /// assert_eq!(x.is_ok_with(|&x| x > 1), false); /// /// let x: Result = Err("hey"); - /// assert_eq!(x.is_ok_with(|x| x > 1), false); + /// assert_eq!(x.is_ok_with(|&x| x > 1), false); /// ``` #[must_use] #[inline] @@ -589,16 +591,17 @@ impl Result { /// # Examples /// /// ``` + /// #![feature(is_some_with)] /// use std::io::{Error, ErrorKind}; /// /// let x: Result = Err(Error::new(ErrorKind::NotFound, "!")); /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), true); /// /// let x: Result = Err(Error::new(ErrorKind::PermissionDenied, "!")); - /// assert_eq!(x.is_ok_with(|x| x.kind() == ErrorKind::NotFound), false); + /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false); /// /// let x: Result = Ok(123); - /// assert_eq!(x.is_ok_with(|x| x.kind() == ErrorKind::NotFound), false); + /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false); /// ``` #[must_use] #[inline]