Rollup merge of #127444 - Sky9x:cstr-bytes-iter, r=dtolnay · model-checking/verify-rust-std@d3cf2e1 (original) (raw)

Original file line number Diff line number Diff line change
@@ -781,8 +781,15 @@ const unsafe fn strlen(ptr: *const c_char) -> usize {
781 781 pub struct Bytes<'a> {
782 782 // since we know the string is nul-terminated, we only need one pointer
783 783 ptr: NonNull<u8>,
784 -phantom: PhantomData<&'a u8>,
784 +phantom: PhantomData<&'a [c_char]>,
785 785 }
786 +
787 +#[unstable(feature = "cstr_bytes", issue = "112115")]
788 +unsafe impl Send for Bytes<'_> {}
789 +
790 +#[unstable(feature = "cstr_bytes", issue = "112115")]
791 +unsafe impl Sync for Bytes<'_> {}
792 +
786 793 impl<'a> Bytes<'a> {
787 794 #[inline]
788 795 fn new(s: &'a CStr) -> Self {
@@ -815,7 +822,7 @@ impl Iterator for Bytes<'_> {
815 822 if ret == 0 {
816 823 None
817 824 } else {
818 -self.ptr = self.ptr.offset(1);
825 +self.ptr = self.ptr.add(1);
819 826 Some(ret)
820 827 }
821 828 }
@@ -825,6 +832,12 @@ impl Iterator for Bytes<'_> {
825 832 fn size_hint(&self) -> (usize, Option<usize>) {
826 833 if self.is_empty() { (0, Some(0)) } else { (1, None) }
827 834 }
835 +
836 +#[inline]
837 +fn count(self) -> usize {
838 +// SAFETY: We always hold a valid pointer to a C string
839 +unsafe { strlen(self.ptr.as_ptr().cast()) }
840 +}
828 841 }
829 842
830 843 #[unstable(feature = "cstr_bytes", issue = "112115")]