Skip to content

Commit

Permalink
Rollup merge of rust-lang#62356 - soc:topic/contains, r=Centril
Browse files Browse the repository at this point in the history
Implement Option::contains and Result::contains

This increases consistency with other common data structures.
  • Loading branch information
Centril authored Jul 8, 2019
2 parents 6e310f2 + 6f76da4 commit 6da3098
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/////////////////////////////////////////////////////////////////////////
Expand Down
52 changes: 52 additions & 0 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 6da3098

Please sign in to comment.