2023-02-25 22:47:20 -08:00
|
|
|
use crate::cmp::BytewiseEq;
|
2021-12-11 15:29:52 -08:00
|
|
|
use crate::convert::TryInto;
|
2021-12-10 17:30:39 -08:00
|
|
|
|
2021-05-30 10:23:50 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
|
|
|
|
|
where
|
|
|
|
|
A: PartialEq<B>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &[B; N]) -> bool {
|
2021-05-30 10:25:41 -07:00
|
|
|
SpecArrayEq::spec_eq(self, other)
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &[B; N]) -> bool {
|
2021-05-30 10:25:41 -07:00
|
|
|
SpecArrayEq::spec_ne(self, other)
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
|
|
|
|
|
where
|
|
|
|
|
A: PartialEq<B>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &[B]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
let b: Result<&[B; N], _> = other.try_into();
|
|
|
|
|
match b {
|
|
|
|
|
Ok(b) => *self == *b,
|
|
|
|
|
Err(_) => false,
|
|
|
|
|
}
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &[B]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
let b: Result<&[B; N], _> = other.try_into();
|
|
|
|
|
match b {
|
|
|
|
|
Ok(b) => *self != *b,
|
|
|
|
|
Err(_) => true,
|
|
|
|
|
}
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
|
|
|
|
|
where
|
|
|
|
|
B: PartialEq<A>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &[A; N]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
let b: Result<&[B; N], _> = self.try_into();
|
|
|
|
|
match b {
|
|
|
|
|
Ok(b) => *b == *other,
|
|
|
|
|
Err(_) => false,
|
|
|
|
|
}
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &[A; N]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
let b: Result<&[B; N], _> = self.try_into();
|
|
|
|
|
match b {
|
|
|
|
|
Ok(b) => *b != *other,
|
|
|
|
|
Err(_) => true,
|
|
|
|
|
}
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<&[B]> for [A; N]
|
|
|
|
|
where
|
|
|
|
|
A: PartialEq<B>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &&[B]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
*self == **other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &&[B]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
*self != **other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]
|
|
|
|
|
where
|
|
|
|
|
B: PartialEq<A>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &[A; N]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
**self == *other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &[A; N]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
**self != *other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]
|
|
|
|
|
where
|
|
|
|
|
A: PartialEq<B>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &&mut [B]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
*self == **other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &&mut [B]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
*self != **other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]
|
|
|
|
|
where
|
|
|
|
|
B: PartialEq<A>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &[A; N]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
**self == *other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ne(&self, other: &[A; N]) -> bool {
|
2021-12-11 15:29:52 -08:00
|
|
|
**self != *other
|
2021-05-30 10:23:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: some less important impls are omitted to reduce code bloat
|
|
|
|
|
// __impl_slice_eq2! { [A; $N], &'b [B; $N] }
|
|
|
|
|
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<T: Eq, const N: usize> Eq for [T; N] {}
|
2021-05-30 10:25:41 -07:00
|
|
|
|
|
|
|
|
trait SpecArrayEq<Other, const N: usize>: Sized {
|
|
|
|
|
fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool;
|
|
|
|
|
fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T {
|
|
|
|
|
default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool {
|
|
|
|
|
a[..] == b[..]
|
|
|
|
|
}
|
|
|
|
|
default fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool {
|
|
|
|
|
a[..] != b[..]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 22:47:20 -08:00
|
|
|
impl<T: BytewiseEq<U>, U, const N: usize> SpecArrayEq<U, N> for T {
|
2021-05-30 10:25:41 -07:00
|
|
|
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
|
2023-02-25 22:47:20 -08:00
|
|
|
// SAFETY: Arrays are compared element-wise, and don't add any padding
|
|
|
|
|
// between elements, so when the elements are `BytewiseEq`, we can
|
|
|
|
|
// compare the entire array at once.
|
|
|
|
|
unsafe { crate::intrinsics::raw_eq(a, crate::mem::transmute(b)) }
|
2021-05-30 10:25:41 -07:00
|
|
|
}
|
|
|
|
|
fn spec_ne(a: &[T; N], b: &[U; N]) -> bool {
|
|
|
|
|
!Self::spec_eq(a, b)
|
|
|
|
|
}
|
|
|
|
|
}
|