Add ptr::{str_from_raw_parts, str_from_raw_parts_mut} functions#91216
Add ptr::{str_from_raw_parts, str_from_raw_parts_mut} functions#91216WaffleLapkin wants to merge 2 commits intorust-lang:masterfrom
ptr::{str_from_raw_parts, str_from_raw_parts_mut} functions#91216Conversation
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 |
dtolnay
left a comment
There was a problem hiding this comment.
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 strwithout 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
as cast from &str:
rust/compiler/rustc_span/src/symbol.rs
Lines 1767 to 1772 in 23f6923
|
@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_partsand are similar toslice_from_raw_parts,slice_from_raw_parts_mutandNonNull::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
strpointer construction.Some alternative designs:
&strconstruction (str::from_utf8)Cons: forces to use 2 functions (
str_from_raw_utf8(slice_from_raw_parts(ptr, len))) to convertptr, leninto*const strstr::from_raw_partstoo (addition to the current design of this PR)Cons: adds a function to
strwhich can be replaced withslice::from_raw_partsandstr::from_utf8_unchecked(in this case having two functions is probably better, as it splits safety conditions into smaller parts)ptr::from_raw_partsPros: doesn't add anything new, reuses existing API
Cons: less clear/longer stabilization path