3
3
use crate :: { vec:: Vec , CapacityError } ;
4
4
use core:: {
5
5
borrow:: Borrow ,
6
+ cmp:: Ordering ,
6
7
error:: Error ,
7
8
ffi:: { c_char, CStr } ,
8
9
fmt,
@@ -292,21 +293,9 @@ impl<const N: usize> Deref for CString<N> {
292
293
}
293
294
}
294
295
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 ( )
310
299
}
311
300
}
312
301
@@ -324,6 +313,30 @@ impl<const N: usize> PartialEq<CString<N>> for &CStr {
324
313
325
314
impl < const N : usize > Eq for CString < N > { }
326
315
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
+
327
340
/// An error to extend [`CString`] with bytes.
328
341
#[ derive( Debug ) ]
329
342
pub enum ExtendError {
@@ -467,50 +480,95 @@ mod tests {
467
480
assert_eq ! ( Borrow :: <CStr >:: borrow( & string) , c"foo" ) ;
468
481
}
469
482
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
+ }
490
517
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
+ }
500
532
}
501
533
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
+ }
515
573
}
516
574
}
0 commit comments