-
Notifications
You must be signed in to change notification settings - Fork 20
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
str::from_raw_parts
#167
Comments
How well can one trust FFI code though? There may be subtle impedance mismatches such as the C side being fine with overlong sequences, unpaired surrogates and such. So idk if making the unchecked route less verbose than the checked route is a good idea. |
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. |
We discussed this in today's libs-api meeting. We went back and forth a bit (and mused about whether it should have |
Initial implementation of `str::from_raw_parts[_mut]` ACP (accepted): rust-lang/libs-team#167 Tracking issue: rust-lang#119206 Thanks to `@Kixiron` for previous work on this (rust-lang#107207) `@rustbot` label +T-libs-api -T-libs r? `@thomcc` Closes rust-lang#107207.
Initial implementation of `str::from_raw_parts[_mut]` ACP (accepted): rust-lang/libs-team#167 Tracking issue: rust-lang#119206 Thanks to ``@Kixiron`` for previous work on this (rust-lang#107207) ``@rustbot`` label +T-libs-api -T-libs r? ``@thomcc`` Closes rust-lang#107207.
Rollup merge of rust-lang#119466 - Sky9x:str_from_raw_parts, r=dtolnay Initial implementation of `str::from_raw_parts[_mut]` ACP (accepted): rust-lang/libs-team#167 Tracking issue: rust-lang#119206 Thanks to ``@Kixiron`` for previous work on this (rust-lang#107207) ``@rustbot`` label +T-libs-api -T-libs r? ``@thomcc`` Closes rust-lang#107207.
Proposal
Add the
str::from_raw_parts
andstr::from_raw_parts_mut
apisProblem statement
Creating a
str
from raw parts is a fairly boilerplate filled task.Motivation, use-cases
Often in unsafe or FFI-related code users end up creating strings from their constituent parts, pointers and lengths. However, as of now there's no direct way to do this for
str
, instead the user must first create a byte slice and then create a string slice from said byte slice, requiring the user to write more code than they really should have to. This proposal turns the pattern ofinto a simple
str::from_raw_parts(ptr, len)
call.Solution sketches
Instead of putting the functions within the
str
module, they could be methods onstr
itself to keep the user from having to import thestr
module or fully qualifyingcore::str::from_raw_parts
wherever they need to use the function.Links and related work
The text was updated successfully, but these errors were encountered: