Skip to content

Commit 704f56f

Browse files
committed
impl Send + Sync and override count for the CStr::bytes iterator
1 parent 6c38c60 commit 704f56f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

core/src/ffi/c_str.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,15 @@ const unsafe fn const_strlen(ptr: *const c_char) -> usize {
780780
pub struct Bytes<'a> {
781781
// since we know the string is nul-terminated, we only need one pointer
782782
ptr: NonNull<u8>,
783-
phantom: PhantomData<&'a u8>,
783+
phantom: PhantomData<&'a [c_char]>,
784784
}
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+
785792
impl<'a> Bytes<'a> {
786793
#[inline]
787794
fn new(s: &'a CStr) -> Self {
@@ -814,7 +821,7 @@ impl Iterator for Bytes<'_> {
814821
if ret == 0 {
815822
None
816823
} else {
817-
self.ptr = self.ptr.offset(1);
824+
self.ptr = self.ptr.add(1);
818825
Some(ret)
819826
}
820827
}
@@ -824,6 +831,12 @@ impl Iterator for Bytes<'_> {
824831
fn size_hint(&self) -> (usize, Option<usize>) {
825832
if self.is_empty() { (0, Some(0)) } else { (1, None) }
826833
}
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+
}
827840
}
828841

829842
#[unstable(feature = "cstr_bytes", issue = "112115")]

0 commit comments

Comments
 (0)