@@ -11,7 +11,7 @@ use alloc::{
11
11
string:: { String , ToString } ,
12
12
vec:: Vec ,
13
13
} ;
14
- use core:: { borrow, convert:: TryFrom , fmt, hash, ops, str} ;
14
+ use core:: { borrow:: Borrow , convert:: TryFrom , fmt, hash, ops, str} ;
15
15
16
16
use bytes:: Bytes ;
17
17
@@ -132,13 +132,12 @@ impl ops::Deref for ByteString {
132
132
#[ inline]
133
133
fn deref ( & self ) -> & str {
134
134
let bytes = self . 0 . as_ref ( ) ;
135
- // SAFETY:
136
- // UTF-8 validity is guaranteed at during construction.
135
+ // SAFETY: UTF-8 validity is guaranteed during construction.
137
136
unsafe { str:: from_utf8_unchecked ( bytes) }
138
137
}
139
138
}
140
139
141
- impl borrow :: Borrow < str > for ByteString {
140
+ impl Borrow < str > for ByteString {
142
141
fn borrow ( & self ) -> & str {
143
142
self
144
143
}
@@ -292,7 +291,7 @@ mod serde {
292
291
293
292
#[ cfg( test) ]
294
293
mod test {
295
- use alloc:: borrow:: ToOwned ;
294
+ use alloc:: { borrow:: ToOwned , format , vec } ;
296
295
use core:: {
297
296
hash:: { Hash , Hasher } ,
298
297
panic:: { RefUnwindSafe , UnwindSafe } ,
@@ -309,20 +308,53 @@ mod test {
309
308
assert_impl_all ! ( ByteString : UnwindSafe , RefUnwindSafe ) ;
310
309
311
310
#[ test]
312
- fn test_partial_eq ( ) {
311
+ fn eq ( ) {
313
312
let s: ByteString = ByteString :: from_static ( "test" ) ;
314
313
assert_eq ! ( s, "test" ) ;
315
314
assert_eq ! ( s, * "test" ) ;
316
315
assert_eq ! ( s, "test" . to_owned( ) ) ;
317
316
}
318
317
319
318
#[ test]
320
- fn test_new ( ) {
319
+ fn new ( ) {
321
320
let _: ByteString = ByteString :: new ( ) ;
322
321
}
323
322
324
323
#[ test]
325
- fn test_hash ( ) {
324
+ fn as_bytes ( ) {
325
+ let buf = ByteString :: new ( ) ;
326
+ assert ! ( buf. as_bytes( ) . is_empty( ) ) ;
327
+
328
+ let buf = ByteString :: from ( "hello" ) ;
329
+ assert_eq ! ( buf. as_bytes( ) , "hello" ) ;
330
+ }
331
+
332
+ #[ test]
333
+ fn from_bytes_unchecked ( ) {
334
+ let buf = unsafe { ByteString :: from_bytes_unchecked ( Bytes :: new ( ) ) } ;
335
+ assert ! ( buf. is_empty( ) ) ;
336
+
337
+ let buf = unsafe { ByteString :: from_bytes_unchecked ( Bytes :: from ( "hello" ) ) } ;
338
+ assert_eq ! ( buf, "hello" ) ;
339
+ }
340
+
341
+ #[ test]
342
+ fn as_ref ( ) {
343
+ let buf = ByteString :: new ( ) ;
344
+
345
+ let _: & ByteString = buf. as_ref ( ) ;
346
+ let _: & [ u8 ] = buf. as_ref ( ) ;
347
+ }
348
+
349
+ #[ test]
350
+ fn borrow ( ) {
351
+ let buf = ByteString :: new ( ) ;
352
+
353
+ let _: & str = buf. borrow ( ) ;
354
+ }
355
+
356
+ #[ test]
357
+ fn hash ( ) {
326
358
let mut hasher1 = AHasher :: default ( ) ;
327
359
"str" . hash ( & mut hasher1) ;
328
360
@@ -333,57 +365,82 @@ mod test {
333
365
}
334
366
335
367
#[ test]
336
- fn test_from_string ( ) {
368
+ fn from_string ( ) {
337
369
let s: ByteString = "hello" . to_owned ( ) . into ( ) ;
338
370
assert_eq ! ( & s, "hello" ) ;
339
371
let t: & str = s. as_ref ( ) ;
340
372
assert_eq ! ( t, "hello" ) ;
341
373
}
342
374
343
375
#[ test]
344
- fn test_from_str ( ) {
376
+ fn from_str ( ) {
345
377
let _: ByteString = "str" . into ( ) ;
378
+ let _: ByteString = "str" . to_owned ( ) . into_boxed_str ( ) . into ( ) ;
346
379
}
347
380
348
381
#[ test]
349
- fn test_from_static_str ( ) {
382
+ fn to_string ( ) {
383
+ let buf = ByteString :: from ( "foo" ) ;
384
+ assert_eq ! ( String :: from( buf) , "foo" ) ;
385
+ }
386
+
387
+ #[ test]
388
+ fn from_static_str ( ) {
350
389
static _S: ByteString = ByteString :: from_static ( "hello" ) ;
351
390
let _ = ByteString :: from_static ( "str" ) ;
352
391
}
353
392
354
393
#[ test]
355
- fn test_try_from_slice ( ) {
394
+ fn try_from_slice ( ) {
356
395
let _ = ByteString :: try_from ( b"nice bytes" ) . unwrap ( ) ;
357
396
}
358
397
359
398
#[ test]
360
- fn test_try_from_array ( ) {
399
+ fn try_from_array ( ) {
361
400
assert_eq ! (
362
401
ByteString :: try_from( [ b'h' , b'i' ] ) . unwrap( ) ,
363
402
ByteString :: from_static( "hi" )
364
403
) ;
365
404
}
366
405
367
406
#[ test]
368
- fn test_try_from_bytes ( ) {
407
+ fn try_from_vec ( ) {
408
+ let _ = ByteString :: try_from ( vec ! [ b'f' , b'o' , b'o' ] ) . unwrap ( ) ;
409
+ ByteString :: try_from ( vec ! [ 0 , 159 , 146 , 150 ] ) . unwrap_err ( ) ;
410
+ }
411
+
412
+ #[ test]
413
+ fn try_from_bytes ( ) {
369
414
let _ = ByteString :: try_from ( Bytes :: from_static ( b"nice bytes" ) ) . unwrap ( ) ;
370
415
}
371
416
372
417
#[ test]
373
- fn test_try_from_bytes_mut ( ) {
418
+ fn try_from_bytes_mut ( ) {
374
419
let _ = ByteString :: try_from ( bytes:: BytesMut :: from ( & b"nice bytes" [ ..] ) ) . unwrap ( ) ;
375
420
}
376
421
422
+ #[ test]
423
+ fn display ( ) {
424
+ let buf = ByteString :: from ( "bar" ) ;
425
+ assert_eq ! ( format!( "{buf}" ) , "bar" ) ;
426
+ }
427
+
428
+ #[ test]
429
+ fn debug ( ) {
430
+ let buf = ByteString :: from ( "baz" ) ;
431
+ assert_eq ! ( format!( "{buf:?}" ) , r#""baz""# ) ;
432
+ }
433
+
377
434
#[ cfg( feature = "serde" ) ]
378
435
#[ test]
379
- fn test_serialize ( ) {
436
+ fn serialize ( ) {
380
437
let s: ByteString = serde_json:: from_str ( r#""nice bytes""# ) . unwrap ( ) ;
381
438
assert_eq ! ( s, "nice bytes" ) ;
382
439
}
383
440
384
441
#[ cfg( feature = "serde" ) ]
385
442
#[ test]
386
- fn test_deserialize ( ) {
443
+ fn deserialize ( ) {
387
444
let s = serde_json:: to_string ( & ByteString :: from_static ( "nice bytes" ) ) . unwrap ( ) ;
388
445
assert_eq ! ( s, r#""nice bytes""# ) ;
389
446
}
@@ -399,7 +456,7 @@ mod test {
399
456
400
457
#[ test]
401
458
#[ should_panic]
402
- fn test_slice_ref_catches_not_a_subset ( ) {
459
+ fn slice_ref_catches_not_a_subset ( ) {
403
460
// panics because the given slice is not derived from the original byte string, despite
404
461
// being a logical subset of the string
405
462
ByteString :: from_static ( "foo bar" ) . slice_ref ( "foo" ) ;
0 commit comments