@@ -735,6 +735,11 @@ impl AsRef<CStr> for CStr {
735
735
}
736
736
}
737
737
738
+ extern "C" {
739
+ /// Provided by libc or compiler_builtins.
740
+ fn strlen ( s : * const c_char ) -> usize ;
741
+ }
742
+
738
743
/// Calculate the length of a nul-terminated string. Defers to C's `strlen` when possible.
739
744
///
740
745
/// # Safety
@@ -756,11 +761,6 @@ const unsafe fn const_strlen(ptr: *const c_char) -> usize {
756
761
757
762
#[ inline]
758
763
fn strlen_rt ( s : * const c_char ) -> usize {
759
- extern "C" {
760
- /// Provided by libc or compiler_builtins.
761
- fn strlen ( s : * const c_char ) -> usize ;
762
- }
763
-
764
764
// SAFETY: Outer caller has provided a pointer to a valid C string.
765
765
unsafe { strlen ( s) }
766
766
}
@@ -780,8 +780,15 @@ const unsafe fn const_strlen(ptr: *const c_char) -> usize {
780
780
pub struct Bytes < ' a > {
781
781
// since we know the string is nul-terminated, we only need one pointer
782
782
ptr : NonNull < u8 > ,
783
- phantom : PhantomData < & ' a u8 > ,
783
+ phantom : PhantomData < & ' a [ c_char ] > ,
784
784
}
785
+
786
+ #[ unstable( feature = "cstr_bytes" , issue = "112115" ) ]
787
+ unsafe impl Send for Bytes < ' _ > { }
788
+
789
+ #[ unstable( feature = "cstr_bytes" , issue = "112115" ) ]
790
+ unsafe impl Sync for Bytes < ' _ > { }
791
+
785
792
impl < ' a > Bytes < ' a > {
786
793
#[ inline]
787
794
fn new ( s : & ' a CStr ) -> Self {
@@ -814,7 +821,7 @@ impl Iterator for Bytes<'_> {
814
821
if ret == 0 {
815
822
None
816
823
} else {
817
- self . ptr = self . ptr . offset ( 1 ) ;
824
+ self . ptr = self . ptr . add ( 1 ) ;
818
825
Some ( ret)
819
826
}
820
827
}
@@ -824,7 +831,14 @@ impl Iterator for Bytes<'_> {
824
831
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
825
832
if self . is_empty ( ) { ( 0 , Some ( 0 ) ) } else { ( 1 , None ) }
826
833
}
834
+
835
+ #[ inline]
836
+ fn count ( self ) -> usize {
837
+ // SAFETY: We always hold a valid pointer to a C string
838
+ unsafe { strlen ( self . ptr . as_ptr ( ) . cast ( ) ) }
839
+ }
827
840
}
828
841
829
842
#[ unstable( feature = "cstr_bytes" , issue = "112115" ) ]
830
843
impl FusedIterator for Bytes < ' _ > { }
844
+
0 commit comments