Skip to content
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

impl FromIterator<&char> for String #40028

Merged
merged 1 commit into from
Mar 1, 2017

Conversation

withoutboats
Copy link
Contributor

No description provided.

@withoutboats withoutboats changed the title impl FromIter<&char> for String impl FromIterator<&char> for String Feb 22, 2017
@withoutboats
Copy link
Contributor Author

withoutboats commented Feb 22, 2017

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

impl<T> FromIterator<T> for String where String: Extend<T> {
    fn from_iter<I: Iterator<Item = T>>(iter: I) -> String {
        let mut string = String::new();
        string.extend(iter);
        string
    }
}

Nor this even more useful impl:

// something something monoid
impl<T, C> FromIterator<T> for C where C: Default + Extend<T> {
    fn from_iter<I: Iterator<Item = T>>(iter: I) -> String {
        let mut collection = C::default();
        collection.extend(iter);
        collection
    }
}

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 22, 2017
@alexcrichton alexcrichton self-assigned this Feb 22, 2017
@alexcrichton
Copy link
Member

@rfcbot fcp merge

Looks good to me!

@rfcbot
Copy link

rfcbot commented Feb 22, 2017

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
Member

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

@withoutboats
Copy link
Contributor Author

withoutboats commented Feb 24, 2017

@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
Member

ollie27 commented Feb 25, 2017

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
Contributor Author

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
Member

@sfackler yes

@sfackler
Copy link
Member

TIL

@rfcbot
Copy link

rfcbot commented Feb 28, 2017

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

@rfcbot rfcbot 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
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Feb 28, 2017

📌 Commit 097398e has been approved by alexcrichton

@withoutboats
Copy link
Contributor Author

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
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:master Mar 1, 2017
@brson brson added the relnotes Marks issues that should be documented in the release notes of the next release. label Apr 11, 2017
@cuviper
Copy link
Member

cuviper commented Apr 27, 2017

@alexcrichton

@sfackler yes

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
Member

Oh sorry I was thinking of this impl, namely:

impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {

I forgot that it was only Extend, not FromIterator.

@alexcrichton
Copy link
Member

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

@cuviper
Copy link
Member

cuviper commented Apr 27, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants