Skip to content

Commit 946aab6

Browse files
committed
Implement PartialEq, Eq, PartialOrd, Ord on SmallVec.
1 parent e590b8d commit 946aab6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,30 @@ impl<A: Array> Clone for SmallVec<A> where A::Item: Clone {
419419
}
420420
}
421421

422+
impl<A: Array, B: Array> PartialEq<SmallVec<B>> for SmallVec<A>
423+
where A::Item: PartialEq<B::Item> {
424+
#[inline]
425+
fn eq(&self, other: &SmallVec<B>) -> bool { self[..] == other[..] }
426+
#[inline]
427+
fn ne(&self, other: &SmallVec<B>) -> bool { self[..] != other[..] }
428+
}
429+
430+
impl<A: Array> Eq for SmallVec<A> where A::Item: Eq {}
431+
432+
impl<A: Array> PartialOrd for SmallVec<A> where A::Item: PartialOrd {
433+
#[inline]
434+
fn partial_cmp(&self, other: &SmallVec<A>) -> Option<cmp::Ordering> {
435+
PartialOrd::partial_cmp(&**self, &**other)
436+
}
437+
}
438+
439+
impl<A: Array> Ord for SmallVec<A> where A::Item: Ord {
440+
#[inline]
441+
fn cmp(&self, other: &SmallVec<A>) -> cmp::Ordering {
442+
Ord::cmp(&**self, &**other)
443+
}
444+
}
445+
422446
unsafe impl<A: Array> Send for SmallVec<A> where A::Item: Send {}
423447

424448
// TODO: Remove these and its users.
@@ -606,4 +630,43 @@ pub mod tests {
606630
let mut v = SmallVec::<[_; 1]>::new();
607631
v.push(DropPanic);
608632
}
633+
634+
#[test]
635+
fn test_eq() {
636+
let mut a: SmallVec<[u32; 2]> = SmallVec::new();
637+
let mut b: SmallVec<[u32; 2]> = SmallVec::new();
638+
let mut c: SmallVec<[u32; 2]> = SmallVec::new();
639+
// a = [1, 2]
640+
a.push(1);
641+
a.push(2);
642+
// b = [1, 2]
643+
b.push(1);
644+
b.push(2);
645+
// c = [3, 4]
646+
c.push(3);
647+
c.push(4);
648+
649+
assert!(a == b);
650+
assert!(a != c);
651+
}
652+
653+
#[test]
654+
fn test_ord() {
655+
let mut a: SmallVec<[u32; 2]> = SmallVec::new();
656+
let mut b: SmallVec<[u32; 2]> = SmallVec::new();
657+
let mut c: SmallVec<[u32; 2]> = SmallVec::new();
658+
// a = [1]
659+
a.push(1);
660+
// b = [1, 1]
661+
b.push(1);
662+
b.push(1);
663+
// c = [1, 2]
664+
c.push(1);
665+
c.push(2);
666+
667+
assert!(a < b);
668+
assert!(b > a);
669+
assert!(b < c);
670+
assert!(c > b);
671+
}
609672
}

0 commit comments

Comments
 (0)