Uh oh!
There was an error while loading. Please reload this page.
implement From<Vec<char>> and From<&'a [char]> for String - #35054
Conversation
rust-highfive
commented
Jul 26, 2016
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
pwoolcoc
commented
Jul 26, 2016
I know that |
tbu-
commented
Jul 26, 2016
|
There was a problem hiding this comment.
It probably makes sense to delegate to the &[char] impl here instead of duplicating the code.
It's possible we could do something clever to reuse the Vecs buffer, though I don't think it's worth thinking about in this PR.
That's an unstable optimization for purely ASCII text. A single non-ascii character in the string of any size (think of © or «) and the buffer is guaranteed to be reallocated. It may be reasonable to use some small correction ( |
tbu-
commented
Jul 26, 2016
@petrochenkov Yes, a single non-ASCII character will change that. However, this is what we do everywhere, allocations are always based on the minimum capacity that will be necessary (it's always the |
pwoolcoc
commented
Jul 26, 2016
Thanks for the suggestions everyone. I'll be back online in a couple hours & I'll push some changes. -------- Original message -------- @petrochenkov Yes, a single non-ASCII character will change that. However, this is what we do everywhere, allocations are always based on the minimum capacity that will be necessary (it's always the Iterator::size_hint().0 that is used for initial capacity). — |
alexcrichton
commented
Jul 26, 2016
Sounds reasonable to me! |
pwoolcoc
commented
Jul 27, 2016
Is it ok to leave the impl as |
pwoolcoc
commented
Jul 27, 2016
r? @brson |
I feel like the implFrom<Vec<char>>forString{fnfrom(mutv:Vec<char>) -> String{unsafe{let ptr = v.as_mut_ptr()as*mutu8;letmut bytes = 0;{letmut rest = v.as_mut_slice();whileletSome((chr, rest_)) = {rest}.split_first_mut(){for byte in chr.encode_utf8(){*ptr.offset(bytes) = byte;
bytes += 1;}
rest = rest_;}}let cap = v.capacity();::std::mem::forget(v);String::from_raw_parts(ptr, bytes asusize, cap)}}}// Perhaps this code could be made better, I didn’t ponder much on it. |
alexcrichton
commented
Jul 27, 2016
@nagisa as @brsonmentioned earlier we could indeed do things like reuse the buffer, but for now it doesn't seem worth the unsafe complexity when no one's clamboring for it. @pwoolcoc yeah I think it's best to stay concrete and avoid generics for |
pwoolcoc
commented
Jul 27, 2016
thanks @alexcrichton, I think it is ready to go but I am unable to replicate the test failure that travis is reporting |
alexcrichton
commented
Jul 27, 2016
Ah yeah that's ok, if you rebase on master it should fix it as the PR to solve that problem went in a few hours ago |
Though there are ways to convert a slice or vec of chars into a string, it would be nice to be able to just do `String::from(['a', 'b', 'c'])`, so this PR implements `From<Vec<char>>` and `From<&'a [char]>` for String.
nagisa
commented
Jul 27, 2016
Seems to go at odds with the philosophy of I’d like to point out that I implemented the code I pasted above manually two or three times already in various locations and so far the desire to reuse the allocation was pretty strong in each use-case. The implementation as proposed by the PR is plain useless as far as I’m and my code are concerned, which is exactly why I am complaining. Shall I send a PR against this PR? |
brson
commented
Jul 27, 2016
@pwoolcoc The next steps are to wait for the libs team to approve the new APIs. Typically this takes until next tuesday, though if enough of them chime in here it could go faster. @nagisa I recognize that optimization is desirable, but still prefer to do it as a follow up, for a few reasons: unsafe optimizations require a different set of eyes and more thorough review; I want to lower barriers to contribution, not frustrate contributors by expanding the scope of PRs, make landing small contributions faster; generally, I'd like to hold up fewer issues by waiting for perfection, and be more willing to settle for incremental progress. (On the subject of making small / first-time contributions faster - it is quite frustrating that any minor lib feature enhancements have a week turnaround waiting for the libs team to meet face-to-face. Very bad contributor experience.) |
pwoolcoc
commented
Jul 27, 2016
@brson ok, thanks! |
brson
commented
Aug 1, 2016
@bors r+ libs team is happy |
bors
commented
Aug 1, 2016
📌 Commit ac73335 has been approved by |
bors
commented
Aug 2, 2016
⌛ Testing commit ac73335 with merge 19765f2... |
implement `From<Vec<char>>` and `From<&'a [char]>` for `String` Though there are ways to convert a slice or vec of chars into a string, it would be nice to be able to just do `String::from(&['a', 'b', 'c'])`, so this PR implements `From<Vec<char>>` and `From<&'a [char]>` for String.
bors
commented
Aug 2, 2016
Revert "implement `From<Vec<char>>` and `From<&'a [char]>` for `String`" This reverts commit ac73335. This is a revert of #35054, which resulted in at least 7 known regressions, reported [here](https://internals.rust-lang.org/t/regression-report-stable-2016-08-16-vs-beta-2016-09-21/4119) and [here](#36352), which will hit stable next week. I think this breakage was somewhat unanticipated, and we did not realize so many crates were broken until this week, so reverting is the conservative thing to do until we figure out how not to cause so much breakage. I've run crater on the revert and did not find any new breakage from the revert. Fixes#36352 cc @pwoolcoc@rust-lang/libs
Though there are ways to convert a slice or vec of chars into a string,
it would be nice to be able to just do
String::from(&['a', 'b', 'c']),so this PR implements
From<Vec<char>>andFrom<&'a [char]>forString.