@@ -11,7 +11,7 @@ use alloc::{
1111 string:: { String , ToString } ,
1212 vec:: Vec ,
1313} ;
14- use core:: { borrow, convert:: TryFrom , fmt, hash, ops, str} ;
14+ use core:: { borrow:: Borrow , convert:: TryFrom , fmt, hash, ops, str} ;
1515
1616use bytes:: Bytes ;
1717
@@ -132,13 +132,12 @@ impl ops::Deref for ByteString {
132132 #[ inline]
133133 fn deref ( & self ) -> & str {
134134 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.
137136 unsafe { str:: from_utf8_unchecked ( bytes) }
138137 }
139138}
140139
141- impl borrow :: Borrow < str > for ByteString {
140+ impl Borrow < str > for ByteString {
142141 fn borrow ( & self ) -> & str {
143142 self
144143 }
@@ -292,7 +291,7 @@ mod serde {
292291
293292#[ cfg( test) ]
294293mod test {
295- use alloc:: borrow:: ToOwned ;
294+ use alloc:: { borrow:: ToOwned , format , vec } ;
296295 use core:: {
297296 hash:: { Hash , Hasher } ,
298297 panic:: { RefUnwindSafe , UnwindSafe } ,
@@ -309,20 +308,53 @@ mod test {
309308 assert_impl_all ! ( ByteString : UnwindSafe , RefUnwindSafe ) ;
310309
311310 #[ test]
312- fn test_partial_eq ( ) {
311+ fn eq ( ) {
313312 let s: ByteString = ByteString :: from_static ( "test" ) ;
314313 assert_eq ! ( s, "test" ) ;
315314 assert_eq ! ( s, * "test" ) ;
316315 assert_eq ! ( s, "test" . to_owned( ) ) ;
317316 }
318317
319318 #[ test]
320- fn test_new ( ) {
319+ fn new ( ) {
321320 let _: ByteString = ByteString :: new ( ) ;
322321 }
323322
324323 #[ 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 ( ) {
326358 let mut hasher1 = AHasher :: default ( ) ;
327359 "str" . hash ( & mut hasher1) ;
328360
@@ -333,57 +365,82 @@ mod test {
333365 }
334366
335367 #[ test]
336- fn test_from_string ( ) {
368+ fn from_string ( ) {
337369 let s: ByteString = "hello" . to_owned ( ) . into ( ) ;
338370 assert_eq ! ( & s, "hello" ) ;
339371 let t: & str = s. as_ref ( ) ;
340372 assert_eq ! ( t, "hello" ) ;
341373 }
342374
343375 #[ test]
344- fn test_from_str ( ) {
376+ fn from_str ( ) {
345377 let _: ByteString = "str" . into ( ) ;
378+ let _: ByteString = "str" . to_owned ( ) . into_boxed_str ( ) . into ( ) ;
346379 }
347380
348381 #[ 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 ( ) {
350389 static _S: ByteString = ByteString :: from_static ( "hello" ) ;
351390 let _ = ByteString :: from_static ( "str" ) ;
352391 }
353392
354393 #[ test]
355- fn test_try_from_slice ( ) {
394+ fn try_from_slice ( ) {
356395 let _ = ByteString :: try_from ( b"nice bytes" ) . unwrap ( ) ;
357396 }
358397
359398 #[ test]
360- fn test_try_from_array ( ) {
399+ fn try_from_array ( ) {
361400 assert_eq ! (
362401 ByteString :: try_from( [ b'h' , b'i' ] ) . unwrap( ) ,
363402 ByteString :: from_static( "hi" )
364403 ) ;
365404 }
366405
367406 #[ 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 ( ) {
369414 let _ = ByteString :: try_from ( Bytes :: from_static ( b"nice bytes" ) ) . unwrap ( ) ;
370415 }
371416
372417 #[ test]
373- fn test_try_from_bytes_mut ( ) {
418+ fn try_from_bytes_mut ( ) {
374419 let _ = ByteString :: try_from ( bytes:: BytesMut :: from ( & b"nice bytes" [ ..] ) ) . unwrap ( ) ;
375420 }
376421
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+
377434 #[ cfg( feature = "serde" ) ]
378435 #[ test]
379- fn test_serialize ( ) {
436+ fn serialize ( ) {
380437 let s: ByteString = serde_json:: from_str ( r#""nice bytes""# ) . unwrap ( ) ;
381438 assert_eq ! ( s, "nice bytes" ) ;
382439 }
383440
384441 #[ cfg( feature = "serde" ) ]
385442 #[ test]
386- fn test_deserialize ( ) {
443+ fn deserialize ( ) {
387444 let s = serde_json:: to_string ( & ByteString :: from_static ( "nice bytes" ) ) . unwrap ( ) ;
388445 assert_eq ! ( s, r#""nice bytes""# ) ;
389446 }
@@ -399,7 +456,7 @@ mod test {
399456
400457 #[ test]
401458 #[ should_panic]
402- fn test_slice_ref_catches_not_a_subset ( ) {
459+ fn slice_ref_catches_not_a_subset ( ) {
403460 // panics because the given slice is not derived from the original byte string, despite
404461 // being a logical subset of the string
405462 ByteString :: from_static ( "foo bar" ) . slice_ref ( "foo" ) ;
0 commit comments