From 9c46bf72fd60f2a19909a5298d68e4f32cd46a9f Mon Sep 17 00:00:00 2001 From: Embers-of-the-Fire Date: Sun, 19 Nov 2023 21:56:36 +0800 Subject: [PATCH] feature: is_none_or --- library/core/src/option.rs | 26 +++++++++++++++++++ library/core/src/result.rs | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index bfd6aee4a2357..c52130e4851b3 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -650,6 +650,32 @@ impl Option { !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 = Some(2); + /// assert_eq!(x.is_none_or(|x| x > 1), true); + /// + /// let x: Option = None; + /// assert_eq!(x.is_none_or(|x| x > 1), true); + /// + /// let x: Option = 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 ///////////////////////////////////////////////////////////////////////// diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 90c346e98bccf..4d3ae7f09b322 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -563,6 +563,32 @@ impl Result { } } + /// 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 @@ -608,6 +634,32 @@ impl Result { } } + /// 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 = Err("some error"); + /// assert_eq!(x.is_err_or(|x| x > 1), true); + /// + /// let x: Result = Ok(0); + /// assert_eq!(x.is_err_or(|x| x > 1), false); + /// + /// let x: Result = 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 /////////////////////////////////////////////////////////////////////////