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

add an associated Item type to IntoIterator #22313

Merged
merged 1 commit into from
Feb 17, 2015
Merged

Conversation

japaric
Copy link
Member

@japaric japaric commented Feb 14, 2015

IntoIterator now has an extra associated item:

trait IntoIterator {
    type Item;
    type IntoIter: Iterator<Self=Self::Item>;
}

This lets you bind the iterator "Item" directly when writing generic functions:

// hypothetical change, not included in this PR
impl Extend<T> for Vec<T> {
    // you can now write
    fn extend<I>(&mut self, it: I) where I: IntoIterator<Item=T> { .. }
    // instead of
    fn extend<I: IntoIterator>(&mut self, it: I) where I::IntoIter: Iterator<Item=T> { .. }
}

The downside is that now you have to write an extra associated type in your IntoIterator implementations:

 impl<T> IntoIterator for Vec<T> {
+    type Item = T;
     type IntoIter = IntoIter<T>;

     fn into_iter(self) -> IntoIter<T> { .. }
 }

Because this breaks all downstream implementations of IntoIterator, this is a [breaking-change]


r? @aturon

@huonw
Copy link
Member

huonw commented Feb 14, 2015

Does type Item = <Self::IntoIter as Iterator>::Item; work?

@japaric
Copy link
Member Author

japaric commented Feb 14, 2015

@huonw AFAIK default type params doesn't yet work with associated types (#19476), but when it does, the idea is to use something like that to avoid having to write the type Item = .. part in the implementations.

@aturon
Copy link
Member

aturon commented Feb 15, 2015

Thanks!

@bors: r+ e727378

@bors
Copy link
Contributor

bors commented Feb 16, 2015

⌛ Testing commit e727378 with merge 1f82fa6...

@bors
Copy link
Contributor

bors commented Feb 16, 2015

💔 Test failed - auto-linux-64-nopt-t

@alexcrichton
Copy link
Member

@bors: retry

Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 17, 2015
 `IntoIterator` now has an extra associated item:

``` rust
trait IntoIterator {
    type Item;
    type IntoIter: Iterator<Self=Self::Item>;
}
```

This lets you bind the iterator \"`Item`\" directly when writing generic functions:

``` rust
// hypothetical change, not included in this PR
impl Extend<T> for Vec<T> {
    // you can now write
    fn extend<I>(&mut self, it: I) where I: IntoIterator<Item=T> { .. }
    // instead of
    fn extend<I: IntoIterator>(&mut self, it: I) where I::IntoIter: Iterator<Item=T> { .. }
}
```

The downside is that now you have to write an extra associated type in your `IntoIterator` implementations:

``` diff
 impl<T> IntoIterator for Vec<T> {
+    type Item = T;
     type IntoIter = IntoIter<T>;

     fn into_iter(self) -> IntoIter<T> { .. }
 }
```

Because this breaks all downstream implementations of `IntoIterator`, this is a [breaking-change]

---

r? @aturon
@bors bors merged commit e727378 into rust-lang:master Feb 17, 2015
@japaric japaric deleted the iter branch February 17, 2015 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants