-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Make CStr
an extern type
#64021
Make CStr
an extern type
#64021
Conversation
r? @cramertj (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@@ -1143,7 +1140,13 @@ impl CStr { | |||
#[inline] | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
pub fn to_bytes_with_nul(&self) -> &[u8] { | |||
unsafe { &*(&self.inner as *const [c_char] as *const [u8]) } | |||
// safety: safe references to `CStr` can only exist if they point to a memory location | |||
// that is terminated by a `\0`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason the original code in from_ptr
didn't assert that the pointer was not null? I'm guessing it either caused miscompilation or pessmizations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I'm guessing an oversight?
Seems |
@Centril We should also add a clippy lint that notices if you use a pointer to |
We discussed this in the lang team meeting today and we were all roughly onboard, with idea that the only additional thing we were promising was that |
No, there are no other changes (except the implicit one being that |
fe24d5d
to
6340dfb
Compare
6340dfb
to
2a7aa53
Compare
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
Rebased and addressed all review comments |
I made one comment about adding a comment for |
4c4a7b4
to
719c734
Compare
Is the question of |
Oh... I did not think about this. Some data:
Proposed solution: extern {
type Foo {
fn size_of_val(&self) -> usize {
unsafe {
let this = self as *const Foo as *const CStr;
this.len()
};
}
fn align_of_val(&self) -> usize {
1
}
}
} We can require every extern type to supply these functions. It's all unstable without a clear way forward anyway. There's no need to see this as us ever intending to stabilize this API, but it would allow the standard library to use it. Since for some types this isn't possible, these types can explicity opt to panic inside the |
I don't think this can happen without further language level work |
@oli-obk What language-level work would be required to make this work? It sounded like you saw a path forward, given that the standard library can use unstable features. |
Well... I'd still need to implement that, and I'm currently swamped in reviews and my own PRs that I need to rebase and a bunch of issues that should really be resolved. Making |
This shrinks
&CStr
to one word size and is thus equivalent to*const c_char
, making it possible to use&CStr
types directly in FFI APIs instead of having to convert from and to at the FFI boundaries