Skip to content

Commit 7db8dd1

Browse files
Optimized all PartialEq implementations
1 parent e7d3370 commit 7db8dd1

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

src/lib.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,12 +1593,30 @@ impl<const N: usize, const M: usize, T, U> PartialEq<CircularBuffer<M, U>> for C
15931593
where T: PartialEq<U>
15941594
{
15951595
fn eq(&self, other: &CircularBuffer<M, U>) -> bool {
1596-
// TODO Optimize
15971596
if self.len() != other.len() {
15981597
return false;
15991598
}
1600-
self.iter().zip(other.iter())
1601-
.all(|(x, y)| x == y)
1599+
1600+
let (a_left, a_right) = self.as_slices();
1601+
let (b_left, b_right) = other.as_slices();
1602+
1603+
match a_left.len().cmp(&b_left.len()) {
1604+
Ordering::Less => {
1605+
let x = a_left.len();
1606+
let y = b_left.len() - x;
1607+
a_left[..] == b_left[..x] && a_right[..y] == b_left[x..] && a_right[y..] == b_right[..]
1608+
},
1609+
Ordering::Greater => {
1610+
let x = b_left.len();
1611+
let y = a_left.len() - x;
1612+
a_left[..x] == b_left[..] && a_right[x..] == b_left[..y] && a_right[..] == b_right[y..]
1613+
},
1614+
Ordering::Equal => {
1615+
debug_assert_eq!(a_left.len(), b_left.len());
1616+
debug_assert_eq!(a_right.len(), b_right.len());
1617+
a_left == b_left && a_right == b_right
1618+
},
1619+
}
16021620
}
16031621
}
16041622

@@ -1608,18 +1626,23 @@ impl<const N: usize, T, U> PartialEq<[U]> for CircularBuffer<N, T>
16081626
where T: PartialEq<U>
16091627
{
16101628
fn eq(&self, other: &[U]) -> bool {
1611-
// TODO Optimize
16121629
if self.len() != other.len() {
16131630
return false;
16141631
}
1615-
self.iter().zip(other.iter())
1616-
.all(|(x, y)| x == y)
1632+
1633+
let (a_left, a_right) = self.as_slices();
1634+
let (b_left, b_right) = other.split_at(a_left.len());
1635+
1636+
debug_assert_eq!(a_left.len(), b_left.len());
1637+
debug_assert_eq!(a_right.len(), b_right.len());
1638+
a_left == b_left && a_right == b_right
16171639
}
16181640
}
16191641

16201642
impl<const N: usize, const M: usize, T, U> PartialEq<[U; M]> for CircularBuffer<N, T>
16211643
where T: PartialEq<U>
16221644
{
1645+
#[inline]
16231646
fn eq(&self, other: &[U; M]) -> bool {
16241647
self == &other[..]
16251648
}
@@ -1628,52 +1651,36 @@ impl<const N: usize, const M: usize, T, U> PartialEq<[U; M]> for CircularBuffer<
16281651
impl<'a, const N: usize, T, U> PartialEq<&'a [U]> for CircularBuffer<N, T>
16291652
where T: PartialEq<U>
16301653
{
1654+
#[inline]
16311655
fn eq(&self, other: &&'a [U]) -> bool {
1632-
// TODO Optimize
1633-
if self.len() != other.len() {
1634-
return false;
1635-
}
1636-
self.iter().zip(other.iter())
1637-
.all(|(x, y)| x == y)
1656+
self == *other
16381657
}
16391658
}
16401659

16411660
impl<'a, const N: usize, T, U> PartialEq<&'a mut [U]> for CircularBuffer<N, T>
16421661
where T: PartialEq<U>
16431662
{
1663+
#[inline]
16441664
fn eq(&self, other: &&'a mut [U]) -> bool {
1645-
// TODO Optimize
1646-
if self.len() != other.len() {
1647-
return false;
1648-
}
1649-
self.iter().zip(other.iter())
1650-
.all(|(x, y)| x == y)
1665+
self == *other
16511666
}
16521667
}
16531668

16541669
impl<'a, const N: usize, const M: usize, T, U> PartialEq<&'a [U; M]> for CircularBuffer<N, T>
16551670
where T: PartialEq<U>
16561671
{
1672+
#[inline]
16571673
fn eq(&self, other: &&'a [U; M]) -> bool {
1658-
// TODO Optimize
1659-
if self.len() != other.len() {
1660-
return false;
1661-
}
1662-
self.iter().zip(other.iter())
1663-
.all(|(x, y)| x == y)
1674+
self == *other
16641675
}
16651676
}
16661677

16671678
impl<'a, const N: usize, const M: usize, T, U> PartialEq<&'a mut [U; M]> for CircularBuffer<N, T>
16681679
where T: PartialEq<U>
16691680
{
1681+
#[inline]
16701682
fn eq(&self, other: &&'a mut [U; M]) -> bool {
1671-
// TODO Optimize
1672-
if self.len() != other.len() {
1673-
return false;
1674-
}
1675-
self.iter().zip(other.iter())
1676-
.all(|(x, y)| x == y)
1683+
self == *other
16771684
}
16781685
}
16791686

0 commit comments

Comments
 (0)