Skip to content

Minor cleanup for slice::Chunks and ChunksMut #47113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,14 @@ impl<T> [T] {
core_slice::SliceExt::windows(self, size)
}

/// Returns an iterator over `size` elements of the slice at a
/// time. The chunks are slices and do not overlap. If `size` does
/// Returns an iterator over `chunk_size` elements of the slice at a
/// time. The chunks are slices and do not overlap. If `chunk_size` does
/// not divide the length of the slice, then the last chunk will
/// not have length `size`.
/// not have length `chunk_size`.
///
/// # Panics
///
/// Panics if `size` is 0.
/// Panics if `chunk_size` is 0.
///
/// # Examples
///
Expand All @@ -627,8 +627,8 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn chunks(&self, size: usize) -> Chunks<T> {
core_slice::SliceExt::chunks(self, size)
pub fn chunks(&self, chunk_size: usize) -> Chunks<T> {
core_slice::SliceExt::chunks(self, chunk_size)
}

/// Returns an iterator over `chunk_size` elements of the slice at a time.
Expand Down
32 changes: 16 additions & 16 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ impl<T> SliceExt for [T] {
}

#[inline]
fn chunks(&self, size: usize) -> Chunks<T> {
assert!(size != 0);
Chunks { v: self, size: size }
fn chunks(&self, chunk_size: usize) -> Chunks<T> {
assert!(chunk_size != 0);
Chunks { v: self, chunk_size: chunk_size }
}

#[inline]
Expand Down Expand Up @@ -532,7 +532,7 @@ impl<T> SliceExt for [T] {

#[inline]
fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
assert!(chunk_size > 0);
assert!(chunk_size != 0);
ChunksMut { v: self, chunk_size: chunk_size }
}

Expand Down Expand Up @@ -2117,7 +2117,7 @@ impl<'a, T> ExactSizeIterator for Windows<'a, T> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for Windows<'a, T> {}

/// An iterator over a slice in (non-overlapping) chunks (`size` elements at a
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
/// time).
///
/// When the slice len is not evenly divided by the chunk size, the last slice
Expand All @@ -2131,7 +2131,7 @@ impl<'a, T> FusedIterator for Windows<'a, T> {}
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Chunks<'a, T:'a> {
v: &'a [T],
size: usize
chunk_size: usize
}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
Expand All @@ -2140,7 +2140,7 @@ impl<'a, T> Clone for Chunks<'a, T> {
fn clone(&self) -> Chunks<'a, T> {
Chunks {
v: self.v,
size: self.size,
chunk_size: self.chunk_size,
}
}
}
Expand All @@ -2154,7 +2154,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
if self.v.is_empty() {
None
} else {
let chunksz = cmp::min(self.v.len(), self.size);
let chunksz = cmp::min(self.v.len(), self.chunk_size);
let (fst, snd) = self.v.split_at(chunksz);
self.v = snd;
Some(fst)
Expand All @@ -2166,8 +2166,8 @@ impl<'a, T> Iterator for Chunks<'a, T> {
if self.v.is_empty() {
(0, Some(0))
} else {
let n = self.v.len() / self.size;
let rem = self.v.len() % self.size;
let n = self.v.len() / self.chunk_size;
let rem = self.v.len() % self.chunk_size;
let n = if rem > 0 { n+1 } else { n };
(n, Some(n))
}
Expand All @@ -2180,12 +2180,12 @@ impl<'a, T> Iterator for Chunks<'a, T> {

#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let (start, overflow) = n.overflowing_mul(self.size);
let (start, overflow) = n.overflowing_mul(self.chunk_size);
if start >= self.v.len() || overflow {
self.v = &[];
None
} else {
let end = match start.checked_add(self.size) {
let end = match start.checked_add(self.chunk_size) {
Some(sum) => cmp::min(self.v.len(), sum),
None => self.v.len(),
};
Expand All @@ -2200,7 +2200,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
if self.v.is_empty() {
None
} else {
let start = (self.v.len() - 1) / self.size * self.size;
let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size;
Some(&self.v[start..])
}
}
Expand All @@ -2213,8 +2213,8 @@ impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
if self.v.is_empty() {
None
} else {
let remainder = self.v.len() % self.size;
let chunksz = if remainder != 0 { remainder } else { self.size };
let remainder = self.v.len() % self.chunk_size;
let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
let (fst, snd) = self.v.split_at(self.v.len() - chunksz);
self.v = fst;
Some(snd)
Expand All @@ -2228,7 +2228,7 @@ impl<'a, T> ExactSizeIterator for Chunks<'a, T> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for Chunks<'a, T> {}

/// An iterator over a slice in (non-overlapping) mutable chunks (`size`
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
/// elements at a time). When the slice len is not evenly divided by the chunk
/// size, the last slice of the iteration will be the remainder.
///
Expand Down