simplify implementation of [T]::splitn and friends #41020

This commit is contained in:
Jason Orendorff 2017-04-03 15:27:42 -05:00
parent 2e3f0d8451
commit a45fedfa38

View file

@ -315,8 +315,7 @@ fn splitn<P>(&self, n: usize, pred: P) -> SplitN<T, P>
SplitN {
inner: GenericSplitN {
iter: self.split(pred),
count: n,
invert: false
count: n
}
}
}
@ -327,9 +326,8 @@ fn rsplitn<P>(&self, n: usize, pred: P) -> RSplitN<T, P>
{
RSplitN {
inner: GenericSplitN {
iter: self.split(pred),
count: n,
invert: true
iter: self.rsplit(pred),
count: n
}
}
}
@ -504,8 +502,7 @@ fn splitn_mut<P>(&mut self, n: usize, pred: P) -> SplitNMut<T, P>
SplitNMut {
inner: GenericSplitN {
iter: self.split_mut(pred),
count: n,
invert: false
count: n
}
}
}
@ -516,9 +513,8 @@ fn rsplitn_mut<P>(&mut self, n: usize, pred: P) -> RSplitNMut<T, P> where
{
RSplitNMut {
inner: GenericSplitN {
iter: self.split_mut(pred),
count: n,
invert: true
iter: self.rsplit_mut(pred),
count: n
}
}
}
@ -1881,7 +1877,6 @@ impl<'a, T, P> FusedIterator for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool
struct GenericSplitN<I> {
iter: I,
count: usize,
invert: bool
}
impl<T, I: SplitIter<Item=T>> Iterator for GenericSplitN<I> {
@ -1892,10 +1887,7 @@ fn next(&mut self) -> Option<T> {
match self.count {
0 => None,
1 => { self.count -= 1; self.iter.finish() }
_ => {
self.count -= 1;
if self.invert {self.iter.next_back()} else {self.iter.next()}
}
_ => { self.count -= 1; self.iter.next() }
}
}
@ -1937,7 +1929,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// [slices]: ../../std/primitive.slice.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
inner: GenericSplitN<Split<'a, T, P>>
inner: GenericSplitN<RSplit<'a, T, P>>
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]
@ -1980,7 +1972,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// [slices]: ../../std/primitive.slice.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
inner: GenericSplitN<SplitMut<'a, T, P>>
inner: GenericSplitN<RSplitMut<'a, T, P>>
}
#[stable(feature = "core_impl_debug", since = "1.9.0")]