Skip to content

impl FromIterator<&char> for String - #40028

Merged
bors merged 1 commit into
rust-lang:masterfrom
withoutboats:string_from_iter
Mar 1, 2017
Merged

impl FromIterator<&char> for String#40028
bors merged 1 commit into
rust-lang:masterfrom
withoutboats:string_from_iter

Conversation

@withoutboats

Copy link
Copy Markdown
Contributor

No description provided.

@withoutboatswithoutboats changed the title impl FromIter<&char> for Stringimpl FromIterator<&char> for StringFeb 22, 2017
@withoutboats

withoutboats commented Feb 22, 2017

Copy link
Copy Markdown
ContributorAuthor

Was sad to observe that this impl is not backwards compatible:

impl<T>FromIterator<T>forStringwhereString:Extend<T>{fnfrom_iter<I:Iterator<Item = T>>(iter:I) -> String{letmut string = String::new();
string.extend(iter);
string
}}

Nor this even more useful impl:

// something something monoidimpl<T,C>FromIterator<T>forCwhereC:Default + Extend<T>{fnfrom_iter<I:Iterator<Item = T>>(iter:I) -> String{letmut collection = C::default();
collection.extend(iter);
collection
}}

@alexcrichtonalexcrichton added the T-libs-api [DEPRECATED; DO NOT USE] label Feb 22, 2017
@alexcrichtonalexcrichton self-assigned this Feb 22, 2017
@alexcrichton

Copy link
Copy Markdown
Member

@rfcbot fcp merge

Looks good to me!

@rfcbot

rfcbot commented Feb 22, 2017

Copy link
Copy Markdown

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@sfackler

Copy link
Copy Markdown
Member

Do we do this kind of implicit dereference elsewhere? Can I for example collect an Iterator<Item = &u8> to a Vec<u8>?

@withoutboats

withoutboats commented Feb 24, 2017

Copy link
Copy Markdown
ContributorAuthor

@sfackler we implement Extend<&char> for String (its used in the impl here!), seems to me that there should be a parity between those two (hence the reference to the non-backwards compatible impls). This is the only case where String: Extend<T> but not FromIterator<T>.

But I also would definitely support <T: Copy> FromIterator<&T> for Vec<T>.

@ollie27

Copy link
Copy Markdown
Contributor

If impl FromIterator<&char> for Stringis added then impl FromIterator<&char> for Cow<str> should also be added to keep consistency between the FromIterator impls for String and Cox<str>.

I also think there should be parity between Extend and FromIterator but it may be too late to add the FromIterator<&T> impls to the collections as it might cause inference errors in existing code. For example [0u8, 1, 2].iter().collect::<Vec<_>>() would suddenly be ambiguous. That might also be why those impls don't already exist.

@withoutboats

Copy link
Copy Markdown
ContributorAuthor

I can add that impl to this PR.

I also think there should be parity between Extend and FromIterator but it may be too late to add the FromIterator<&T> impls to the collections as it might cause inference errors in existing code. For example [0u8, 1, 2].iter().collect::<Vec<_>>() would suddenly be ambiguous. That might also be why those impls don't already exist.

(Note there's no extend impl so there's no parity in that case).

That impl would need a crater run, but I have trouble imagining code that would not have enough contextual information to distinguish a Vec<u8> from a Vec<&u8>. Anyway its a separate concept from this PR.

@alexcrichton

Copy link
Copy Markdown
Member

@sfackleryes

@sfackler

Copy link
Copy Markdown
Member

TIL

@rfcbot

Copy link
Copy Markdown

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbotrfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Feb 28, 2017
@alexcrichton

Copy link
Copy Markdown
Member

@bors: r+

@bors

bors commented Feb 28, 2017

Copy link
Copy Markdown
Collaborator

📌 Commit 097398e has been approved by alexcrichton

@withoutboats

Copy link
Copy Markdown
ContributorAuthor

I'll try to remember to put up another PR for the Cow impl

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Mar 1, 2017
…excrichton
impl FromIterator<&char> for String
@frewsxcvfrewsxcv mentioned this pull request Mar 1, 2017
bors added a commit that referenced this pull request Mar 1, 2017
Rollup of 6 pull requests
- Successful merges: #39419, #39936, #39944, #39960, #40028, #40128
- Failed merges:
bors added a commit that referenced this pull request Mar 1, 2017
Rollup of 6 pull requests
- Successful merges: #39419, #39936, #39944, #39960, #40028, #40128
- Failed merges:
@bors
bors merged commit 097398e into rust-lang:masterMar 1, 2017
@brsonbrson added the relnotes Marks issues that should be documented in the release notes of the next release. label Apr 11, 2017
@cuviper

Copy link
Copy Markdown
Member

@alexcrichton

@sfackleryes

AFAICS, that impl is only used for its spec_extend. There's still no FromIterator<&'a T> for Vec<T>, nor for any other collection. String is the only one doing this, and the rest only copy for Extend.

@alexcrichton

Copy link
Copy Markdown
Member

Oh sorry I was thinking of this impl, namely:

impl<'a,T:'a + Copy>Extend<&'aT>forVec<T>{

I forgot that it was only Extend, not FromIterator.

@alexcrichton

Copy link
Copy Markdown
Member

Unfortunately this was just released though so we don't have much time to rethink the merge...

@cuviper

Copy link
Copy Markdown
Member

Yeah, I only got here because of the release notes.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.relnotesMarks issues that should be documented in the release notes of the next release.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@withoutboats@alexcrichton@rfcbot@sfackler@ollie27@bors@cuviper@brson