diff options
| author | Trevor Gross <t.gross35@gmail.com> | 2024-07-16 20:10:11 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 20:10:11 -0500 |
| commit | 4013acdda2167176f6dba4c2aac22ca8fb82bdc3 (patch) | |
| tree | e88b680cc7fe4889c2d570e30042f1b85aa692ee | |
| parent | 606d8cf9e81702fc50aff3baa4154f90df68b261 (diff) | |
| parent | 7f8f1780d47efa836a636de62a2b27cba20f2644 (diff) | |
| download | rust-4013acdda2167176f6dba4c2aac22ca8fb82bdc3.tar.gz rust-4013acdda2167176f6dba4c2aac22ca8fb82bdc3.zip | |
Rollup merge of #127444 - Sky9x:cstr-bytes-iter, r=dtolnay
`impl Send + Sync` and override `count` for the `CStr::bytes` iterator cc tracking issue #112115
| -rw-r--r-- | library/core/src/ffi/c_str.rs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index 76752f22ed8..563f0a324e3 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -781,8 +781,15 @@ const unsafe fn strlen(ptr: *const c_char) -> usize { pub struct Bytes<'a> { // since we know the string is nul-terminated, we only need one pointer ptr: NonNull<u8>, - phantom: PhantomData<&'a u8>, + phantom: PhantomData<&'a [c_char]>, } + +#[unstable(feature = "cstr_bytes", issue = "112115")] +unsafe impl Send for Bytes<'_> {} + +#[unstable(feature = "cstr_bytes", issue = "112115")] +unsafe impl Sync for Bytes<'_> {} + impl<'a> Bytes<'a> { #[inline] fn new(s: &'a CStr) -> Self { @@ -815,7 +822,7 @@ impl Iterator for Bytes<'_> { if ret == 0 { None } else { - self.ptr = self.ptr.offset(1); + self.ptr = self.ptr.add(1); Some(ret) } } @@ -825,6 +832,12 @@ impl Iterator for Bytes<'_> { fn size_hint(&self) -> (usize, Option<usize>) { if self.is_empty() { (0, Some(0)) } else { (1, None) } } + + #[inline] + fn count(self) -> usize { + // SAFETY: We always hold a valid pointer to a C string + unsafe { strlen(self.ptr.as_ptr().cast()) } + } } #[unstable(feature = "cstr_bytes", issue = "112115")] |
