Fix clippy errors.
parent
7223e08df3
commit
b46fae331b
@ -1,56 +0,0 @@
|
|||||||
pub(crate) trait RemoveTrailing: Iterator + Sized {
|
|
||||||
fn remove_trailing<R: Into<usize>>(self, amount_to_remove: R) -> RemoveTrailingIter<Self>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I> RemoveTrailing for I
|
|
||||||
where
|
|
||||||
I: Iterator,
|
|
||||||
{
|
|
||||||
fn remove_trailing<R: Into<usize>>(self, amount_to_remove: R) -> RemoveTrailingIter<Self> {
|
|
||||||
RemoveTrailingIter {
|
|
||||||
inner: self,
|
|
||||||
buffer: Vec::new(),
|
|
||||||
next_to_pop: 0,
|
|
||||||
amount_to_remove: amount_to_remove.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct RemoveTrailingIter<I: Iterator> {
|
|
||||||
inner: I,
|
|
||||||
buffer: Vec<I::Item>,
|
|
||||||
next_to_pop: usize,
|
|
||||||
amount_to_remove: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I: Iterator> Iterator for RemoveTrailingIter<I> {
|
|
||||||
type Item = I::Item;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
if self.buffer.len() < self.amount_to_remove {
|
|
||||||
self.buffer.reserve_exact(self.amount_to_remove);
|
|
||||||
}
|
|
||||||
while self.buffer.len() < self.amount_to_remove {
|
|
||||||
if let Some(elem) = self.inner.next() {
|
|
||||||
self.buffer.push(elem);
|
|
||||||
} else {
|
|
||||||
// The inner was smaller than amount_to_remove, so never return anything.
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let new_value = self.inner.next();
|
|
||||||
if self.amount_to_remove == 0 {
|
|
||||||
return new_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(new_value) = new_value {
|
|
||||||
let ret = std::mem::replace(&mut self.buffer[self.next_to_pop], new_value);
|
|
||||||
self.next_to_pop = (self.next_to_pop + 1) % self.amount_to_remove;
|
|
||||||
Some(ret)
|
|
||||||
} else {
|
|
||||||
// We have exactly the amount in the buffer than we wanted to cut off, so stop returning values.
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue