impl Send + Sync and override count for the CStr::bytes iterator · model-checking/verify-rust-std@704f56f (original) (raw)

Original file line number Diff line number Diff line change
@@ -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,6 +831,12 @@ 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")]