From 946aab6ae43f6dc0bd0974d4d7a365e07f2ee5e6 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 23 May 2016 23:14:07 -0400 Subject: [PATCH] Implement PartialEq, Eq, PartialOrd, Ord on SmallVec. --- lib.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lib.rs b/lib.rs index 2a0339b..e4e3765 100644 --- a/lib.rs +++ b/lib.rs @@ -419,6 +419,30 @@ impl Clone for SmallVec where A::Item: Clone { } } +impl PartialEq> for SmallVec + where A::Item: PartialEq { + #[inline] + fn eq(&self, other: &SmallVec) -> bool { self[..] == other[..] } + #[inline] + fn ne(&self, other: &SmallVec) -> bool { self[..] != other[..] } +} + +impl Eq for SmallVec where A::Item: Eq {} + +impl PartialOrd for SmallVec where A::Item: PartialOrd { + #[inline] + fn partial_cmp(&self, other: &SmallVec) -> Option { + PartialOrd::partial_cmp(&**self, &**other) + } +} + +impl Ord for SmallVec where A::Item: Ord { + #[inline] + fn cmp(&self, other: &SmallVec) -> cmp::Ordering { + Ord::cmp(&**self, &**other) + } +} + unsafe impl Send for SmallVec where A::Item: Send {} // TODO: Remove these and its users. @@ -606,4 +630,43 @@ pub mod tests { let mut v = SmallVec::<[_; 1]>::new(); v.push(DropPanic); } + + #[test] + fn test_eq() { + let mut a: SmallVec<[u32; 2]> = SmallVec::new(); + let mut b: SmallVec<[u32; 2]> = SmallVec::new(); + let mut c: SmallVec<[u32; 2]> = SmallVec::new(); + // a = [1, 2] + a.push(1); + a.push(2); + // b = [1, 2] + b.push(1); + b.push(2); + // c = [3, 4] + c.push(3); + c.push(4); + + assert!(a == b); + assert!(a != c); + } + + #[test] + fn test_ord() { + let mut a: SmallVec<[u32; 2]> = SmallVec::new(); + let mut b: SmallVec<[u32; 2]> = SmallVec::new(); + let mut c: SmallVec<[u32; 2]> = SmallVec::new(); + // a = [1] + a.push(1); + // b = [1, 1] + b.push(1); + b.push(1); + // c = [1, 2] + c.push(1); + c.push(2); + + assert!(a < b); + assert!(b > a); + assert!(b < c); + assert!(c > b); + } }