Skip to content

Commit 2a8dfc3

Browse files
author
Palmer Cox
committed
Implement DoubleEndedIterator for MutChunkIter.
1 parent f2a01ea commit 2a8dfc3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libstd/vec.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2611,6 +2611,23 @@ impl<'self, T> Iterator<&'self mut [T]> for MutChunkIter<'self, T> {
26112611
}
26122612
}
26132613

2614+
impl<'self, T> DoubleEndedIterator<&'self mut [T]> for MutChunkIter<'self, T> {
2615+
#[inline]
2616+
fn next_back(&mut self) -> Option<&'self mut [T]> {
2617+
if self.remaining == 0 {
2618+
None
2619+
} else {
2620+
let remainder = self.remaining % self.chunk_size;
2621+
let sz = if remainder != 0 { remainder } else { self.chunk_size };
2622+
let tmp = util::replace(&mut self.v, &mut []);
2623+
let (head, tail) = tmp.mut_split(self.remaining - sz);
2624+
self.v = head;
2625+
self.remaining -= sz;
2626+
Some(tail)
2627+
}
2628+
}
2629+
}
2630+
26142631
/// An iterator that moves out of a vector.
26152632
#[deriving(Clone)]
26162633
pub struct MoveIterator<T> {
@@ -4033,6 +4050,18 @@ mod tests {
40334050
assert_eq!(v, result);
40344051
}
40354052

4053+
#[test]
4054+
fn test_mut_chunks_invert() {
4055+
let mut v = [0u8, 1, 2, 3, 4, 5, 6];
4056+
for (i, chunk) in v.mut_chunks(3).invert().enumerate() {
4057+
for x in chunk.mut_iter() {
4058+
*x = i as u8;
4059+
}
4060+
}
4061+
let result = [2u8, 2, 2, 1, 1, 1, 0];
4062+
assert_eq!(v, result);
4063+
}
4064+
40364065
#[test]
40374066
#[should_fail]
40384067
fn test_mut_chunks_0() {

0 commit comments

Comments
 (0)