-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Request: Lossy way to move Vec<u8>
into a String
#64727
Comments
@Lokathor - I would like to implement this (I have no contributions yet in the project). May i please claim the request? |
I think that normally the Libs team is given a chance to comment on the issue (it's only been 3 hours), but the worst that can happen is that they reject your PR. All I can say for sure is that I will not be implementing this particular patch myself. |
Thanks for letting me know, I will wait until someone from the lib team will respond. |
Seems like the right API here is actually |
|
Hm, I had thought that the lossy conversion always replaced a single byte with a single byte, if that's not the case then the slice case is indeed not all that helpful probably. If this was true then the slice case would be good to have as a more general form, though. |
So we want this signature: |
That is one option. Another would be to add a method to FromUtf8 so that you can consume that error into the lossy String. The value here is not having to start over on the utf8 parsing. |
There was a post with possible designs on internals: |
I hope |
I prefer this approach for a couple of reasons. It signals whether or not the conversion was lossless. Whereas if we went with the signature Also, it will deter people from checking for the presence of And even for users who "just want a string" and don't care about the error, you can still reasonably get that in single readable line: String::from_utf8(v).unwrap_or_else(|e| e.into_string_lossy()) |
Rollup merge of rust-lang#129439 - okaneco:vec_string_lossy, r=Noratrieb Implement feature `string_from_utf8_lossy_owned` for lossy conversion from `Vec<u8>` to `String` methods Accepted ACP: rust-lang/libs-team#116 Tracking issue: rust-lang#129436 Implement feature for lossily converting from `Vec<u8>` to `String` - Add `String::from_utf8_lossy_owned` - Add `FromUtf8Error::into_utf8_lossy` --- Related to rust-lang#64727, but unsure whether to mark it "fixed" by this PR. That issue partly asks for in-place replacement of the original allocation. We fulfill the other half of that request with these functions. closes rust-lang#64727
If you have a
Vec<u8>
of utf8 data there's three ways to get aString
:from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error>
from_utf8_lossy(v: &'a [u8]) -> Cow<'a, str>
unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String
There doesn't appear to be any way to hand over the existing
Vec<u8>
and have any necessary character changes into�
be done in place. Thestd::string::FromUtf8Error
type doesn't, for example, have a way to just do a lossy in-place conversion.The text was updated successfully, but these errors were encountered: