1
Fork 0

Use matches macro in libcore and libstd

This commit is contained in:
Igor Aleksanov 2020-01-07 10:35:16 +03:00
parent aa0769b92e
commit f720469fd0
12 changed files with 29 additions and 118 deletions

View file

@ -821,10 +821,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use] #[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
fn lt(&self, other: &Rhs) -> bool { fn lt(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) { matches!(self.partial_cmp(other), Some(Less))
Some(Less) => true,
_ => false,
}
} }
/// This method tests less than or equal to (for `self` and `other`) and is used by the `<=` /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
@ -843,10 +840,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use] #[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
fn le(&self, other: &Rhs) -> bool { fn le(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) { matches!(self.partial_cmp(other), Some(Less) | Some(Equal))
Some(Less) | Some(Equal) => true,
_ => false,
}
} }
/// This method tests greater than (for `self` and `other`) and is used by the `>` operator. /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
@ -864,10 +858,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use] #[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
fn gt(&self, other: &Rhs) -> bool { fn gt(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) { matches!(self.partial_cmp(other), Some(Greater))
Some(Greater) => true,
_ => false,
}
} }
/// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
@ -886,10 +877,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use] #[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
fn ge(&self, other: &Rhs) -> bool { fn ge(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) { matches!(self.partial_cmp(other), Some(Greater) | Some(Equal))
Some(Greater) | Some(Equal) => true,
_ => false,
}
} }
} }

View file

@ -2968,10 +2968,7 @@ pub trait Iterator {
Self::Item: PartialOrd<I::Item>, Self::Item: PartialOrd<I::Item>,
Self: Sized, Self: Sized,
{ {
match self.partial_cmp(other) { matches!(self.partial_cmp(other), Some(Ordering::Less) | Some(Ordering::Equal))
Some(Ordering::Less) | Some(Ordering::Equal) => true,
_ => false,
}
} }
/// Determines if the elements of this `Iterator` are lexicographically /// Determines if the elements of this `Iterator` are lexicographically
@ -3011,10 +3008,7 @@ pub trait Iterator {
Self::Item: PartialOrd<I::Item>, Self::Item: PartialOrd<I::Item>,
Self: Sized, Self: Sized,
{ {
match self.partial_cmp(other) { matches!(self.partial_cmp(other), Some(Ordering::Greater) | Some(Ordering::Equal))
Some(Ordering::Greater) | Some(Ordering::Equal) => true,
_ => false,
}
} }
/// Checks if the elements of this iterator are sorted. /// Checks if the elements of this iterator are sorted.

View file

@ -4283,10 +4283,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_alphabetic(&self) -> bool { pub fn is_ascii_alphabetic(&self) -> bool {
match *self { matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
b'A'..=b'Z' | b'a'..=b'z' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII uppercase character: /// Checks if the value is an ASCII uppercase character:
@ -4318,10 +4315,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_uppercase(&self) -> bool { pub fn is_ascii_uppercase(&self) -> bool {
match *self { matches!(*self, b'A'..=b'Z')
b'A'..=b'Z' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII lowercase character: /// Checks if the value is an ASCII lowercase character:
@ -4353,10 +4347,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_lowercase(&self) -> bool { pub fn is_ascii_lowercase(&self) -> bool {
match *self { matches!(*self, b'a'..=b'z')
b'a'..=b'z' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII alphanumeric character: /// Checks if the value is an ASCII alphanumeric character:
@ -4391,10 +4382,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_alphanumeric(&self) -> bool { pub fn is_ascii_alphanumeric(&self) -> bool {
match *self { matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII decimal digit: /// Checks if the value is an ASCII decimal digit:
@ -4426,10 +4414,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_digit(&self) -> bool { pub fn is_ascii_digit(&self) -> bool {
match *self { matches!(*self, b'0'..=b'9')
b'0'..=b'9' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII hexadecimal digit: /// Checks if the value is an ASCII hexadecimal digit:
@ -4464,10 +4449,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_hexdigit(&self) -> bool { pub fn is_ascii_hexdigit(&self) -> bool {
match *self { matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII punctuation character: /// Checks if the value is an ASCII punctuation character:
@ -4503,10 +4485,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_punctuation(&self) -> bool { pub fn is_ascii_punctuation(&self) -> bool {
match *self { matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII graphic character: /// Checks if the value is an ASCII graphic character:
@ -4538,10 +4517,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_graphic(&self) -> bool { pub fn is_ascii_graphic(&self) -> bool {
match *self { matches!(*self, b'!'..=b'~')
b'!'..=b'~' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII whitespace character: /// Checks if the value is an ASCII whitespace character:
@ -4590,10 +4566,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_whitespace(&self) -> bool { pub fn is_ascii_whitespace(&self) -> bool {
match *self { matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' => true,
_ => false,
}
} }
/// Checks if the value is an ASCII control character: /// Checks if the value is an ASCII control character:
@ -4627,10 +4600,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")] #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline] #[inline]
pub fn is_ascii_control(&self) -> bool { pub fn is_ascii_control(&self) -> bool {
match *self { matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
b'\0'..=b'\x1F' | b'\x7F' => true,
_ => false,
}
} }
} }

View file

@ -187,10 +187,7 @@ impl<T> Option<T> {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn is_some(&self) -> bool { pub fn is_some(&self) -> bool {
match *self { matches!(*self, Some(_))
Some(_) => true,
None => false,
}
} }
/// Returns `true` if the option is a [`None`] value. /// Returns `true` if the option is a [`None`] value.

View file

@ -282,10 +282,7 @@ impl<T, E> Result<T, E> {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub const fn is_ok(&self) -> bool { pub const fn is_ok(&self) -> bool {
match *self { matches!(*self, Ok(_))
Ok(_) => true,
Err(_) => false,
}
} }
/// Returns `true` if the result is [`Err`]. /// Returns `true` if the result is [`Err`].

View file

@ -46,10 +46,7 @@ pub trait Pattern<'a>: Sized {
/// Checks whether the pattern matches at the front of the haystack /// Checks whether the pattern matches at the front of the haystack
#[inline] #[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool { fn is_prefix_of(self, haystack: &'a str) -> bool {
match self.into_searcher(haystack).next() { matches!(self.into_searcher(haystack).next(), SearchStep::Match(0, _))
SearchStep::Match(0, _) => true,
_ => false,
}
} }
/// Checks whether the pattern matches at the back of the haystack /// Checks whether the pattern matches at the back of the haystack
@ -58,10 +55,7 @@ pub trait Pattern<'a>: Sized {
where where
Self::Searcher: ReverseSearcher<'a>, Self::Searcher: ReverseSearcher<'a>,
{ {
match self.into_searcher(haystack).next_back() { matches!(self.into_searcher(haystack).next_back(), SearchStep::Match(_, j) if haystack.len() == j)
SearchStep::Match(_, j) if haystack.len() == j => true,
_ => false,
}
} }
} }

View file

@ -39,10 +39,7 @@ impl<T> Poll<T> {
#[inline] #[inline]
#[stable(feature = "futures_api", since = "1.36.0")] #[stable(feature = "futures_api", since = "1.36.0")]
pub fn is_ready(&self) -> bool { pub fn is_ready(&self) -> bool {
match *self { matches!(*self, Poll::Ready(_))
Poll::Ready(_) => true,
Poll::Pending => false,
}
} }
/// Returns `true` if this is `Poll::Pending` /// Returns `true` if this is `Poll::Pending`

View file

@ -227,10 +227,7 @@ impl SocketAddr {
/// ``` /// ```
#[stable(feature = "sockaddr_checker", since = "1.16.0")] #[stable(feature = "sockaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool { pub fn is_ipv4(&self) -> bool {
match *self { matches!(*self, SocketAddr::V4(_))
SocketAddr::V4(_) => true,
SocketAddr::V6(_) => false,
}
} }
/// Returns [`true`] if the [IP address] in this `SocketAddr` is an /// Returns [`true`] if the [IP address] in this `SocketAddr` is an
@ -252,10 +249,7 @@ impl SocketAddr {
/// ``` /// ```
#[stable(feature = "sockaddr_checker", since = "1.16.0")] #[stable(feature = "sockaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool { pub fn is_ipv6(&self) -> bool {
match *self { matches!(*self, SocketAddr::V6(_))
SocketAddr::V4(_) => false,
SocketAddr::V6(_) => true,
}
} }
} }

View file

@ -281,10 +281,7 @@ impl IpAddr {
/// ``` /// ```
#[stable(feature = "ipaddr_checker", since = "1.16.0")] #[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool { pub fn is_ipv4(&self) -> bool {
match self { matches!(self, IpAddr::V4(_))
IpAddr::V4(_) => true,
IpAddr::V6(_) => false,
}
} }
/// Returns [`true`] if this address is an [IPv6 address], and [`false`] otherwise. /// Returns [`true`] if this address is an [IPv6 address], and [`false`] otherwise.
@ -303,10 +300,7 @@ impl IpAddr {
/// ``` /// ```
#[stable(feature = "ipaddr_checker", since = "1.16.0")] #[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool { pub fn is_ipv6(&self) -> bool {
match self { matches!(self, IpAddr::V6(_))
IpAddr::V4(_) => false,
IpAddr::V6(_) => true,
}
} }
} }

View file

@ -224,18 +224,12 @@ impl<'a> Prefix<'a> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn is_verbatim(&self) -> bool { pub fn is_verbatim(&self) -> bool {
use self::Prefix::*; use self::Prefix::*;
match *self { matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..) => true,
_ => false,
}
} }
#[inline] #[inline]
fn is_drive(&self) -> bool { fn is_drive(&self) -> bool {
match *self { matches!(*self, Prefix::Disk(_))
Prefix::Disk(_) => true,
_ => false,
}
} }
#[inline] #[inline]

View file

@ -199,10 +199,7 @@ mod tests {
// At this point, all spawned threads should be blocked, // At this point, all spawned threads should be blocked,
// so we shouldn't get anything from the port // so we shouldn't get anything from the port
assert!(match rx.try_recv() { assert!(matches!(rx.try_recv(), Err(TryRecvError::Empty)));
Err(TryRecvError::Empty) => true,
_ => false,
});
let mut leader_found = barrier.wait().is_leader(); let mut leader_found = barrier.wait().is_leader();

View file

@ -118,12 +118,7 @@ impl<T> Packet<T> {
// Just tests whether this channel has been sent on or not, this is only // Just tests whether this channel has been sent on or not, this is only
// safe to use from the sender. // safe to use from the sender.
pub fn sent(&self) -> bool { pub fn sent(&self) -> bool {
unsafe { unsafe { !matches!(*self.upgrade.get(), NothingSent) }
match *self.upgrade.get() {
NothingSent => false,
_ => true,
}
}
} }
pub fn recv(&self, deadline: Option<Instant>) -> Result<T, Failure<T>> { pub fn recv(&self, deadline: Option<Instant>) -> Result<T, Failure<T>> {