11//! [`CStr`] and its related types.
22
3+ use safety:: { ensures, requires} ;
4+
35use crate :: cmp:: Ordering ;
46use crate :: error:: Error ;
57use crate :: ffi:: c_char;
68use crate :: intrinsics:: const_eval_select;
79use crate :: iter:: FusedIterator ;
10+ #[ cfg( kani) ]
11+ use crate :: kani;
812use crate :: marker:: PhantomData ;
913use crate :: ptr:: NonNull ;
1014use crate :: slice:: memchr;
11- use crate :: { fmt, ops, slice, str} ;
12- use safety:: { requires, ensures} ;
13-
1415use crate :: ub_checks:: Invariant ;
1516#[ allow( unused_imports) ]
1617use crate :: ub_checks:: can_dereference;
17-
18- #[ cfg( kani) ]
19- use crate :: kani;
18+ use crate :: { fmt, ops, slice, str} ;
2019
2120// FIXME: because this is doc(inline)d, we *have* to use intra-doc links because the actual link
2221// depends on where the item is being documented. however, since this is libcore, we can't
@@ -228,7 +227,7 @@ impl Invariant for &CStr {
228227 let bytes: & [ c_char ] = & self . inner ;
229228 let len = bytes. len ( ) ;
230229
231- !bytes. is_empty ( ) && bytes[ len - 1 ] == 0 && !bytes[ ..len- 1 ] . contains ( & 0 )
230+ !bytes. is_empty ( ) && bytes[ len - 1 ] == 0 && !bytes[ ..len - 1 ] . contains ( & 0 )
232231 }
233232}
234233
@@ -887,7 +886,7 @@ impl FusedIterator for Bytes<'_> {}
887886#[ unstable( feature = "kani" , issue = "none" ) ]
888887mod verify {
889888 use super :: * ;
890-
889+
891890 // Helper function
892891 fn arbitrary_cstr ( slice : & [ u8 ] ) -> & CStr {
893892 // At a minimum, the slice has a null terminator to form a valid CStr.
@@ -934,7 +933,7 @@ mod verify {
934933 let len = bytes. len ( ) ;
935934 assert_eq ! ( bytes, & slice[ ..len] ) ;
936935 }
937-
936+
938937 // pub fn bytes(&self) -> Bytes<'_>
939938 #[ kani:: proof]
940939 #[ kani:: unwind( 32 ) ]
@@ -972,7 +971,7 @@ mod verify {
972971
973972 // pub const fn as_ptr(&self) -> *const c_char
974973 #[ kani:: proof]
975- #[ kani:: unwind( 33 ) ]
974+ #[ kani:: unwind( 33 ) ]
976975 fn check_as_ptr ( ) {
977976 const MAX_SIZE : usize = 32 ;
978977 let string: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
@@ -996,10 +995,10 @@ mod verify {
996995 }
997996 assert ! ( c_str. is_safe( ) ) ;
998997 }
999-
998+
1000999 // pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError>
10011000 #[ kani:: proof]
1002- #[ kani:: unwind( 17 ) ]
1001+ #[ kani:: unwind( 17 ) ]
10031002 fn check_from_bytes_with_nul ( ) {
10041003 const MAX_SIZE : usize = 16 ;
10051004 let string: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
@@ -1017,10 +1016,10 @@ mod verify {
10171016 fn check_count_bytes ( ) {
10181017 const MAX_SIZE : usize = 32 ;
10191018 let mut bytes: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
1020-
1019+
10211020 // Non-deterministically generate a length within the valid range [0, MAX_SIZE]
10221021 let mut len: usize = kani:: any_where ( |& x| x < MAX_SIZE ) ;
1023-
1022+
10241023 // If a null byte exists before the generated length
10251024 // adjust len to its position
10261025 if let Some ( pos) = bytes[ ..len] . iter ( ) . position ( |& x| x == 0 ) {
@@ -1029,7 +1028,7 @@ mod verify {
10291028 // If no null byte, insert one at the chosen length
10301029 bytes[ len] = 0 ;
10311030 }
1032-
1031+
10331032 let c_str = CStr :: from_bytes_until_nul ( & bytes) . unwrap ( ) ;
10341033 // Verify that count_bytes matches the adjusted length
10351034 assert_eq ! ( c_str. count_bytes( ) , len) ;
@@ -1076,7 +1075,9 @@ mod verify {
10761075 let mut string: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
10771076 let ptr = string. as_ptr ( ) as * const c_char ;
10781077
1079- unsafe { super :: strlen ( ptr) ; }
1078+ unsafe {
1079+ super :: strlen ( ptr) ;
1080+ }
10801081 }
10811082
10821083 // pub const unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr
@@ -1087,9 +1088,11 @@ mod verify {
10871088 let string: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
10881089 let ptr = string. as_ptr ( ) as * const c_char ;
10891090
1090- unsafe { CStr :: from_ptr ( ptr) ; }
1091+ unsafe {
1092+ CStr :: from_ptr ( ptr) ;
1093+ }
10911094 }
1092-
1095+
10931096 // pub const fn is_empty(&self) -> bool
10941097 #[ kani:: proof]
10951098 #[ kani:: unwind( 33 ) ]
0 commit comments