Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking Issue for CStr::bytes #112115

Open
4 of 7 tasks
clarfonthey opened this issue May 30, 2023 · 1 comment
Open
4 of 7 tasks

Tracking Issue for CStr::bytes #112115

clarfonthey opened this issue May 30, 2023 · 1 comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented May 30, 2023

Feature gate: #![feature(cstr_bytes)]

This is a tracking issue for the CStr::bytes API.

Public API

impl CStr {
    pub fn bytes(&self) -> CStrBytes<'_>;
}

// core::ffi::c_str::Bytes
pub struct Bytes<'a> {
    // ...
}
impl Iterator for Bytes<'_> {
    type Item = u8;
    // ...
}

Steps / History

Unresolved Questions

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/api-change-proposals.html

  2. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@clarfonthey clarfonthey added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels May 30, 2023
@clarfonthey clarfonthey changed the title Tracking Issue for XXX Tracking Issue for CStr::bytes May 30, 2023
@clarfonthey
Copy link
Contributor Author

Going through old branches and figured I might as well include the diff for the AsRef impl here just in case we ever do decide to go with it, and so that I can prune the branch locally.

diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs
index dbdbaccb535..d571e9acb3c 100644
--- a/library/core/src/ffi/c_str.rs
+++ b/library/core/src/ffi/c_str.rs
@@ -783,6 +783,23 @@ fn is_empty(&self) -> bool {
         // the nul terminator.
         unsafe { self.ptr.read() == 0 }
     }
+
+    /// Returns the remainder of the string as a slice.
+    #[unstable(feature = "cstr_bytes", issue = "none")]
+    #[inline]
+    pub fn as_c_str(&self) -> &'a CStr {
+        // SAFETY: We start with a valid C string and never increment beyond the
+        // nul terminator, retaining a valid C string, even if it's empty.
+        unsafe { CStr::from_ptr(self.ptr.as_ptr().cast_const().cast()) }
+    }
+}
+
+#[unstable(feature = "cstr_bytes", issue = "none")]
+impl AsRef<CStr> for Bytes<'_> {
+    #[inline]
+    fn as_ref(&self) -> &CStr {
+        self.as_c_str()
+    }
 }

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

tgross35 added a commit to tgross35/rust that referenced this issue Jul 16, 2024
`impl Send + Sync` and override `count` for the `CStr::bytes` iterator

cc tracking issue rust-lang#112115
tgross35 added a commit to tgross35/rust that referenced this issue Jul 16, 2024
`impl Send + Sync` and override `count` for the `CStr::bytes` iterator

cc tracking issue rust-lang#112115
tgross35 added a commit to tgross35/rust that referenced this issue Jul 16, 2024
`impl Send + Sync` and override `count` for the `CStr::bytes` iterator

cc tracking issue rust-lang#112115
tgross35 added a commit to tgross35/rust that referenced this issue Jul 17, 2024
`impl Send + Sync` and override `count` for the `CStr::bytes` iterator

cc tracking issue rust-lang#112115
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 17, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant