summaryrefslogtreecommitdiffstats
path: root/vendor/itertools/src/intersperse.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/itertools/src/intersperse.rs
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/itertools/src/intersperse.rs')
-rw-r--r--vendor/itertools/src/intersperse.rs94
1 files changed, 59 insertions, 35 deletions
diff --git a/vendor/itertools/src/intersperse.rs b/vendor/itertools/src/intersperse.rs
index 10a3a5389..5f4f7938a 100644
--- a/vendor/itertools/src/intersperse.rs
+++ b/vendor/itertools/src/intersperse.rs
@@ -1,5 +1,5 @@
-use std::iter::{Fuse, FusedIterator};
use super::size_hint;
+use std::iter::{Fuse, FusedIterator};
pub trait IntersperseElement<Item> {
fn generate(&mut self) -> Item;
@@ -26,12 +26,13 @@ pub type Intersperse<I> = IntersperseWith<I, IntersperseElementSimple<<I as Iter
/// Create a new Intersperse iterator
pub fn intersperse<I>(iter: I, elt: I::Item) -> Intersperse<I>
- where I: Iterator,
+where
+ I: Iterator,
{
intersperse_with(iter, IntersperseElementSimple(elt))
}
-impl<Item, F: FnMut()->Item> IntersperseElement<Item> for F {
+impl<Item, F: FnMut() -> Item> IntersperseElement<Item> for F {
fn generate(&mut self) -> Item {
self()
}
@@ -48,71 +49,94 @@ impl<Item, F: FnMut()->Item> IntersperseElement<Item> for F {
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[derive(Clone, Debug)]
pub struct IntersperseWith<I, ElemF>
- where I: Iterator,
+where
+ I: Iterator,
{
element: ElemF,
iter: Fuse<I>,
- peek: Option<I::Item>,
+ /// `peek` is None while no item have been taken out of `iter` (at definition).
+ /// Then `peek` will alternatively be `Some(None)` and `Some(Some(item))`,
+ /// where `None` indicates it's time to generate from `element` (unless `iter` is empty).
+ peek: Option<Option<I::Item>>,
}
/// Create a new `IntersperseWith` iterator
pub fn intersperse_with<I, ElemF>(iter: I, elt: ElemF) -> IntersperseWith<I, ElemF>
- where I: Iterator,
+where
+ I: Iterator,
{
- let mut iter = iter.fuse();
IntersperseWith {
- peek: iter.next(),
- iter,
+ peek: None,
+ iter: iter.fuse(),
element: elt,
}
}
impl<I, ElemF> Iterator for IntersperseWith<I, ElemF>
- where I: Iterator,
- ElemF: IntersperseElement<I::Item>
+where
+ I: Iterator,
+ ElemF: IntersperseElement<I::Item>,
{
type Item = I::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
- if self.peek.is_some() {
- self.peek.take()
- } else {
- self.peek = self.iter.next();
- if self.peek.is_some() {
- Some(self.element.generate())
- } else {
- None
+ let Self {
+ element,
+ iter,
+ peek,
+ } = self;
+ match peek {
+ Some(item @ Some(_)) => item.take(),
+ Some(None) => match iter.next() {
+ new @ Some(_) => {
+ *peek = Some(new);
+ Some(element.generate())
+ }
+ None => None,
+ },
+ None => {
+ *peek = Some(None);
+ iter.next()
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
- // 2 * SH + { 1 or 0 }
- let has_peek = self.peek.is_some() as usize;
- let sh = self.iter.size_hint();
- size_hint::add_scalar(size_hint::add(sh, sh), has_peek)
+ let mut sh = self.iter.size_hint();
+ sh = size_hint::add(sh, sh);
+ match self.peek {
+ Some(Some(_)) => size_hint::add_scalar(sh, 1),
+ Some(None) => sh,
+ None => size_hint::sub_scalar(sh, 1),
+ }
}
- fn fold<B, F>(mut self, init: B, mut f: F) -> B where
- Self: Sized, F: FnMut(B, Self::Item) -> B,
+ fn fold<B, F>(self, init: B, mut f: F) -> B
+ where
+ Self: Sized,
+ F: FnMut(B, Self::Item) -> B,
{
+ let Self {
+ mut element,
+ mut iter,
+ peek,
+ } = self;
let mut accum = init;
- if let Some(x) = self.peek.take() {
+ if let Some(x) = peek.unwrap_or_else(|| iter.next()) {
accum = f(accum, x);
}
- let element = &mut self.element;
-
- self.iter.fold(accum,
- |accum, x| {
- let accum = f(accum, element.generate());
- f(accum, x)
+ iter.fold(accum, |accum, x| {
+ let accum = f(accum, element.generate());
+ f(accum, x)
})
}
}
impl<I, ElemF> FusedIterator for IntersperseWith<I, ElemF>
- where I: Iterator,
- ElemF: IntersperseElement<I::Item>
-{}
+where
+ I: Iterator,
+ ElemF: IntersperseElement<I::Item>,
+{
+}