Skip to content

Commit bbd1d32

Browse files
committed
Implement Ord
1 parent 19f9c95 commit bbd1d32

File tree

1 file changed

+115
-57
lines changed

1 file changed

+115
-57
lines changed

src/c_string.rs

Lines changed: 115 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{vec::Vec, CapacityError};
44
use core::{
55
borrow::Borrow,
6+
cmp::Ordering,
67
error::Error,
78
ffi::{c_char, CStr},
89
fmt,
@@ -292,21 +293,9 @@ impl<const N: usize> Deref for CString<N> {
292293
}
293294
}
294295

295-
impl<const N: usize, const M: usize> PartialEq<CString<M>> for CString<N> {
296-
fn eq(&self, rhs: &CString<M>) -> bool {
297-
self.as_c_str() == rhs.as_c_str()
298-
}
299-
}
300-
301-
impl<const N: usize> PartialEq<CStr> for CString<N> {
302-
fn eq(&self, rhs: &CStr) -> bool {
303-
self.as_c_str() == rhs
304-
}
305-
}
306-
307-
impl<const N: usize> PartialEq<&CStr> for CString<N> {
308-
fn eq(&self, rhs: &&CStr) -> bool {
309-
self.as_c_str() == *rhs
296+
impl<const N: usize, T: AsRef<CStr>> PartialEq<T> for CString<N> {
297+
fn eq(&self, rhs: &T) -> bool {
298+
self.as_c_str() == rhs.as_ref()
310299
}
311300
}
312301

@@ -324,6 +313,30 @@ impl<const N: usize> PartialEq<CString<N>> for &CStr {
324313

325314
impl<const N: usize> Eq for CString<N> {}
326315

316+
impl<const N: usize, T: AsRef<CStr>> PartialOrd<T> for CString<N> {
317+
fn partial_cmp(&self, rhs: &T) -> Option<Ordering> {
318+
self.as_c_str().partial_cmp(rhs.as_ref())
319+
}
320+
}
321+
322+
impl<const N: usize> PartialOrd<CString<N>> for CStr {
323+
fn partial_cmp(&self, rhs: &CString<N>) -> Option<Ordering> {
324+
self.as_ref().partial_cmp(rhs.as_c_str())
325+
}
326+
}
327+
328+
impl<const N: usize> PartialOrd<CString<N>> for &CStr {
329+
fn partial_cmp(&self, rhs: &CString<N>) -> Option<Ordering> {
330+
self.as_ref().partial_cmp(rhs.as_c_str())
331+
}
332+
}
333+
334+
impl<const N: usize> Ord for CString<N> {
335+
fn cmp(&self, rhs: &Self) -> Ordering {
336+
self.as_c_str().cmp(rhs.as_c_str())
337+
}
338+
}
339+
327340
/// An error to extend [`CString`] with bytes.
328341
#[derive(Debug)]
329342
pub enum ExtendError {
@@ -467,50 +480,95 @@ mod tests {
467480
assert_eq!(Borrow::<CStr>::borrow(&string), c"foo");
468481
}
469482

470-
#[test]
471-
fn equal() {
472-
// Empty strings
473-
assert!(CString::<1>::new() == CString::<1>::new());
474-
assert!(CString::<1>::new() == CString::<2>::new());
475-
assert!(CString::<1>::from_bytes_with_nul(b"\0").unwrap() == CString::<3>::new());
476-
477-
// Single character
478-
assert!(
479-
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
480-
== CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
481-
);
482-
assert!(
483-
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
484-
== CString::<3>::from_bytes_with_nul(b"a\0").unwrap()
485-
);
486-
assert!(
487-
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
488-
!= CString::<2>::from_bytes_with_nul(b"b\0").unwrap()
489-
);
483+
mod equality {
484+
use super::*;
485+
486+
#[test]
487+
fn c_string() {
488+
// Empty strings
489+
assert!(CString::<1>::new() == CString::<1>::new());
490+
assert!(CString::<1>::new() == CString::<2>::new());
491+
assert!(CString::<1>::from_bytes_with_nul(b"\0").unwrap() == CString::<3>::new());
492+
493+
// Single character
494+
assert!(
495+
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
496+
== CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
497+
);
498+
assert!(
499+
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
500+
== CString::<3>::from_bytes_with_nul(b"a\0").unwrap()
501+
);
502+
assert!(
503+
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
504+
!= CString::<2>::from_bytes_with_nul(b"b\0").unwrap()
505+
);
506+
507+
// Multiple characters
508+
assert!(
509+
CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
510+
== CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
511+
);
512+
assert!(
513+
CString::<3>::from_bytes_with_nul(b"ab\0").unwrap()
514+
!= CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
515+
);
516+
}
490517

491-
// Multiple characters
492-
assert!(
493-
CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
494-
== CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
495-
);
496-
assert!(
497-
CString::<3>::from_bytes_with_nul(b"ab\0").unwrap()
498-
!= CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
499-
);
518+
#[test]
519+
fn c_str() {
520+
// Empty strings
521+
assert!(CString::<1>::new() == c"");
522+
assert!(c"" == CString::<1>::new());
523+
524+
// Single character
525+
assert!(CString::<2>::from_bytes_with_nul(b"a\0").unwrap() == c"a");
526+
assert!(c"a" == CString::<2>::from_bytes_with_nul(b"a\0").unwrap());
527+
528+
// Multiple characters
529+
assert!(CString::<4>::from_bytes_with_nul(b"abc\0").unwrap() == c"abc");
530+
assert!(c"abc" == CString::<4>::from_bytes_with_nul(b"abc\0").unwrap());
531+
}
500532
}
501533

502-
#[test]
503-
fn equal_with_c_str() {
504-
// Empty strings
505-
assert!(CString::<1>::new() == c"");
506-
assert!(c"" == CString::<1>::new());
507-
508-
// Single character
509-
assert!(CString::<2>::from_bytes_with_nul(b"a\0").unwrap() == c"a");
510-
assert!(c"a" == CString::<2>::from_bytes_with_nul(b"a\0").unwrap());
511-
512-
// Multiple characters
513-
assert!(CString::<4>::from_bytes_with_nul(b"abc\0").unwrap() == c"abc");
514-
assert!(c"abc" == CString::<4>::from_bytes_with_nul(b"abc\0").unwrap());
534+
mod ordering {
535+
use super::*;
536+
537+
#[test]
538+
fn c_string() {
539+
assert_eq!(
540+
CString::<1>::new().partial_cmp(&CString::<1>::new()),
541+
Some(Ordering::Equal)
542+
);
543+
assert_eq!(
544+
CString::<2>::from_bytes_with_nul(b"a\0")
545+
.unwrap()
546+
.partial_cmp(&CString::<2>::from_bytes_with_nul(b"b\0").unwrap()),
547+
Some(Ordering::Less)
548+
);
549+
assert_eq!(
550+
CString::<2>::from_bytes_with_nul(b"b\0")
551+
.unwrap()
552+
.partial_cmp(&CString::<2>::from_bytes_with_nul(b"a\0").unwrap()),
553+
Some(Ordering::Greater)
554+
);
555+
}
556+
557+
#[test]
558+
fn c_str() {
559+
assert_eq!(CString::<1>::new().partial_cmp(&c""), Some(Ordering::Equal));
560+
assert_eq!(
561+
CString::<2>::from_bytes_with_nul(b"a\0")
562+
.unwrap()
563+
.partial_cmp(&c"b"),
564+
Some(Ordering::Less)
565+
);
566+
assert_eq!(
567+
CString::<2>::from_bytes_with_nul(b"b\0")
568+
.unwrap()
569+
.partial_cmp(&c"a"),
570+
Some(Ordering::Greater)
571+
);
572+
}
515573
}
516574
}

0 commit comments

Comments
 (0)