Skip to content

Commit 872d15d

Browse files
author
blake2-ppc
committed
std: Implement RandomAccessIterator for Invert
1 parent c5e4c55 commit 872d15d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/libstd/iterator.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Invert<T> {
106106
fn next_back(&mut self) -> Option<A> { self.iter.next() }
107107
}
108108

109+
impl<A, T: DoubleEndedIterator<A> + RandomAccessIterator<A>> RandomAccessIterator<A>
110+
for Invert<T> {
111+
#[inline]
112+
fn indexable(&self) -> uint { self.iter.indexable() }
113+
#[inline]
114+
fn idx(&self, index: uint) -> Option<A> {
115+
self.iter.idx(self.indexable() - index - 1)
116+
}
117+
}
118+
109119
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
110120
/// implementations of the `Iterator` trait.
111121
///
@@ -2017,6 +2027,17 @@ mod tests {
20172027
check_randacc_iter(xs.iter().enumerate(), xs.len());
20182028
}
20192029

2030+
#[test]
2031+
fn test_random_access_invert() {
2032+
let xs = [1, 2, 3, 4, 5];
2033+
check_randacc_iter(xs.iter().invert(), xs.len());
2034+
let mut it = xs.iter().invert();
2035+
it.next();
2036+
it.next_back();
2037+
it.next();
2038+
check_randacc_iter(it, xs.len() - 3);
2039+
}
2040+
20202041
#[test]
20212042
fn test_random_access_zip() {
20222043
let xs = [1, 2, 3, 4, 5];

0 commit comments

Comments
 (0)