-
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
Add ptr::{str_from_raw_parts, str_from_raw_parts_mut}
functions
#91216
Conversation
The functions are under feature gate `str_from_raw_parts` and are similar to `slice_from_raw_parts`, `slice_from_raw_parts_mut`.
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
8e50c5a
to
927d775
Compare
r? @dtolnay |
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 would prefer not to add these. In the long term, ptr::from_raw_parts
should be fine for this use case. In the short term, it sounded like the only immediate motivation for adding these APIs was:
#85816 (comment)
it seemed weird to have methods on*const str
without a convenient way to create such pointers
This isn't compelling to me because people are successfully and reasonably conveniently working with *const str
in real code today; they aren't the ones asking for this. A few examples:
as
cast from *const [u8]
:
Lines 1818 to 1819 in 23f6923
let rc = Rc::<[u8]>::from(v.as_bytes()); | |
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const str) } |
as
cast from &str
:
rust/compiler/rustc_span/src/symbol.rs
Lines 1767 to 1772 in 23f6923
let string: &str = | |
unsafe { str::from_utf8_unchecked(inner.arena.alloc_slice(string.as_bytes())) }; | |
// SAFETY: we can extend the arena allocation to `'static` because we | |
// only access these while the arena is still alive. | |
let string: &'static str = unsafe { &*(string as *const str) }; |
@dtolnay that makes sense. I didn't know the Considering how easy it is to construct |
TL;DR: this PR adds the following API:
The functions are under feature gate
str_from_raw_parts
and are similar toslice_from_raw_parts
,slice_from_raw_parts_mut
andNonNull::slice_from_raw_parts
.These methods were originally proposed as a part of #85816 and there were some concerns about the specific design we should use for
str
pointer construction.Some alternative designs:
&str
construction (str::from_utf8
)Cons: forces to use 2 functions (
str_from_raw_utf8(slice_from_raw_parts(ptr, len))
) to convertptr, len
into*const str
str::from_raw_parts
too (addition to the current design of this PR)Cons: adds a function to
str
which can be replaced withslice::from_raw_parts
andstr::from_utf8_unchecked
(in this case having two functions is probably better, as it splits safety conditions into smaller parts)ptr::from_raw_parts
Pros: doesn't add anything new, reuses existing API
Cons: less clear/longer stabilization path