Skip to content

Commit e7eeef9

Browse files
committed
Do array-slice equality via arrays, rather than always via slices
This'll still go via slices eventually for large arrays, but this way slice comparisons to short arrays can use the same memcmp-avoidance tricks. Added some tests for all the combinations to make sure I didn't accidentally infinitely-recurse something.
1 parent 4a2bc3f commit e7eeef9

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

core/src/array/equality.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::convert::TryInto;
12
use crate::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
23
use crate::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
34

@@ -23,11 +24,19 @@ where
2324
{
2425
#[inline]
2526
fn eq(&self, other: &[B]) -> bool {
26-
self[..] == other[..]
27+
let b: Result<&[B; N], _> = other.try_into();
28+
match b {
29+
Ok(b) => *self == *b,
30+
Err(_) => false,
31+
}
2732
}
2833
#[inline]
2934
fn ne(&self, other: &[B]) -> bool {
30-
self[..] != other[..]
35+
let b: Result<&[B; N], _> = other.try_into();
36+
match b {
37+
Ok(b) => *self != *b,
38+
Err(_) => true,
39+
}
3140
}
3241
}
3342

@@ -38,11 +47,19 @@ where
3847
{
3948
#[inline]
4049
fn eq(&self, other: &[A; N]) -> bool {
41-
self[..] == other[..]
50+
let b: Result<&[B; N], _> = self.try_into();
51+
match b {
52+
Ok(b) => *b == *other,
53+
Err(_) => false,
54+
}
4255
}
4356
#[inline]
4457
fn ne(&self, other: &[A; N]) -> bool {
45-
self[..] != other[..]
58+
let b: Result<&[B; N], _> = self.try_into();
59+
match b {
60+
Ok(b) => *b != *other,
61+
Err(_) => true,
62+
}
4663
}
4764
}
4865

@@ -53,11 +70,11 @@ where
5370
{
5471
#[inline]
5572
fn eq(&self, other: &&[B]) -> bool {
56-
self[..] == other[..]
73+
*self == **other
5774
}
5875
#[inline]
5976
fn ne(&self, other: &&[B]) -> bool {
60-
self[..] != other[..]
77+
*self != **other
6178
}
6279
}
6380

@@ -68,11 +85,11 @@ where
6885
{
6986
#[inline]
7087
fn eq(&self, other: &[A; N]) -> bool {
71-
self[..] == other[..]
88+
**self == *other
7289
}
7390
#[inline]
7491
fn ne(&self, other: &[A; N]) -> bool {
75-
self[..] != other[..]
92+
**self != *other
7693
}
7794
}
7895

@@ -83,11 +100,11 @@ where
83100
{
84101
#[inline]
85102
fn eq(&self, other: &&mut [B]) -> bool {
86-
self[..] == other[..]
103+
*self == **other
87104
}
88105
#[inline]
89106
fn ne(&self, other: &&mut [B]) -> bool {
90-
self[..] != other[..]
107+
*self != **other
91108
}
92109
}
93110

@@ -98,11 +115,11 @@ where
98115
{
99116
#[inline]
100117
fn eq(&self, other: &[A; N]) -> bool {
101-
self[..] == other[..]
118+
**self == *other
102119
}
103120
#[inline]
104121
fn ne(&self, other: &[A; N]) -> bool {
105-
self[..] != other[..]
122+
**self != *other
106123
}
107124
}
108125

core/tests/array.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,47 @@ fn array_intoiter_advance_back_by() {
624624
assert_eq!(it.len(), 0);
625625
assert_eq!(counter.get(), 100);
626626
}
627+
628+
#[test]
629+
fn array_mixed_equality_integers() {
630+
let array3: [i32; 3] = [1, 2, 3];
631+
let array3b: [i32; 3] = [3, 2, 1];
632+
let array4: [i32; 4] = [1, 2, 3, 4];
633+
634+
let slice3: &[i32] = &{ array3 };
635+
let slice3b: &[i32] = &{ array3b };
636+
let slice4: &[i32] = &{ array4 };
637+
assert!(array3 == slice3);
638+
assert!(array3 != slice3b);
639+
assert!(array3 != slice4);
640+
assert!(slice3 == array3);
641+
assert!(slice3b != array3);
642+
assert!(slice4 != array3);
643+
644+
let mut3: &mut [i32] = &mut { array3 };
645+
let mut3b: &mut [i32] = &mut { array3b };
646+
let mut4: &mut [i32] = &mut { array4 };
647+
assert!(array3 == mut3);
648+
assert!(array3 != mut3b);
649+
assert!(array3 != mut4);
650+
assert!(mut3 == array3);
651+
assert!(mut3b != array3);
652+
assert!(mut4 != array3);
653+
}
654+
655+
#[test]
656+
fn array_mixed_equality_nans() {
657+
let array3: [f32; 3] = [1.0, std::f32::NAN, 3.0];
658+
659+
let slice3: &[f32] = &{ array3 };
660+
assert!(!(array3 == slice3));
661+
assert!(array3 != slice3);
662+
assert!(!(slice3 == array3));
663+
assert!(slice3 != array3);
664+
665+
let mut3: &mut [f32] = &mut { array3 };
666+
assert!(!(array3 == mut3));
667+
assert!(array3 != mut3);
668+
assert!(!(mut3 == array3));
669+
assert!(mut3 != array3);
670+
}

0 commit comments

Comments
 (0)