@@ -1593,12 +1593,30 @@ impl<const N: usize, const M: usize, T, U> PartialEq<CircularBuffer<M, U>> for C
1593
1593
where T : PartialEq < U >
1594
1594
{
1595
1595
fn eq ( & self , other : & CircularBuffer < M , U > ) -> bool {
1596
- // TODO Optimize
1597
1596
if self . len ( ) != other. len ( ) {
1598
1597
return false ;
1599
1598
}
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
+ }
1602
1620
}
1603
1621
}
1604
1622
@@ -1608,18 +1626,23 @@ impl<const N: usize, T, U> PartialEq<[U]> for CircularBuffer<N, T>
1608
1626
where T : PartialEq < U >
1609
1627
{
1610
1628
fn eq ( & self , other : & [ U ] ) -> bool {
1611
- // TODO Optimize
1612
1629
if self . len ( ) != other. len ( ) {
1613
1630
return false ;
1614
1631
}
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
1617
1639
}
1618
1640
}
1619
1641
1620
1642
impl < const N : usize , const M : usize , T , U > PartialEq < [ U ; M ] > for CircularBuffer < N , T >
1621
1643
where T : PartialEq < U >
1622
1644
{
1645
+ #[ inline]
1623
1646
fn eq ( & self , other : & [ U ; M ] ) -> bool {
1624
1647
self == & other[ ..]
1625
1648
}
@@ -1628,52 +1651,36 @@ impl<const N: usize, const M: usize, T, U> PartialEq<[U; M]> for CircularBuffer<
1628
1651
impl < ' a , const N : usize , T , U > PartialEq < & ' a [ U ] > for CircularBuffer < N , T >
1629
1652
where T : PartialEq < U >
1630
1653
{
1654
+ #[ inline]
1631
1655
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
1638
1657
}
1639
1658
}
1640
1659
1641
1660
impl < ' a , const N : usize , T , U > PartialEq < & ' a mut [ U ] > for CircularBuffer < N , T >
1642
1661
where T : PartialEq < U >
1643
1662
{
1663
+ #[ inline]
1644
1664
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
1651
1666
}
1652
1667
}
1653
1668
1654
1669
impl < ' a , const N : usize , const M : usize , T , U > PartialEq < & ' a [ U ; M ] > for CircularBuffer < N , T >
1655
1670
where T : PartialEq < U >
1656
1671
{
1672
+ #[ inline]
1657
1673
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
1664
1675
}
1665
1676
}
1666
1677
1667
1678
impl < ' a , const N : usize , const M : usize , T , U > PartialEq < & ' a mut [ U ; M ] > for CircularBuffer < N , T >
1668
1679
where T : PartialEq < U >
1669
1680
{
1681
+ #[ inline]
1670
1682
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
1677
1684
}
1678
1685
}
1679
1686
0 commit comments