Skip to content

Commit d1b53ba

Browse files
authored
Merge pull request #226 from bbqsrc/master
Add PartialOrd and Ord to Vec and String
2 parents 186b2b5 + 345cafb commit d1b53ba

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/string.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::{fmt, fmt::Write, hash, ops, str};
1+
use core::{cmp::Ordering, fmt, fmt::Write, hash, ops, str};
22

33
use hash32;
44

@@ -439,6 +439,20 @@ impl<const N: usize> PartialEq<String<N>> for &str {
439439

440440
impl<const N: usize> Eq for String<N> {}
441441

442+
impl<const N1: usize, const N2: usize> PartialOrd<String<N2>> for String<N1> {
443+
#[inline]
444+
fn partial_cmp(&self, other: &String<N2>) -> Option<Ordering> {
445+
PartialOrd::partial_cmp(&**self, &**other)
446+
}
447+
}
448+
449+
impl<const N: usize> Ord for String<N> {
450+
#[inline]
451+
fn cmp(&self, other: &Self) -> Ordering {
452+
Ord::cmp(&**self, &**other)
453+
}
454+
}
455+
442456
macro_rules! impl_from_num {
443457
($num:ty, $size:expr) => {
444458
impl<const N: usize> From<$num> for String<N> {
@@ -480,6 +494,22 @@ mod tests {
480494
assert_eq!(s2, "abcd efgh");
481495
}
482496

497+
#[test]
498+
fn cmp() {
499+
let s1: String<4> = String::from("abcd");
500+
let s2: String<4> = String::from("zzzz");
501+
502+
assert!(s1 < s2);
503+
}
504+
505+
#[test]
506+
fn cmp_heterogenous_size() {
507+
let s1: String<4> = String::from("abcd");
508+
let s2: String<8> = String::from("zzzz");
509+
510+
assert!(s1 < s2);
511+
}
512+
483513
#[test]
484514
fn debug() {
485515
use core::fmt::Write;

src/vec.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::{fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr, slice};
1+
use core::{cmp::Ordering, fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr, slice};
22
use hash32;
33

44
/// A fixed capacity [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html)
@@ -764,6 +764,25 @@ where
764764
// Implements Eq if underlying data is Eq
765765
impl<T, const N: usize> Eq for Vec<T, N> where T: Eq {}
766766

767+
impl<T, const N1: usize, const N2: usize> PartialOrd<Vec<T, N2>> for Vec<T, N1>
768+
where
769+
T: PartialOrd,
770+
{
771+
fn partial_cmp(&self, other: &Vec<T, N2>) -> Option<Ordering> {
772+
PartialOrd::partial_cmp(&**self, &**other)
773+
}
774+
}
775+
776+
impl<T, const N: usize> Ord for Vec<T, N>
777+
where
778+
T: Ord,
779+
{
780+
#[inline]
781+
fn cmp(&self, other: &Self) -> Ordering {
782+
Ord::cmp(&**self, &**other)
783+
}
784+
}
785+
767786
impl<T, const N: usize> ops::Deref for Vec<T, N> {
768787
type Target = [T];
769788

@@ -888,6 +907,32 @@ mod tests {
888907
assert_eq!(xs, ys);
889908
}
890909

910+
#[test]
911+
fn cmp() {
912+
let mut xs: Vec<i32, 4> = Vec::new();
913+
let mut ys: Vec<i32, 4> = Vec::new();
914+
915+
assert_eq!(xs, ys);
916+
917+
xs.push(1).unwrap();
918+
ys.push(2).unwrap();
919+
920+
assert!(xs < ys);
921+
}
922+
923+
#[test]
924+
fn cmp_heterogenous_size() {
925+
let mut xs: Vec<i32, 4> = Vec::new();
926+
let mut ys: Vec<i32, 8> = Vec::new();
927+
928+
assert_eq!(xs, ys);
929+
930+
xs.push(1).unwrap();
931+
ys.push(2).unwrap();
932+
933+
assert!(xs < ys);
934+
}
935+
891936
#[test]
892937
fn full() {
893938
let mut v: Vec<i32, 4> = Vec::new();

0 commit comments

Comments
 (0)