@@ -578,7 +578,7 @@ mod tests {
578
578
579
579
macro_rules! format {
580
580
( $( $f: tt) * ) => ( {
581
- CString :: try_from_fmt( :: kernel:: fmt!( $( $f) * ) ) . unwrap ( ) . to_str( ) . unwrap ( )
581
+ CString :: try_from_fmt( :: kernel:: fmt!( $( $f) * ) ) ? . to_str( ) ?
582
582
} )
583
583
}
584
584
@@ -597,66 +597,72 @@ mod tests {
597
597
\\ xf0\\ xf1\\ xf2\\ xf3\\ xf4\\ xf5\\ xf6\\ xf7\\ xf8\\ xf9\\ xfa\\ xfb\\ xfc\\ xfd\\ xfe\\ xff";
598
598
599
599
#[ test]
600
- fn test_cstr_to_str ( ) {
600
+ fn test_cstr_to_str ( ) -> Result {
601
601
let good_bytes = b"\xf0 \x9f \xa6 \x80 \0 " ;
602
- let checked_cstr = CStr :: from_bytes_with_nul ( good_bytes) . unwrap ( ) ;
603
- let checked_str = checked_cstr. to_str ( ) . unwrap ( ) ;
602
+ let checked_cstr = CStr :: from_bytes_with_nul ( good_bytes) ? ;
603
+ let checked_str = checked_cstr. to_str ( ) ? ;
604
604
assert_eq ! ( checked_str, "🦀" ) ;
605
+ Ok ( ( ) )
605
606
}
606
607
607
608
#[ test]
608
- fn test_cstr_to_str_invalid_utf8 ( ) {
609
+ fn test_cstr_to_str_invalid_utf8 ( ) -> Result {
609
610
let bad_bytes = b"\xc3 \x28 \0 " ;
610
- let checked_cstr = CStr :: from_bytes_with_nul ( bad_bytes) . unwrap ( ) ;
611
+ let checked_cstr = CStr :: from_bytes_with_nul ( bad_bytes) ? ;
611
612
assert ! ( checked_cstr. to_str( ) . is_err( ) ) ;
613
+ Ok ( ( ) )
612
614
}
613
615
614
616
#[ test]
615
- fn test_cstr_as_str_unchecked ( ) {
617
+ fn test_cstr_as_str_unchecked ( ) -> Result {
616
618
let good_bytes = b"\xf0 \x9f \x90 \xA7 \0 " ;
617
- let checked_cstr = CStr :: from_bytes_with_nul ( good_bytes) . unwrap ( ) ;
619
+ let checked_cstr = CStr :: from_bytes_with_nul ( good_bytes) ? ;
618
620
// SAFETY: The contents come from a string literal which contains valid UTF-8.
619
621
let unchecked_str = unsafe { checked_cstr. as_str_unchecked ( ) } ;
620
622
assert_eq ! ( unchecked_str, "🐧" ) ;
623
+ Ok ( ( ) )
621
624
}
622
625
623
626
#[ test]
624
- fn test_cstr_display ( ) {
625
- let hello_world = CStr :: from_bytes_with_nul ( b"hello, world!\0 " ) . unwrap ( ) ;
627
+ fn test_cstr_display ( ) -> Result {
628
+ let hello_world = CStr :: from_bytes_with_nul ( b"hello, world!\0 " ) ? ;
626
629
assert_eq ! ( format!( "{hello_world}" ) , "hello, world!" ) ;
627
- let non_printables = CStr :: from_bytes_with_nul ( b"\x01 \x09 \x0a \0 " ) . unwrap ( ) ;
630
+ let non_printables = CStr :: from_bytes_with_nul ( b"\x01 \x09 \x0a \0 " ) ? ;
628
631
assert_eq ! ( format!( "{non_printables}" ) , "\\ x01\\ x09\\ x0a" ) ;
629
- let non_ascii = CStr :: from_bytes_with_nul ( b"d\xe9 j\xe0 vu\0 " ) . unwrap ( ) ;
632
+ let non_ascii = CStr :: from_bytes_with_nul ( b"d\xe9 j\xe0 vu\0 " ) ? ;
630
633
assert_eq ! ( format!( "{non_ascii}" ) , "d\\ xe9j\\ xe0 vu" ) ;
631
- let good_bytes = CStr :: from_bytes_with_nul ( b"\xf0 \x9f \xa6 \x80 \0 " ) . unwrap ( ) ;
634
+ let good_bytes = CStr :: from_bytes_with_nul ( b"\xf0 \x9f \xa6 \x80 \0 " ) ? ;
632
635
assert_eq ! ( format!( "{good_bytes}" ) , "\\ xf0\\ x9f\\ xa6\\ x80" ) ;
636
+ Ok ( ( ) )
633
637
}
634
638
635
639
#[ test]
636
- fn test_cstr_display_all_bytes ( ) {
640
+ fn test_cstr_display_all_bytes ( ) -> Result {
637
641
let mut bytes: [ u8 ; 256 ] = [ 0 ; 256 ] ;
638
642
// fill `bytes` with [1..=255] + [0]
639
643
for i in u8:: MIN ..=u8:: MAX {
640
644
bytes[ i as usize ] = i. wrapping_add ( 1 ) ;
641
645
}
642
- let cstr = CStr :: from_bytes_with_nul ( & bytes) . unwrap ( ) ;
646
+ let cstr = CStr :: from_bytes_with_nul ( & bytes) ? ;
643
647
assert_eq ! ( format!( "{cstr}" ) , ALL_ASCII_CHARS ) ;
648
+ Ok ( ( ) )
644
649
}
645
650
646
651
#[ test]
647
- fn test_cstr_debug ( ) {
648
- let hello_world = CStr :: from_bytes_with_nul ( b"hello, world!\0 " ) . unwrap ( ) ;
652
+ fn test_cstr_debug ( ) -> Result {
653
+ let hello_world = CStr :: from_bytes_with_nul ( b"hello, world!\0 " ) ? ;
649
654
assert_eq ! ( format!( "{hello_world:?}" ) , "\" hello, world!\" " ) ;
650
- let non_printables = CStr :: from_bytes_with_nul ( b"\x01 \x09 \x0a \0 " ) . unwrap ( ) ;
655
+ let non_printables = CStr :: from_bytes_with_nul ( b"\x01 \x09 \x0a \0 " ) ? ;
651
656
assert_eq ! ( format!( "{non_printables:?}" ) , "\" \\ x01\\ x09\\ x0a\" " ) ;
652
- let non_ascii = CStr :: from_bytes_with_nul ( b"d\xe9 j\xe0 vu\0 " ) . unwrap ( ) ;
657
+ let non_ascii = CStr :: from_bytes_with_nul ( b"d\xe9 j\xe0 vu\0 " ) ? ;
653
658
assert_eq ! ( format!( "{non_ascii:?}" ) , "\" d\\ xe9j\\ xe0 vu\" " ) ;
654
- let good_bytes = CStr :: from_bytes_with_nul ( b"\xf0 \x9f \xa6 \x80 \0 " ) . unwrap ( ) ;
659
+ let good_bytes = CStr :: from_bytes_with_nul ( b"\xf0 \x9f \xa6 \x80 \0 " ) ? ;
655
660
assert_eq ! ( format!( "{good_bytes:?}" ) , "\" \\ xf0\\ x9f\\ xa6\\ x80\" " ) ;
661
+ Ok ( ( ) )
656
662
}
657
663
658
664
#[ test]
659
- fn test_bstr_display ( ) {
665
+ fn test_bstr_display ( ) -> Result {
660
666
let hello_world = BStr :: from_bytes ( b"hello, world!" ) ;
661
667
assert_eq ! ( format!( "{hello_world}" ) , "hello, world!" ) ;
662
668
let escapes = BStr :: from_bytes ( b"_\t _\n _\r _\\ _\' _\" _" ) ;
@@ -667,10 +673,11 @@ mod tests {
667
673
assert_eq ! ( format!( "{non_ascii}" ) , "d\\ xe9j\\ xe0 vu" ) ;
668
674
let good_bytes = BStr :: from_bytes ( b"\xf0 \x9f \xa6 \x80 " ) ;
669
675
assert_eq ! ( format!( "{good_bytes}" ) , "\\ xf0\\ x9f\\ xa6\\ x80" ) ;
676
+ Ok ( ( ) )
670
677
}
671
678
672
679
#[ test]
673
- fn test_bstr_debug ( ) {
680
+ fn test_bstr_debug ( ) -> Result {
674
681
let hello_world = BStr :: from_bytes ( b"hello, world!" ) ;
675
682
assert_eq ! ( format!( "{hello_world:?}" ) , "\" hello, world!\" " ) ;
676
683
let escapes = BStr :: from_bytes ( b"_\t _\n _\r _\\ _\' _\" _" ) ;
@@ -681,6 +688,7 @@ mod tests {
681
688
assert_eq ! ( format!( "{non_ascii:?}" ) , "\" d\\ xe9j\\ xe0 vu\" " ) ;
682
689
let good_bytes = BStr :: from_bytes ( b"\xf0 \x9f \xa6 \x80 " ) ;
683
690
assert_eq ! ( format!( "{good_bytes:?}" ) , "\" \\ xf0\\ x9f\\ xa6\\ x80\" " ) ;
691
+ Ok ( ( ) )
684
692
}
685
693
}
686
694
0 commit comments