@@ -136,7 +136,7 @@ impl fmt::Display for CStr8 {
136
136
}
137
137
}
138
138
139
- impl < StrType : AsRef < str > > EqStrUntilNul < StrType > for CStr8 {
139
+ impl < StrType : AsRef < str > + ? Sized > EqStrUntilNul < StrType > for CStr8 {
140
140
fn eq_str_until_nul ( & self , other : & StrType ) -> bool {
141
141
let other = other. as_ref ( ) ;
142
142
@@ -147,7 +147,8 @@ impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CStr8 {
147
147
. copied ( )
148
148
. map ( char:: from)
149
149
. zip ( other. chars ( ) )
150
- // this only works as CStr8 is guaranteed to have a fixed character length
150
+ // This only works as CStr8 is guaranteed to have a fixed character length
151
+ // (unlike UTF-8).
151
152
. take_while ( |( l, r) | * l != '\0' && * r != '\0' )
152
153
. any ( |( l, r) | l != r) ;
153
154
@@ -345,7 +346,7 @@ impl CStr16 {
345
346
}
346
347
}
347
348
348
- impl < StrType : AsRef < str > > EqStrUntilNul < StrType > for CStr16 {
349
+ impl < StrType : AsRef < str > + ? Sized > EqStrUntilNul < StrType > for CStr16 {
349
350
fn eq_str_until_nul ( & self , other : & StrType ) -> bool {
350
351
let other = other. as_ref ( ) ;
351
352
@@ -354,7 +355,8 @@ impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CStr16 {
354
355
. copied ( )
355
356
. map ( char:: from)
356
357
. zip ( other. chars ( ) )
357
- // this only works as CStr16 is guaranteed to have a fixed character length
358
+ // This only works as CStr16 is guaranteed to have a fixed character length
359
+ // (unlike UTF-8 or UTF-16).
358
360
. take_while ( |( l, r) | * l != '\0' && * r != '\0' )
359
361
. any ( |( l, r) | l != r) ;
360
362
@@ -416,10 +418,11 @@ impl<'a> UnalignedSlice<'a, u16> {
416
418
}
417
419
}
418
420
419
- /// Trait that helps to compare Rust strings against CStr16 types.
420
- /// A generic implementation of this trait enables us that we only have to
421
- /// implement one direction (`left.eq_str_until_nul(&right)`) and we get
422
- /// the other direction (`right.eq_str_until_nul(&left)`) for free.
421
+ /// The EqStrUntilNul trait helps to compare Rust strings against UEFI string types (UCS-2 strings).
422
+ /// The given generic implementation of this trait enables us that we only have to
423
+ /// implement one direction (`left.eq_str_until_nul(&right)`) for each UEFI string type and we
424
+ /// get the other direction (`right.eq_str_until_nul(&left)`) for free. Hence, the relation is
425
+ /// reflexive.
423
426
pub trait EqStrUntilNul < StrType : ?Sized > {
424
427
/// Checks if the provided Rust string `StrType` is equal to [Self] until the first null-byte
425
428
/// is found. An exception is the terminating null-byte of [Self] which is ignored.
@@ -575,4 +578,41 @@ mod tests {
575
578
const S : & CStr16 = cstr16 ! ( "ABC" ) ;
576
579
assert_eq ! ( S . to_u16_slice_with_nul( ) , [ 65 , 66 , 67 , 0 ] ) ;
577
580
}
581
+
582
+ /// Tests the trait implementation of trait [`EqStrUntilNul]` for [`CStr8`].
583
+ ///
584
+ /// This tests that `String` and `str` from the standard library can be
585
+ /// checked for equality against a [`CStr8`]. It checks both directions,
586
+ /// i.e., the equality is reflexive.
587
+ #[ test]
588
+ fn test_cstr8_eq_std_str ( ) {
589
+ let input: & CStr8 = cstr8 ! ( "test" ) ;
590
+
591
+ // test various comparisons with different order (left, right)
592
+ assert ! ( input. eq_str_until_nul( "test" ) ) ; // requires ?Sized constraint
593
+ assert ! ( input. eq_str_until_nul( & "test" ) ) ;
594
+ assert ! ( input. eq_str_until_nul( & String :: from( "test" ) ) ) ;
595
+
596
+ // now other direction
597
+ assert ! ( String :: from( "test" ) . eq_str_until_nul( input) ) ;
598
+ assert ! ( "test" . eq_str_until_nul( input) ) ;
599
+ }
600
+
601
+ /// Tests the trait implementation of trait [`EqStrUntilNul]` for [`CStr16`].
602
+ ///
603
+ /// This tests that `String` and `str` from the standard library can be
604
+ /// checked for equality against a [`CStr16`]. It checks both directions,
605
+ /// i.e., the equality is reflexive.
606
+ #[ test]
607
+ fn test_cstr16_eq_std_str ( ) {
608
+ let input: & CStr16 = cstr16 ! ( "test" ) ;
609
+
610
+ assert ! ( input. eq_str_until_nul( "test" ) ) ; // requires ?Sized constraint
611
+ assert ! ( input. eq_str_until_nul( & "test" ) ) ;
612
+ assert ! ( input. eq_str_until_nul( & String :: from( "test" ) ) ) ;
613
+
614
+ // now other direction
615
+ assert ! ( String :: from( "test" ) . eq_str_until_nul( input) ) ;
616
+ assert ! ( "test" . eq_str_until_nul( input) ) ;
617
+ }
578
618
}
0 commit comments