You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not a fan of this. This saves a negligible amount of code but bunches up a whole lot of safety considerations. slice::from_raw_parts has language-level memory safety invariants, and str::from_utf8_unchecked has a single library-level safety invariant (that the slice contains valid UTF-8). In code, these should be justified separately. The example in rust-lang/libs-team#167 was
// SAFETY: <explain why ptr and len make a valid slice>let bytes = unsafe{ slice::from_raw_parts(ptr, len)};// SAFETY: <explain why this slice is UTF-8>unsafe{ str::from_utf8_unchecked(bytes)}
I don't see the value of combining these to
// SAFETY: <explain why ptr and len make a valid slice and why this slice is UTF-8>unsafe{ str::from_raw_parts(ptr, len)}
The proposed documentation also says
The pointed-to bytes must be valid UTF-8. If this might not be the case, use str::from_utf8(slice::from_raw_parts(ptr, len)), which will return an Err if the data isn’t valid UTF-8.
which kind of admits that there are two safety considerations that should be handled by two function calls. In the end, docs could just recommend str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)).
This API intends to be the borrowed equivalent the already-stable String::from_raw_parts, and fill in the two missing methods in the table below, bringing the string API more in line with the slice API.
String
Slice
&
❌ str::from_raw_parts
✅ slice::from_raw_parts
&mut
❌ str::from_raw_parts_mut
✅ slice::from_raw_parts_mut
Owned
✅ String::from_raw_parts
✅ Vec::from_raw_parts
The documentation could be improved to properly express the safety concerns.
Also note that the ACP was accepted because of the existence of String::from_raw_parts, which by the same argument could be written as String::from_utf8_unchecked(Vec::from_raw_parts(ptr, len, cap)).
If you really don't want these functions to appear in your codebase, consider the clippy::disallowed_methods lint:
It looks like String::from_raw_parts's safety documentation was at some point copy-pasted from Vec::from_raw_parts's, but it has since diverged and is currently much less detailed. It for example misses the invariant "The allocated size in bytes must be no larger than isize::MAX". This is exactly the issue I'm trying to point out, the documentation basically has to say
Safety
Everything in Vec::<u8>::from_raw_parts's safety requirements
Everything in String::from_utf8_unchecked's safety requirements
at which point why not just make those two calls?
String::from_raw_parts is one method with this issue, does this justify adding two more?
I'll copy my comment from the ACP here, as something potentially still worth discussing at stabilization time:
I'm not convinced that one more function call is "a fairly boilerplate filled task". That's especially true for unsafe code where the hard part is demonstrating that the safety conditions have been met.
Because of that, I think two calls is actually better when the two safety conditions are very different, like they are here.
I think this should be moved to str::from_raw_parts, possibly keeping std::str::from_raw_parts and core::str::from_raw_parts as aliases, for consistency with str::from_utf8 (#131114).
Reacted by Gabriel Bjørnager Jensen, Ellen Emilia Anna Zscheile and anatawa12
Activity
Rollup merge of rust-lang#119466 - Sky9x:str_from_raw_parts, r=dtolnay
Rollup merge of rust-lang#119466 - Sky9x:str_from_raw_parts, r=dtolnay
Unrolled build for rust-lang#119466
Rollup merge of rust-lang#124100 - dev-ardi:master, r=workingjubilee
Rollup merge of rust-lang#124100 - dev-ardi:master, r=workingjubilee
Unrolled build for rust-lang#124100
robertbastian commentedon Jun 18, 2024
I'm not a fan of this. This saves a negligible amount of code but bunches up a whole lot of safety considerations.
slice::from_raw_partshas language-level memory safety invariants, andstr::from_utf8_uncheckedhas a single library-level safety invariant (that the slice contains valid UTF-8). In code, these should be justified separately. The example in rust-lang/libs-team#167 wasbut it should have really been
I don't see the value of combining these to
The proposed documentation also says
which kind of admits that there are two safety considerations that should be handled by two function calls. In the end, docs could just recommend
str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)).Sky9x commentedon Jun 19, 2024
This API intends to be the borrowed equivalent the already-stable
String::from_raw_parts, and fill in the two missing methods in the table below, bringing the string API more in line with the slice API.&str::from_raw_partsslice::from_raw_parts&mutstr::from_raw_parts_mutslice::from_raw_parts_mutString::from_raw_partsVec::from_raw_partsThe documentation could be improved to properly express the safety concerns.
Also note that the ACP was accepted because of the existence of
String::from_raw_parts, which by the same argument could be written asString::from_utf8_unchecked(Vec::from_raw_parts(ptr, len, cap)).If you really don't want these functions to appear in your codebase, consider the
clippy::disallowed_methodslint:robertbastian commentedon Jun 20, 2024
It looks like
String::from_raw_parts's safety documentation was at some point copy-pasted fromVec::from_raw_parts's, but it has since diverged and is currently much less detailed. It for example misses the invariant "The allocated size in bytes must be no larger thanisize::MAX". This is exactly the issue I'm trying to point out, the documentation basically has to sayat which point why not just make those two calls?
String::from_raw_partsis one method with this issue, does this justify adding two more?scottmcm commentedon Oct 1, 2024
I'll copy my comment from the ACP here, as something potentially still worth discussing at stabilization time:
(This is the same sentiment as #119206 (comment) above.)
PureWhiteWu commentedon Dec 9, 2024
Hi, is there any progress on this feature? Seems there's no outstanding questions.
robertbastian commentedon Feb 9, 2025
There is an open discussion on whether this should be done.
If consistency is the driving argument, we should look at this complete table:
str::from_raw_partsslice::from_raw_partsstr::from_raw_parts_mut❌
str::into_raw_partsslice::from_raw_parts_mut❌
slice::into_raw_partsString::from_raw_parts❌
String::into_raw_partsVec::from_raw_parts❌
Vec::into_raw_partsRollup merge of rust-lang#136775 - robertbastian:patch-2, r=Amanieu
Rollup merge of rust-lang#136775 - robertbastian:patch-2, r=Amanieu
Unrolled build for rust-lang#136775
Rollup merge of #136775 - robertbastian:patch-2, r=Amanieu
Rollup merge of rust-lang#136775 - robertbastian:patch-2, r=Amanieu
Rollup merge of rust-lang#136775 - robertbastian:patch-2, r=Amanieu
abgros commentedon Jul 8, 2025
I think this should be moved to
str::from_raw_parts, possibly keepingstd::str::from_raw_partsandcore::str::from_raw_partsas aliases, for consistency withstr::from_utf8(#131114).