Skip to content

Commit 20f13b2

Browse files
committed
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
2 parents 35fc0c8 + 765bc90 commit 20f13b2

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

src/libstd/iter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,8 @@ pub struct ByRef<'a, T> {
795795
impl<'a, A, T: Iterator<A>> Iterator<A> for ByRef<'a, T> {
796796
#[inline]
797797
fn next(&mut self) -> Option<A> { self.iter.next() }
798-
// FIXME: #9629 we cannot implement &self methods like size_hint on ByRef
798+
#[inline]
799+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
799800
}
800801

801802
impl<'a, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for ByRef<'a, T> {

src/libstd/vec.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,8 +2127,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
21272127
#[inline]
21282128
fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'a, T> {
21292129
assert!(chunk_size > 0);
2130-
let len = self.len();
2131-
MutChunkIter { v: self, chunk_size: chunk_size, remaining: len }
2130+
MutChunkIter { v: self, chunk_size: chunk_size }
21322131
}
21332132

21342133
fn mut_shift_ref(&mut self) -> &'a mut T {
@@ -2529,13 +2528,13 @@ impl<'a, T> Iterator<&'a mut [T]> for MutSplitIterator<'a, T> {
25292528

25302529
#[inline]
25312530
fn size_hint(&self) -> (uint, Option<uint>) {
2532-
if self.finished { return (0, Some(0)) }
2533-
2534-
// if the predicate doesn't match anything, we yield one slice
2535-
// if it matches every element, we yield len+1 empty slices.
2536-
// FIXME #9629
2537-
//(1, Some(self.v.len() + 1))
2538-
(1, None)
2531+
if self.finished {
2532+
(0, Some(0))
2533+
} else {
2534+
// if the predicate doesn't match anything, we yield one slice
2535+
// if it matches every element, we yield len+1 empty slices.
2536+
(1, Some(self.v.len() + 1))
2537+
}
25392538
}
25402539
}
25412540

@@ -2548,10 +2547,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplitIterator<'a, T> {
25482547
None => {
25492548
self.finished = true;
25502549
let tmp = util::replace(&mut self.v, &mut []);
2551-
let len = tmp.len();
2552-
let (head, tail) = tmp.mut_split_at(len);
2553-
self.v = tail;
2554-
Some(head)
2550+
Some(tmp)
25552551
}
25562552
Some(idx) => {
25572553
let tmp = util::replace(&mut self.v, &mut []);
@@ -2568,31 +2564,29 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplitIterator<'a, T> {
25682564
/// the remainder.
25692565
pub struct MutChunkIter<'a, T> {
25702566
priv v: &'a mut [T],
2571-
priv chunk_size: uint,
2572-
priv remaining: uint
2567+
priv chunk_size: uint
25732568
}
25742569

25752570
impl<'a, T> Iterator<&'a mut [T]> for MutChunkIter<'a, T> {
25762571
#[inline]
25772572
fn next(&mut self) -> Option<&'a mut [T]> {
2578-
if self.remaining == 0 {
2573+
if self.v.len() == 0 {
25792574
None
25802575
} else {
2581-
let sz = cmp::min(self.remaining, self.chunk_size);
2576+
let sz = cmp::min(self.v.len(), self.chunk_size);
25822577
let tmp = util::replace(&mut self.v, &mut []);
25832578
let (head, tail) = tmp.mut_split_at(sz);
25842579
self.v = tail;
2585-
self.remaining -= sz;
25862580
Some(head)
25872581
}
25882582
}
25892583

25902584
#[inline]
25912585
fn size_hint(&self) -> (uint, Option<uint>) {
2592-
if self.remaining == 0 {
2586+
if self.v.len() == 0 {
25932587
(0, Some(0))
25942588
} else {
2595-
let (n, rem) = self.remaining.div_rem(&self.chunk_size);
2589+
let (n, rem) = self.v.len().div_rem(&self.chunk_size);
25962590
let n = if rem > 0 { n + 1 } else { n };
25972591
(n, Some(n))
25982592
}
@@ -2602,15 +2596,15 @@ impl<'a, T> Iterator<&'a mut [T]> for MutChunkIter<'a, T> {
26022596
impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutChunkIter<'a, T> {
26032597
#[inline]
26042598
fn next_back(&mut self) -> Option<&'a mut [T]> {
2605-
if self.remaining == 0 {
2599+
if self.v.len() == 0 {
26062600
None
26072601
} else {
2608-
let remainder = self.remaining % self.chunk_size;
2602+
let remainder = self.v.len() % self.chunk_size;
26092603
let sz = if remainder != 0 { remainder } else { self.chunk_size };
26102604
let tmp = util::replace(&mut self.v, &mut []);
2611-
let (head, tail) = tmp.mut_split_at(self.remaining - sz);
2605+
let tmp_len = tmp.len();
2606+
let (head, tail) = tmp.mut_split_at(tmp_len - sz);
26122607
self.v = head;
2613-
self.remaining -= sz;
26142608
Some(tail)
26152609
}
26162610
}

0 commit comments

Comments
 (0)