diff --git a/src/string.rs b/src/string.rs index 5848095d..faec39e1 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,4 +1,4 @@ -use core::{fmt, fmt::Write, hash, ops, str}; +use core::{cmp::Ordering, fmt, fmt::Write, hash, ops, str}; use hash32; @@ -439,6 +439,20 @@ impl PartialEq> for &str { impl Eq for String {} +impl PartialOrd> for String { + #[inline] + fn partial_cmp(&self, other: &String) -> Option { + PartialOrd::partial_cmp(&**self, &**other) + } +} + +impl Ord for String { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + Ord::cmp(&**self, &**other) + } +} + macro_rules! impl_from_num { ($num:ty, $size:expr) => { impl From<$num> for String { @@ -480,6 +494,22 @@ mod tests { assert_eq!(s2, "abcd efgh"); } + #[test] + fn cmp() { + let s1: String<4> = String::from("abcd"); + let s2: String<4> = String::from("zzzz"); + + assert!(s1 < s2); + } + + #[test] + fn cmp_heterogenous_size() { + let s1: String<4> = String::from("abcd"); + let s2: String<8> = String::from("zzzz"); + + assert!(s1 < s2); + } + #[test] fn debug() { use core::fmt::Write; diff --git a/src/vec.rs b/src/vec.rs index 3dee4b46..73c160c6 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1,4 +1,4 @@ -use core::{fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr, slice}; +use core::{cmp::Ordering, fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr, slice}; use hash32; /// A fixed capacity [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html) @@ -764,6 +764,25 @@ where // Implements Eq if underlying data is Eq impl Eq for Vec where T: Eq {} +impl PartialOrd> for Vec +where + T: PartialOrd, +{ + fn partial_cmp(&self, other: &Vec) -> Option { + PartialOrd::partial_cmp(&**self, &**other) + } +} + +impl Ord for Vec +where + T: Ord, +{ + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + Ord::cmp(&**self, &**other) + } +} + impl ops::Deref for Vec { type Target = [T]; @@ -888,6 +907,32 @@ mod tests { assert_eq!(xs, ys); } + #[test] + fn cmp() { + let mut xs: Vec = Vec::new(); + let mut ys: Vec = Vec::new(); + + assert_eq!(xs, ys); + + xs.push(1).unwrap(); + ys.push(2).unwrap(); + + assert!(xs < ys); + } + + #[test] + fn cmp_heterogenous_size() { + let mut xs: Vec = Vec::new(); + let mut ys: Vec = Vec::new(); + + assert_eq!(xs, ys); + + xs.push(1).unwrap(); + ys.push(2).unwrap(); + + assert!(xs < ys); + } + #[test] fn full() { let mut v: Vec = Vec::new();