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 @@ pub const fn is_some(&self) -> bool {
/// #![feature(is_some_with)]
///
/// 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);
/// 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;
/// assert_eq!(x.is_some_and(|&x| x > 1), false);
/// assert_eq!(x.is_some_and(|x| x > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_some_and(&self, f: impl FnOnce(&T) -> bool) -> bool {
matches!(self, Some(x) if f(x))
pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool {
match self {
None => false,
Some(x) => f(x),
}
}
/// Returns `true` if the option is a [`None`] value.

View File

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

View File

@ -211,7 +211,7 @@ pub(super) fn find_granting(
}
// 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.
});
if found { Ok(None) } else { Err(()) }