Skip to content

Commit 0c8f467

Browse files
committed
Add PartialOrd and Ord implementation to ArrayString
Fixes #50.
1 parent d3c1055 commit 0c8f467

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/array_string.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Borrow;
2+
use std::cmp;
23
use std::fmt;
34
use std::hash::{Hash, Hasher};
45
use std::mem;
@@ -281,10 +282,45 @@ impl<A: Array<Item=u8> + Copy> Clone for ArrayString<A> {
281282
fn clone(&self) -> ArrayString<A> {
282283
*self
283284
}
284-
285285
fn clone_from(&mut self, rhs: &Self) {
286286
// guaranteed to fit due to types matching.
287287
self.clear();
288288
self.push_str(rhs).ok();
289289
}
290290
}
291+
292+
impl<A: Array<Item=u8>> PartialOrd for ArrayString<A> {
293+
fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
294+
(**self).partial_cmp(&**rhs)
295+
}
296+
fn lt(&self, rhs: &Self) -> bool { **self < **rhs }
297+
fn le(&self, rhs: &Self) -> bool { **self <= **rhs }
298+
fn gt(&self, rhs: &Self) -> bool { **self > **rhs }
299+
fn ge(&self, rhs: &Self) -> bool { **self >= **rhs }
300+
}
301+
302+
impl<A: Array<Item=u8>> PartialOrd<str> for ArrayString<A> {
303+
fn partial_cmp(&self, rhs: &str) -> Option<cmp::Ordering> {
304+
(**self).partial_cmp(rhs)
305+
}
306+
fn lt(&self, rhs: &str) -> bool { &**self < rhs }
307+
fn le(&self, rhs: &str) -> bool { &**self <= rhs }
308+
fn gt(&self, rhs: &str) -> bool { &**self > rhs }
309+
fn ge(&self, rhs: &str) -> bool { &**self >= rhs }
310+
}
311+
312+
impl<A: Array<Item=u8>> PartialOrd<ArrayString<A>> for str {
313+
fn partial_cmp(&self, rhs: &ArrayString<A>) -> Option<cmp::Ordering> {
314+
self.partial_cmp(&**rhs)
315+
}
316+
fn lt(&self, rhs: &ArrayString<A>) -> bool { self < &**rhs }
317+
fn le(&self, rhs: &ArrayString<A>) -> bool { self <= &**rhs }
318+
fn gt(&self, rhs: &ArrayString<A>) -> bool { self > &**rhs }
319+
fn ge(&self, rhs: &ArrayString<A>) -> bool { self >= &**rhs }
320+
}
321+
322+
impl<A: Array<Item=u8>> Ord for ArrayString<A> {
323+
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
324+
(**self).cmp(&**rhs)
325+
}
326+
}

0 commit comments

Comments
 (0)