1
Fork 0

Change is_some_and to take by value

This commit is contained in:
Cameron Steffen 2022-06-21 14:27:53 -05:00
parent 744e397d88
commit 2f83134e37
3 changed files with 22 additions and 13 deletions

View file

@ -562,19 +562,22 @@ impl<T> Option<T> {
/// #![feature(is_some_with)] /// #![feature(is_some_with)]
/// ///
/// let x: Option<u32> = Some(2); /// let x: Option<u32> = Some(2);
/// assert_eq!(x.is_some_and(|&x| x > 1), true); /// assert_eq!(x.is_some_and(|x| x > 1), true);
/// ///
/// let x: Option<u32> = Some(0); /// let x: Option<u32> = Some(0);
/// assert_eq!(x.is_some_and(|&x| x > 1), false); /// assert_eq!(x.is_some_and(|x| x > 1), false);
/// ///
/// let x: Option<u32> = None; /// let x: Option<u32> = None;
/// assert_eq!(x.is_some_and(|&x| x > 1), false); /// assert_eq!(x.is_some_and(|x| x > 1), false);
/// ``` /// ```
#[must_use] #[must_use]
#[inline] #[inline]
#[unstable(feature = "is_some_with", issue = "93050")] #[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_some_and(&self, f: impl FnOnce(&T) -> bool) -> bool { pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool {
matches!(self, Some(x) if f(x)) match self {
None => false,
Some(x) => f(x),
}
} }
/// Returns `true` if the option is a [`None`] value. /// Returns `true` if the option is a [`None`] value.

View file

@ -551,19 +551,22 @@ impl<T, E> Result<T, E> {
/// #![feature(is_some_with)] /// #![feature(is_some_with)]
/// ///
/// let x: Result<u32, &str> = Ok(2); /// let x: Result<u32, &str> = Ok(2);
/// assert_eq!(x.is_ok_and(|&x| x > 1), true); /// assert_eq!(x.is_ok_and(|x| x > 1), true);
/// ///
/// let x: Result<u32, &str> = Ok(0); /// let x: Result<u32, &str> = Ok(0);
/// assert_eq!(x.is_ok_and(|&x| x > 1), false); /// assert_eq!(x.is_ok_and(|x| x > 1), false);
/// ///
/// let x: Result<u32, &str> = Err("hey"); /// let x: Result<u32, &str> = Err("hey");
/// assert_eq!(x.is_ok_and(|&x| x > 1), false); /// assert_eq!(x.is_ok_and(|x| x > 1), false);
/// ``` /// ```
#[must_use] #[must_use]
#[inline] #[inline]
#[unstable(feature = "is_some_with", issue = "93050")] #[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_ok_and(&self, f: impl FnOnce(&T) -> bool) -> bool { pub fn is_ok_and(self, f: impl FnOnce(T) -> bool) -> bool {
matches!(self, Ok(x) if f(x)) match self {
Err(_) => false,
Ok(x) => f(x),
}
} }
/// Returns `true` if the result is [`Err`]. /// Returns `true` if the result is [`Err`].
@ -607,8 +610,11 @@ impl<T, E> Result<T, E> {
#[must_use] #[must_use]
#[inline] #[inline]
#[unstable(feature = "is_some_with", issue = "93050")] #[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_err_and(&self, f: impl FnOnce(&E) -> bool) -> bool { pub fn is_err_and(self, f: impl FnOnce(E) -> bool) -> bool {
matches!(self, Err(x) if f(x)) match self {
Ok(_) => false,
Err(e) => f(e),
}
} }
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////

View file

@ -211,7 +211,7 @@ impl<'tcx> Stack {
} }
// Couldn't find it in the stack; but if there is an unknown bottom it might be there. // Couldn't find it in the stack; but if there is an unknown bottom it might be there.
let found = self.unknown_bottom.is_some_and(|&unknown_limit| { let found = self.unknown_bottom.is_some_and(|unknown_limit| {
tag.0 < unknown_limit.0 // unknown_limit is an upper bound for what can be in the unknown bottom. tag.0 < unknown_limit.0 // unknown_limit is an upper bound for what can be in the unknown bottom.
}); });
if found { Ok(None) } else { Err(()) } if found { Ok(None) } else { Err(()) }