diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-07-08 02:40:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-08 02:40:53 +0200 |
| commit | 6da30983d626bdb6ca455edb204218c6835d5078 (patch) | |
| tree | 18f9b1343c53b9842b1dc3bf22990eb1240c7658 /src | |
| parent | 6e310f2abae97323ca1d5469657b83aa1a9407e0 (diff) | |
| parent | 6f76da494b4b89839cd9a1a13e725ec5cdd83743 (diff) | |
| download | rust-6da30983d626bdb6ca455edb204218c6835d5078.tar.gz rust-6da30983d626bdb6ca455edb204218c6835d5078.zip | |
Rollup merge of #62356 - soc:topic/contains, r=Centril
Implement Option::contains and Result::contains This increases consistency with other common data structures.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/option.rs | 26 | ||||
| -rw-r--r-- | src/libcore/result.rs | 52 |
2 files changed, 78 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index a6bdd5a9063..29169951e46 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -208,6 +208,32 @@ impl<T> Option<T> { !self.is_some() } + /// Returns `true` if the option is a [`Some`] value containing the given value. + /// + /// # Examples + /// + /// ``` + /// #![feature(option_result_contains)] + /// + /// let x: Option<u32> = Some(2); + /// assert_eq!(x.contains(&2), true); + /// + /// let x: Option<u32> = Some(3); + /// assert_eq!(x.contains(&2), false); + /// + /// let x: Option<u32> = None; + /// assert_eq!(x.contains(&2), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "option_result_contains", issue = "62358")] + pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> { + match self { + Some(y) => x == y, + None => false, + } + } + ///////////////////////////////////////////////////////////////////////// // Adapter for working with references ///////////////////////////////////////////////////////////////////////// diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 8a09877ce1f..b64ad149cf4 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -309,6 +309,58 @@ impl<T, E> Result<T, E> { !self.is_ok() } + /// Returns `true` if the result is an [`Ok`] value containing the given value. + /// + /// # Examples + /// + /// ``` + /// #![feature(option_result_contains)] + /// + /// let x: Result<u32, &str> = Ok(2); + /// assert_eq!(x.contains(&2), true); + /// + /// let x: Result<u32, &str> = Ok(3); + /// assert_eq!(x.contains(&2), false); + /// + /// let x: Result<u32, &str> = Err("Some error message"); + /// assert_eq!(x.contains(&2), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "option_result_contains", issue = "62358")] + pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> { + match self { + Ok(y) => x == y, + Err(_) => false + } + } + + /// Returns `true` if the result is an [`Err`] value containing the given value. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_contains_err)] + /// + /// let x: Result<u32, &str> = Ok(2); + /// assert_eq!(x.contains_err(&"Some error message"), false); + /// + /// let x: Result<u32, &str> = Err("Some error message"); + /// assert_eq!(x.contains_err(&"Some error message"), true); + /// + /// let x: Result<u32, &str> = Err("Some other error message"); + /// assert_eq!(x.contains_err(&"Some error message"), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "result_contains_err", issue = "62358")] + pub fn contains_err<F>(&self, f: &F) -> bool where F: PartialEq<E> { + match self { + Ok(_) => false, + Err(e) => f == e + } + } + ///////////////////////////////////////////////////////////////////////// // Adapter for each variant ///////////////////////////////////////////////////////////////////////// |
