Skip to content

Commit

Permalink
auto merge of #20490 : japaric/rust/assoc-types, r=aturon
Browse files Browse the repository at this point in the history
closes #20486 
closes #20474 
closes #20441

[breaking-change]

The `Index[Mut]` traits now have one less input parameter, as the return type of the indexing operation is an associated type. This breaks all existing implementations.

---

binop traits (`Add`, `Sub`, etc) now have an associated type for their return type. Also, the RHS input parameter now defaults to `Self` (except for the `Shl` and `Shr` traits). For example, the `Add` trait now looks like this:

``` rust
trait Add<Rhs=Self> {
    type Output;

    fn add(self, Rhs) -> Self::Output;
}
```

The `Neg` and `Not` traits now also have an associated type for their return type.

This breaks all existing implementations of these traits.

---
Affected traits:

- `Iterator { type Item }`
- `IteratorExt` no input/output types, uses `<Self as Iterator>::Item` in its methods
- `DoubleEndedIterator` no input/output types, uses `<Self as Iterator>::Item` in its methods
- `DoubleEndedIteratorExt` no input/output types, uses `<Self as Iterator>::Item` in its methods
- `RandomAccessIterator` no input/output types
- `ExactSizeIterator` no input/output types, uses `<Self as Iterator>::Item` in its methods

This breaks all the implementations of these traits.
  • Loading branch information
bors committed Jan 4, 2015
2 parents 496dc4e + ce8f748 commit c6c7866
Show file tree
Hide file tree
Showing 137 changed files with 1,788 additions and 826 deletions.
28 changes: 17 additions & 11 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,9 @@ impl<'a, T> Clone for Iter<'a, T> {
}

#[stable]
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

#[inline]
fn next(&mut self) -> Option<&'a T> { self.iter.next() }

Expand All @@ -582,21 +584,23 @@ impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
}

#[stable]
impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
}

#[stable]
impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {}
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}

/// An iterator that moves out of a `BinaryHeap`.
pub struct IntoIter<T> {
iter: vec::IntoIter<T>,
}

#[stable]
impl<T> Iterator<T> for IntoIter<T> {
impl<T> Iterator for IntoIter<T> {
type Item = T;

#[inline]
fn next(&mut self) -> Option<T> { self.iter.next() }

Expand All @@ -605,21 +609,23 @@ impl<T> Iterator<T> for IntoIter<T> {
}

#[stable]
impl<T> DoubleEndedIterator<T> for IntoIter<T> {
impl<T> DoubleEndedIterator for IntoIter<T> {
#[inline]
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
}

#[stable]
impl<T> ExactSizeIterator<T> for IntoIter<T> {}
impl<T> ExactSizeIterator for IntoIter<T> {}

/// An iterator that drains a `BinaryHeap`.
pub struct Drain<'a, T: 'a> {
iter: vec::Drain<'a, T>,
}

#[stable]
impl<'a, T: 'a> Iterator<T> for Drain<'a, T> {
impl<'a, T: 'a> Iterator for Drain<'a, T> {
type Item = T;

#[inline]
fn next(&mut self) -> Option<T> { self.iter.next() }

Expand All @@ -628,24 +634,24 @@ impl<'a, T: 'a> Iterator<T> for Drain<'a, T> {
}

#[stable]
impl<'a, T: 'a> DoubleEndedIterator<T> for Drain<'a, T> {
impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
}

#[stable]
impl<'a, T: 'a> ExactSizeIterator<T> for Drain<'a, T> {}
impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {}

#[stable]
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BinaryHeap<T> {
fn from_iter<Iter: Iterator<Item=T>>(iter: Iter) -> BinaryHeap<T> {
BinaryHeap::from_vec(iter.collect())
}
}

#[stable]
impl<T: Ord> Extend<T> for BinaryHeap<T> {
fn extend<Iter: Iterator<T>>(&mut self, mut iter: Iter) {
fn extend<Iter: Iterator<Item=T>>(&mut self, mut iter: Iter) {
let (lower, _) = iter.size_hint();

self.reserve(lower);
Expand Down
59 changes: 45 additions & 14 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ pub struct Bitv {
nbits: uint
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing)
impl Index<uint,bool> for Bitv {
#[inline]
Expand All @@ -176,6 +178,21 @@ impl Index<uint,bool> for Bitv {
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing)
impl Index<uint> for Bitv {
type Output = bool;

#[inline]
fn index(&self, i: &uint) -> &bool {
if self.get(*i).expect("index out of bounds") {
&TRUE
} else {
&FALSE
}
}
}

/// Computes how many blocks are needed to store that many bits
fn blocks_for_bits(bits: uint) -> uint {
// If we want 17 bits, dividing by 32 will produce 0. So we add 1 to make sure we
Expand Down Expand Up @@ -938,7 +955,7 @@ impl Default for Bitv {

#[stable]
impl FromIterator<bool> for Bitv {
fn from_iter<I:Iterator<bool>>(iterator: I) -> Bitv {
fn from_iter<I:Iterator<Item=bool>>(iterator: I) -> Bitv {
let mut ret = Bitv::new();
ret.extend(iterator);
ret
Expand All @@ -948,7 +965,7 @@ impl FromIterator<bool> for Bitv {
#[stable]
impl Extend<bool> for Bitv {
#[inline]
fn extend<I: Iterator<bool>>(&mut self, mut iterator: I) {
fn extend<I: Iterator<Item=bool>>(&mut self, mut iterator: I) {
let (min, _) = iterator.size_hint();
self.reserve(min);
for element in iterator {
Expand Down Expand Up @@ -1031,7 +1048,9 @@ pub struct Iter<'a> {
}

#[stable]
impl<'a> Iterator<bool> for Iter<'a> {
impl<'a> Iterator for Iter<'a> {
type Item = bool;

#[inline]
fn next(&mut self) -> Option<bool> {
if self.next_idx != self.end_idx {
Expand All @@ -1050,7 +1069,7 @@ impl<'a> Iterator<bool> for Iter<'a> {
}

#[stable]
impl<'a> DoubleEndedIterator<bool> for Iter<'a> {
impl<'a> DoubleEndedIterator for Iter<'a> {
#[inline]
fn next_back(&mut self) -> Option<bool> {
if self.next_idx != self.end_idx {
Expand All @@ -1063,10 +1082,10 @@ impl<'a> DoubleEndedIterator<bool> for Iter<'a> {
}

#[stable]
impl<'a> ExactSizeIterator<bool> for Iter<'a> {}
impl<'a> ExactSizeIterator for Iter<'a> {}

#[stable]
impl<'a> RandomAccessIterator<bool> for Iter<'a> {
impl<'a> RandomAccessIterator for Iter<'a> {
#[inline]
fn indexable(&self) -> uint {
self.end_idx - self.next_idx
Expand Down Expand Up @@ -1134,7 +1153,7 @@ impl Default for BitvSet {

#[stable]
impl FromIterator<uint> for BitvSet {
fn from_iter<I:Iterator<uint>>(iterator: I) -> BitvSet {
fn from_iter<I:Iterator<Item=uint>>(iterator: I) -> BitvSet {
let mut ret = BitvSet::new();
ret.extend(iterator);
ret
Expand All @@ -1144,7 +1163,7 @@ impl FromIterator<uint> for BitvSet {
#[stable]
impl Extend<uint> for BitvSet {
#[inline]
fn extend<I: Iterator<uint>>(&mut self, mut iterator: I) {
fn extend<I: Iterator<Item=uint>>(&mut self, mut iterator: I) {
for i in iterator {
self.insert(i);
}
Expand Down Expand Up @@ -1792,7 +1811,9 @@ pub struct Difference<'a>(TwoBitPositions<'a>);
pub struct SymmetricDifference<'a>(TwoBitPositions<'a>);

#[stable]
impl<'a> Iterator<uint> for SetIter<'a> {
impl<'a> Iterator for SetIter<'a> {
type Item = uint;

fn next(&mut self) -> Option<uint> {
while self.next_idx < self.set.bitv.len() {
let idx = self.next_idx;
Expand All @@ -1813,7 +1834,9 @@ impl<'a> Iterator<uint> for SetIter<'a> {
}

#[stable]
impl<'a> Iterator<uint> for TwoBitPositions<'a> {
impl<'a> Iterator for TwoBitPositions<'a> {
type Item = uint;

fn next(&mut self) -> Option<uint> {
while self.next_idx < self.set.bitv.len() ||
self.next_idx < self.other.bitv.len() {
Expand Down Expand Up @@ -1849,25 +1872,33 @@ impl<'a> Iterator<uint> for TwoBitPositions<'a> {
}

#[stable]
impl<'a> Iterator<uint> for Union<'a> {
impl<'a> Iterator for Union<'a> {
type Item = uint;

#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
}

#[stable]
impl<'a> Iterator<uint> for Intersection<'a> {
impl<'a> Iterator for Intersection<'a> {
type Item = uint;

#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
}

#[stable]
impl<'a> Iterator<uint> for Difference<'a> {
impl<'a> Iterator for Difference<'a> {
type Item = uint;

#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
}

#[stable]
impl<'a> Iterator<uint> for SymmetricDifference<'a> {
impl<'a> Iterator for SymmetricDifference<'a> {
type Item = uint;

#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
}
Expand Down
Loading

0 comments on commit c6c7866

Please sign in to comment.