Skip to content

Commit d3cf2e1

Browse files
authored
Rollup merge of rust-lang#127444 - Sky9x:cstr-bytes-iter, r=dtolnay
`impl Send + Sync` and override `count` for the `CStr::bytes` iterator cc tracking issue rust-lang#112115
2 parents 12075d1 + 704f56f commit d3cf2e1

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
@@ -781,8 +781,15 @@ const unsafe fn strlen(ptr: *const c_char) -> usize {
781781
pub struct Bytes<'a> {
782782
// since we know the string is nul-terminated, we only need one pointer
783783
ptr: NonNull<u8>,
784-
phantom: PhantomData<&'a u8>,
784+
phantom: PhantomData<&'a [c_char]>,
785785
}
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+
786793
impl<'a> Bytes<'a> {
787794
#[inline]
788795
fn new(s: &'a CStr) -> Self {
@@ -815,7 +822,7 @@ impl Iterator for Bytes<'_> {
815822
if ret == 0 {
816823
None
817824
} else {
818-
self.ptr = self.ptr.offset(1);
825+
self.ptr = self.ptr.add(1);
819826
Some(ret)
820827
}
821828
}
@@ -825,6 +832,12 @@ impl Iterator for Bytes<'_> {
825832
fn size_hint(&self) -> (usize, Option<usize>) {
826833
if self.is_empty() { (0, Some(0)) } else { (1, None) }
827834
}
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+
}
828841
}
829842

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

0 commit comments

Comments
 (0)