auto merge of #10930 : DaGenix/rust/remove-unnecessary-fields, r=alexcrichton
3 minor clean-ups now that #9629 is fixed: * Update MutChunkIter to remove the ```remainder``` that existed just to allow the size_hint() method to be implemented. This is no longer necessary since we can just access the length of the slice directly. * Update MutSplitIterator to address the FIXME in its size_hint() method. This method was only partially implemented due to the issue. Also, implement a minor optimization in the case that its the last iteration. * Update ByRef iterator to implement the size_hint() method. I noticed that MutSplitIterator returns an empty slice if called on an empty slice. I don't know if this is intended or not, but I left the ```finished``` field in-place to preserve this behavior. @TeXitoi @blake2-ppc
This commit is contained in:
commit
20f13b228f
2 changed files with 20 additions and 25 deletions
|
|
@ -795,7 +795,8 @@ pub struct ByRef<'a, T> {
|
|||
impl<'a, A, T: Iterator<A>> Iterator<A> for ByRef<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<A> { self.iter.next() }
|
||||
// FIXME: #9629 we cannot implement &self methods like size_hint on ByRef
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
impl<'a, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for ByRef<'a, T> {
|
||||
|
|
|
|||
|
|
@ -2127,8 +2127,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
|
|||
#[inline]
|
||||
fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'a, T> {
|
||||
assert!(chunk_size > 0);
|
||||
let len = self.len();
|
||||
MutChunkIter { v: self, chunk_size: chunk_size, remaining: len }
|
||||
MutChunkIter { v: self, chunk_size: chunk_size }
|
||||
}
|
||||
|
||||
fn mut_shift_ref(&mut self) -> &'a mut T {
|
||||
|
|
@ -2529,13 +2528,13 @@ impl<'a, T> Iterator<&'a mut [T]> for MutSplitIterator<'a, T> {
|
|||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
if self.finished { return (0, Some(0)) }
|
||||
|
||||
// if the predicate doesn't match anything, we yield one slice
|
||||
// if it matches every element, we yield len+1 empty slices.
|
||||
// FIXME #9629
|
||||
//(1, Some(self.v.len() + 1))
|
||||
(1, None)
|
||||
if self.finished {
|
||||
(0, Some(0))
|
||||
} else {
|
||||
// if the predicate doesn't match anything, we yield one slice
|
||||
// if it matches every element, we yield len+1 empty slices.
|
||||
(1, Some(self.v.len() + 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2548,10 +2547,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplitIterator<'a, T> {
|
|||
None => {
|
||||
self.finished = true;
|
||||
let tmp = util::replace(&mut self.v, &mut []);
|
||||
let len = tmp.len();
|
||||
let (head, tail) = tmp.mut_split_at(len);
|
||||
self.v = tail;
|
||||
Some(head)
|
||||
Some(tmp)
|
||||
}
|
||||
Some(idx) => {
|
||||
let tmp = util::replace(&mut self.v, &mut []);
|
||||
|
|
@ -2568,31 +2564,29 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplitIterator<'a, T> {
|
|||
/// the remainder.
|
||||
pub struct MutChunkIter<'a, T> {
|
||||
priv v: &'a mut [T],
|
||||
priv chunk_size: uint,
|
||||
priv remaining: uint
|
||||
priv chunk_size: uint
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator<&'a mut [T]> for MutChunkIter<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a mut [T]> {
|
||||
if self.remaining == 0 {
|
||||
if self.v.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
let sz = cmp::min(self.remaining, self.chunk_size);
|
||||
let sz = cmp::min(self.v.len(), self.chunk_size);
|
||||
let tmp = util::replace(&mut self.v, &mut []);
|
||||
let (head, tail) = tmp.mut_split_at(sz);
|
||||
self.v = tail;
|
||||
self.remaining -= sz;
|
||||
Some(head)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
if self.remaining == 0 {
|
||||
if self.v.len() == 0 {
|
||||
(0, Some(0))
|
||||
} else {
|
||||
let (n, rem) = self.remaining.div_rem(&self.chunk_size);
|
||||
let (n, rem) = self.v.len().div_rem(&self.chunk_size);
|
||||
let n = if rem > 0 { n + 1 } else { n };
|
||||
(n, Some(n))
|
||||
}
|
||||
|
|
@ -2602,15 +2596,15 @@ impl<'a, T> Iterator<&'a mut [T]> for MutChunkIter<'a, T> {
|
|||
impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutChunkIter<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a mut [T]> {
|
||||
if self.remaining == 0 {
|
||||
if self.v.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
let remainder = self.remaining % self.chunk_size;
|
||||
let remainder = self.v.len() % self.chunk_size;
|
||||
let sz = if remainder != 0 { remainder } else { self.chunk_size };
|
||||
let tmp = util::replace(&mut self.v, &mut []);
|
||||
let (head, tail) = tmp.mut_split_at(self.remaining - sz);
|
||||
let tmp_len = tmp.len();
|
||||
let (head, tail) = tmp.mut_split_at(tmp_len - sz);
|
||||
self.v = head;
|
||||
self.remaining -= sz;
|
||||
Some(tail)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue