@@ -1155,20 +1155,42 @@ mod tests {
1155
1155
use super :: { AsAsciiStr , AsAsciiStrError , AsMutAsciiStr , AsciiStr } ;
1156
1156
use AsciiChar ;
1157
1157
1158
+ /// Ensures that common types, `str`, `[u8]`, `AsciiStr` and their
1159
+ /// references, shared and mutable implement `AsAsciiStr`.
1158
1160
#[ test]
1159
1161
fn generic_as_ascii_str ( ) {
1162
+ // Generic function to ensure `C` implements `AsAsciiStr`
1160
1163
fn generic < C : AsAsciiStr + ?Sized > ( c : & C ) -> Result < & AsciiStr , AsAsciiStrError > {
1161
1164
c. as_ascii_str ( )
1162
1165
}
1166
+
1163
1167
let arr = [ AsciiChar :: A ] ;
1164
- let ascii_str: & AsciiStr = arr. as_ref ( ) . into ( ) ;
1165
- assert_eq ! ( generic( "A" ) , Ok ( ascii_str) ) ;
1166
- assert_eq ! ( generic( & b"A" [ ..] ) , Ok ( ascii_str) ) ;
1167
- assert_eq ! ( generic( ascii_str) , Ok ( ascii_str) ) ;
1168
- assert_eq ! ( generic( & "A" ) , Ok ( ascii_str) ) ;
1169
- assert_eq ! ( generic( & ascii_str) , Ok ( ascii_str) ) ;
1170
- // Note: Not sure what this is supposed to be testing, as `generic` can only accept shared references
1171
- //assert_eq!(generic(&mut "A"), Ok(ascii_str));
1168
+ let ascii_str = arr. as_ref ( ) . into ( ) ;
1169
+ let mut mut_arr = arr; // Note: We need a second copy to prevent overlapping mutable borrows.
1170
+ let mut_ascii_str = mut_arr. as_mut ( ) . into ( ) ;
1171
+ let mut_arr_mut_ref: & mut [ AsciiChar ] = & mut [ AsciiChar :: A ] ;
1172
+ let mut string = "A" . to_string ( ) ;
1173
+ let mut string2 = "A" . to_string ( ) ;
1174
+ let string_mut = string. as_mut ( ) ;
1175
+ let string_mut_bytes = unsafe { string2. as_bytes_mut ( ) } ; // SAFETY: We don't modify it
1176
+
1177
+ // Note: This is a trick because `rustfmt` doesn't support
1178
+ // attributes on blocks yet.
1179
+ #[ rustfmt:: skip]
1180
+ let _ = [
1181
+ assert_eq ! ( generic:: <str >( "A" ) , Ok ( ascii_str) ) ,
1182
+ assert_eq ! ( generic:: <[ u8 ] >( & b"A" [ ..] ) , Ok ( ascii_str) ) ,
1183
+ assert_eq ! ( generic:: <AsciiStr >( ascii_str ) , Ok ( ascii_str) ) ,
1184
+ assert_eq ! ( generic:: <[ AsciiChar ] >( & arr ) , Ok ( ascii_str) ) ,
1185
+ assert_eq ! ( generic:: <& str >( & "A" ) , Ok ( ascii_str) ) ,
1186
+ assert_eq ! ( generic:: <& [ u8 ] >( &&b"A" [ ..] ) , Ok ( ascii_str) ) ,
1187
+ assert_eq ! ( generic:: <& AsciiStr >( & ascii_str ) , Ok ( ascii_str) ) ,
1188
+ assert_eq ! ( generic:: <& [ AsciiChar ] >( &&arr[ ..] ) , Ok ( ascii_str) ) ,
1189
+ assert_eq ! ( generic:: <& mut str >( & string_mut ) , Ok ( ascii_str) ) ,
1190
+ assert_eq ! ( generic:: <& mut [ u8 ] >( & string_mut_bytes) , Ok ( ascii_str) ) ,
1191
+ assert_eq ! ( generic:: <& mut AsciiStr >( & mut_ascii_str ) , Ok ( ascii_str) ) ,
1192
+ assert_eq ! ( generic:: <& mut [ AsciiChar ] >( & mut_arr_mut_ref ) , Ok ( ascii_str) ) ,
1193
+ ] ;
1172
1194
}
1173
1195
1174
1196
#[ cfg( feature = "std" ) ]
0 commit comments