Skip to content

Commit

Permalink
simplify implementation of [T]::splitn and friends #41020
Browse files Browse the repository at this point in the history
  • Loading branch information
jorendorff committed Apr 4, 2017
1 parent 2e3f0d8 commit a45fedf
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ impl<T> SliceExt for [T] {
SplitN {
inner: GenericSplitN {
iter: self.split(pred),
count: n,
invert: false
count: n
}
}
}
Expand All @@ -327,9 +326,8 @@ impl<T> SliceExt for [T] {
{
RSplitN {
inner: GenericSplitN {
iter: self.split(pred),
count: n,
invert: true
iter: self.rsplit(pred),
count: n
}
}
}
Expand Down Expand Up @@ -504,8 +502,7 @@ impl<T> SliceExt for [T] {
SplitNMut {
inner: GenericSplitN {
iter: self.split_mut(pred),
count: n,
invert: false
count: n
}
}
}
Expand All @@ -516,9 +513,8 @@ impl<T> SliceExt for [T] {
{
RSplitNMut {
inner: GenericSplitN {
iter: self.split_mut(pred),
count: n,
invert: true
iter: self.rsplit_mut(pred),
count: n
}
}
}
Expand Down Expand Up @@ -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> {
Expand All @@ -1892,10 +1887,7 @@ impl<T, I: SplitIter<Item=T>> Iterator for GenericSplitN<I> {
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() }
}
}

Expand Down Expand Up @@ -1937,7 +1929,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitN<'a, T, P> where P: FnMut(&
/// [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")]
Expand Down Expand Up @@ -1980,7 +1972,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitNMut<'a, T, P> where P: FnMu
/// [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")]
Expand Down

0 comments on commit a45fedf

Please sign in to comment.