about summary refs log tree commit diff
path: root/src/libcore/slice
diff options
context:
space:
mode:
authorSebastian Dröge <sebastian@centricular.com>2018-01-02 23:54:42 +0200
committerSebastian Dröge <sebastian@centricular.com>2018-01-13 12:18:55 +0200
commitaa0c08a22c32da5cc1db16fba2e5e6b762a47de5 (patch)
tree8796a580cd2a0dc79a27df5974b23343b9e34bb4 /src/libcore/slice
parente51a89a0ad35f42af9c39d4ada25a5ad9746cd62 (diff)
downloadrust-aa0c08a22c32da5cc1db16fba2e5e6b762a47de5.tar.gz
rust-aa0c08a22c32da5cc1db16fba2e5e6b762a47de5.zip
Apply review comments from @bluss
- Simplify nth() by making use of the fact that the slice is evenly
  divisible by the chunk size, and calling next() instead of
  duplicating it
- Call next_back() in last(), they are equivalent
- Implement ExactSizeIterator::is_empty()
Diffstat (limited to 'src/libcore/slice')
-rw-r--r--src/libcore/slice/mod.rs67
1 files changed, 23 insertions, 44 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs
index 5791b1d6e79..2d606f43084 100644
--- a/src/libcore/slice/mod.rs
+++ b/src/libcore/slice/mod.rs
@@ -2448,30 +2448,16 @@ impl<'a, T> Iterator for ExactChunks<'a, T> {
             self.v = &[];
             None
         } else {
-            let end = match start.checked_add(self.chunk_size) {
-                Some(sum) => cmp::min(self.v.len(), sum),
-                None => self.v.len(),
-            };
-
-            if end - start != self.chunk_size {
-                self.v = &[];
-                None
-            } else {
-                let nth = &self.v[start..end];
-                self.v = &self.v[end..];
-                Some(nth)
-            }
+            let (_, snd) = self.v.split_at(start);
+            self.v = snd;
+            assert!(self.v.len() == self.chunk_size);
+            self.next()
         }
     }
 
     #[inline]
-    fn last(self) -> Option<Self::Item> {
-        if self.v.len() < self.chunk_size {
-            None
-        } else {
-            let start = self.v.len() - self.chunk_size;
-            Some(&self.v[start..])
-        }
+    fn last(mut self) -> Option<Self::Item> {
+        self.next_back()
     }
 }
 
@@ -2490,7 +2476,11 @@ impl<'a, T> DoubleEndedIterator for ExactChunks<'a, T> {
 }
 
 #[unstable(feature = "exact_chunks", issue = "47115")]
-impl<'a, T> ExactSizeIterator for ExactChunks<'a, T> {}
+impl<'a, T> ExactSizeIterator for ExactChunks<'a, T> {
+    fn is_empty(&self) -> bool {
+        self.v.is_empty()
+    }
+}
 
 #[unstable(feature = "fused", issue = "35602")]
 impl<'a, T> FusedIterator for ExactChunks<'a, T> {}
@@ -2544,32 +2534,17 @@ impl<'a, T> Iterator for ExactChunksMut<'a, T> {
             self.v = &mut [];
             None
         } else {
-            let end = match start.checked_add(self.chunk_size) {
-                Some(sum) => cmp::min(self.v.len(), sum),
-                None => self.v.len(),
-            };
-
-            if end - start != self.chunk_size {
-                self.v = &mut [];
-                None
-            } else {
-                let tmp = mem::replace(&mut self.v, &mut []);
-                let (head, tail) = tmp.split_at_mut(end);
-                let (_, nth) =  head.split_at_mut(start);
-                self.v = tail;
-                Some(nth)
-            }
+            let tmp = mem::replace(&mut self.v, &mut []);
+            let (_, snd) = tmp.split_at_mut(start);
+            self.v = snd;
+            assert!(self.v.len() == self.chunk_size);
+            self.next()
         }
     }
 
     #[inline]
-    fn last(self) -> Option<Self::Item> {
-        if self.v.len() < self.chunk_size {
-            None
-        } else {
-            let start = (self.v.len() - self.chunk_size) / self.chunk_size * self.chunk_size;
-            Some(&mut self.v[start..])
-        }
+    fn last(mut self) -> Option<Self::Item> {
+        self.next_back()
     }
 }
 
@@ -2590,7 +2565,11 @@ impl<'a, T> DoubleEndedIterator for ExactChunksMut<'a, T> {
 }
 
 #[unstable(feature = "exact_chunks", issue = "47115")]
-impl<'a, T> ExactSizeIterator for ExactChunksMut<'a, T> {}
+impl<'a, T> ExactSizeIterator for ExactChunksMut<'a, T> {
+    fn is_empty(&self) -> bool {
+        self.v.is_empty()
+    }
+}
 
 #[unstable(feature = "fused", issue = "35602")]
 impl<'a, T> FusedIterator for ExactChunksMut<'a, T> {}