@@ -292,6 +292,14 @@ impl<const N: usize> Deref for CString<N> {
292
292
}
293
293
}
294
294
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 > Eq for CString < N > { }
302
+
295
303
/// An error to extend [`CString`] with bytes.
296
304
#[ derive( Debug ) ]
297
305
pub enum ExtendError {
@@ -331,6 +339,11 @@ mod tests {
331
339
assert_eq ! ( empty. to_str( ) , Ok ( "" ) ) ;
332
340
}
333
341
342
+ #[ test]
343
+ fn create_with_capacity_error ( ) {
344
+ assert ! ( CString :: <1 >:: from_bytes_with_nul( b"a\0 " ) . is_err( ) )
345
+ }
346
+
334
347
#[ test]
335
348
fn push_no_bytes ( ) {
336
349
let mut c_string = CString :: < 1 > :: new ( ) ;
@@ -429,4 +442,36 @@ mod tests {
429
442
string. extend_from_bytes ( b"foo" ) . unwrap ( ) ;
430
443
assert_eq ! ( Borrow :: <CStr >:: borrow( & string) , c"foo" ) ;
431
444
}
445
+
446
+ #[ test]
447
+ fn equal ( ) {
448
+ // Empty strings
449
+ assert ! ( CString :: <1 >:: new( ) == CString :: <1 >:: new( ) ) ;
450
+ assert ! ( CString :: <1 >:: new( ) == CString :: <2 >:: new( ) ) ;
451
+ assert ! ( CString :: <1 >:: from_bytes_with_nul( b"\0 " ) . unwrap( ) == CString :: <3 >:: new( ) ) ;
452
+
453
+ // Single character
454
+ assert ! (
455
+ CString :: <2 >:: from_bytes_with_nul( b"a\0 " ) . unwrap( )
456
+ == CString :: <2 >:: from_bytes_with_nul( b"a\0 " ) . unwrap( )
457
+ ) ;
458
+ assert ! (
459
+ CString :: <2 >:: from_bytes_with_nul( b"a\0 " ) . unwrap( )
460
+ == CString :: <3 >:: from_bytes_with_nul( b"a\0 " ) . unwrap( )
461
+ ) ;
462
+ assert ! (
463
+ CString :: <2 >:: from_bytes_with_nul( b"a\0 " ) . unwrap( )
464
+ != CString :: <2 >:: from_bytes_with_nul( b"b\0 " ) . unwrap( )
465
+ ) ;
466
+
467
+ // Multiple characters
468
+ assert ! (
469
+ CString :: <4 >:: from_bytes_with_nul( b"abc\0 " ) . unwrap( )
470
+ == CString :: <4 >:: from_bytes_with_nul( b"abc\0 " ) . unwrap( )
471
+ ) ;
472
+ assert ! (
473
+ CString :: <3 >:: from_bytes_with_nul( b"ab\0 " ) . unwrap( )
474
+ != CString :: <4 >:: from_bytes_with_nul( b"abc\0 " ) . unwrap( )
475
+ ) ;
476
+ }
432
477
}
0 commit comments