From 0215ceb532ff2254e51713aa7ecacd0232f581b1 Mon Sep 17 00:00:00 2001 From: Tobias Decking Date: Sun, 6 Sep 2026 15:19:31 +0200 Subject: [PATCH 1/2] comparison refactor --- src/comparisons.rs | 88 ++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/src/comparisons.rs b/src/comparisons.rs index 62b6815..f785c6a 100644 --- a/src/comparisons.rs +++ b/src/comparisons.rs @@ -1,59 +1,57 @@ -use crate::SmallVec; - -impl PartialEq> for SmallVec -where T: PartialEq -{ - #[inline] - fn eq(&self, other: &SmallVec) -> bool { - self.as_slice().eq(other.as_slice()) +use { + crate::SmallVec, + alloc::{ + borrow::Cow, + collections::VecDeque, + vec::Vec } -} -impl Eq for SmallVec where T: Eq {} +}; -impl PartialEq<[U; M]> for SmallVec -where T: PartialEq -{ - #[inline] - fn eq(&self, other: &[U; M]) -> bool { - self[..] == other[..] - } +macro_rules! __impl_slice_eq1 { + ([$($vars:tt)*] $lhs:ty, $rhs:ty $(where $ty:ty: $bound:ident)?) => { + impl PartialEq<$rhs> for $lhs + where + T: PartialEq, + $($ty: $bound)? + { + #[inline] + fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } + #[inline] + fn ne(&self, other: &$rhs) -> bool { self[..] != other[..] } + } + }; } -impl PartialEq<&[U; M]> for SmallVec -where T: PartialEq -{ - #[inline] - fn eq(&self, other: &&[U; M]) -> bool { - self[..] == other[..] - } -} - -impl PartialEq<[U]> for SmallVec -where T: PartialEq -{ - #[inline] - fn eq(&self, other: &[U]) -> bool { - self[..] == other[..] - } -} +__impl_slice_eq1! { [const N: usize, const M: usize] SmallVec, SmallVec } +__impl_slice_eq1! { [const N: usize, const M: usize] SmallVec, [U; N] } +__impl_slice_eq1! { [const N: usize, const M: usize] SmallVec, &[U; N] } +__impl_slice_eq1! { [const N: usize] SmallVec, [U] } +__impl_slice_eq1! { [const N: usize] SmallVec, &[U] } +__impl_slice_eq1! { [const N: usize] SmallVec, &mut [U] } +__impl_slice_eq1! { [const N: usize] [T], SmallVec } +__impl_slice_eq1! { [const N: usize] &[T], SmallVec } +__impl_slice_eq1! { [const N: usize] &mut [T], SmallVec } +__impl_slice_eq1! { [const N: usize] Vec, SmallVec } +__impl_slice_eq1! { [const N: usize] SmallVec, Vec } +__impl_slice_eq1! { [const N: usize] Cow<'_, [T]>, SmallVec where T: Clone } +__impl_slice_eq1! { [const N: usize] SmallVec, Cow<'_, [U]> where U: Clone } -impl PartialEq<&[U]> for SmallVec +impl PartialEq> for VecDeque where T: PartialEq { #[inline] - fn eq(&self, other: &&[U]) -> bool { - self[..] == other[..] + fn eq(&self, other: &SmallVec) -> bool { + let other = other.as_slice(); + if self.len() != other.len() { + return false; + } + let (sa, sb) = self.as_slices(); + let (oa, ob) = other[..].split_at(sa.len()); + sa == oa && sb == ob } } -impl PartialEq<&mut [U]> for SmallVec -where T: PartialEq -{ - #[inline] - fn eq(&self, other: &&mut [U]) -> bool { - self[..] == other[..] - } -} +impl Eq for SmallVec where T: Eq {} impl PartialOrd for SmallVec where T: PartialOrd From 7396a5216075c70455d2a537d057478671dc5e6a Mon Sep 17 00:00:00 2001 From: Tobias Decking Date: Sun, 6 Sep 2026 15:37:11 +0200 Subject: [PATCH 2/2] clippy --- src/comparisons.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/comparisons.rs b/src/comparisons.rs index f785c6a..3fb7e6a 100644 --- a/src/comparisons.rs +++ b/src/comparisons.rs @@ -16,8 +16,6 @@ macro_rules! __impl_slice_eq1 { { #[inline] fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } - #[inline] - fn ne(&self, other: &$rhs) -> bool { self[..] != other[..] } } }; }