From e85f3f6fbbef6077adc287dbee66864e54b8ef84 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Tue, 15 Aug 2023 22:06:08 +0200 Subject: [PATCH] Fuse the iterator in `LazyBuffer` --- src/combinations.rs | 4 ++-- src/lazy_buffer.rs | 16 ++++------------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/combinations.rs b/src/combinations.rs index 68a59c5e4..18adfc70e 100644 --- a/src/combinations.rs +++ b/src/combinations.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::iter::FusedIterator; +use std::iter::{Fuse, FusedIterator}; use super::lazy_buffer::LazyBuffer; use alloc::vec::Vec; @@ -54,7 +54,7 @@ impl Combinations { /// Returns a reference to the source iterator. #[inline] - pub(crate) fn src(&self) -> &I { &self.pool.it } + pub(crate) fn src(&self) -> &Fuse { &self.pool.it } /// Resets this `Combinations` back to an initial state for combinations of length /// `k` over the same pool data source. If `k` is larger than the current length diff --git a/src/lazy_buffer.rs b/src/lazy_buffer.rs index ca24062aa..65cc8e2cd 100644 --- a/src/lazy_buffer.rs +++ b/src/lazy_buffer.rs @@ -1,10 +1,10 @@ +use std::iter::Fuse; use std::ops::Index; use alloc::vec::Vec; #[derive(Debug, Clone)] pub struct LazyBuffer { - pub it: I, - done: bool, + pub it: Fuse, buffer: Vec, } @@ -14,8 +14,7 @@ where { pub fn new(it: I) -> LazyBuffer { LazyBuffer { - it, - done: false, + it: it.fuse(), buffer: Vec::new(), } } @@ -25,26 +24,19 @@ where } pub fn get_next(&mut self) -> bool { - if self.done { - return false; - } if let Some(x) = self.it.next() { self.buffer.push(x); true } else { - self.done = true; false } } pub fn prefill(&mut self, len: usize) { let buffer_len = self.buffer.len(); - - if !self.done && len > buffer_len { + if len > buffer_len { let delta = len - buffer_len; - self.buffer.extend(self.it.by_ref().take(delta)); - self.done = self.buffer.len() < len; } } }