From f2a01ea27734c9b20389f75d2b0c0da34f4ba5b7 Mon Sep 17 00:00:00 2001 From: Palmer Cox
Date: Sat, 30 Nov 2013 16:28:42 -0500
Subject: Implement mut_chunks() method for MutableVector trait.
mut_chunks() returns an iterator that produces mutable slices. This is the
mutable version of the existing chunks() method on the ImmutableVector trait.
---
src/libstd/vec.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+)
(limited to 'src/libstd')
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 293c9ed9817..d7b1f188314 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -1933,6 +1933,18 @@ pub trait MutableVector<'self, T> {
/// Returns a reversed iterator that allows modifying each value
fn mut_rev_iter(self) -> MutRevIterator<'self, T>;
+ /**
+ * Returns an iterator over `size` elements of the vector at a time.
+ * The chunks are mutable and do not overlap. If `size` does not divide the
+ * length of the vector, then the last chunk will not have length
+ * `size`.
+ *
+ * # Failure
+ *
+ * Fails if `size` is 0.
+ */
+ fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'self, T>;
+
/**
* Returns a mutable reference to the first element in this slice
* and adjusts the slice in place so that it no longer contains
@@ -2069,6 +2081,13 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
self.mut_iter().invert()
}
+ #[inline]
+ fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'self, T> {
+ assert!(chunk_size > 0);
+ let len = self.len();
+ MutChunkIter { v: self, chunk_size: chunk_size, remaining: len }
+ }
+
fn mut_shift_ref(&mut self) -> &'self mut T {
unsafe {
let s: &mut Slice