Skip to main content

async_itertools/
lib.rs

1//! `itertools` for `Stream`s. Work-in-progress.
2//!
3//! Made with [`futures-lite`](<https://docs.rs/futures-lite/2.6.1>) in mind rather than
4//! [`futures-util`](<https://docs.rs/futures-util/0.3.31>).
5//!
6//! Large parts of the code are taken from [the original crate](<https://docs.rs/itertools/0.14.0>).
7//! Their licenses:
8//!
9//! - [MIT](<https://github.com/rust-itertools/itertools/blob/6bd5053fca990a8c7a3de6429faa53fef81de41d/LICENSE-MIT>)
10//! - [Apache-2.0](<https://github.com/rust-itertools/itertools/blob/6bd5053fca990a8c7a3de6429faa53fef81de41d/LICENSE-APACHE>)
11
12#![no_std]
13
14use futures_lite::Stream;
15
16pub use self::{
17    features::{
18        coalesce::{Coalesce, Dedup, DedupBy, coalesce, dedup, dedup_by},
19        zip_longest::zip_longest,
20    },
21    types::either_or_both::EitherOrBoth,
22};
23
24mod features;
25mod internal;
26mod types;
27
28/// `Itertools` extension trait but for [`Stream`]s instead of [`Iterator`]s.
29pub trait AsyncItertools: Stream {
30    /// Yields pairs, [`EitherOrBoth::Both`], of items from both streams, until at least one runs
31    /// out, then either [`EitherOrBoth::Left`] (if right ends) or [`EitherOrBoth::Right`] (if left
32    /// ends).
33    fn zip_longest<U>(self, other: U) -> ZipLongest<Self, U>
34    where
35        Self: Sized,
36        U: Stream,
37    {
38        zip_longest(self, other)
39    }
40
41    fn coalesce<F>(self, f: F) -> Coalesce<Self, F>
42    where
43        Self: Sized,
44        F: FnMut(Self::Item, Self::Item) -> Result<Self::Item, (Self::Item, Self::Item)>,
45    {
46        coalesce(self, f)
47    }
48
49    fn dedup_by<F>(self, f: F) -> DedupBy<Self, F>
50    where
51        Self: Sized,
52        F: FnMut(&Self::Item, &Self::Item) -> bool,
53    {
54        dedup_by(self, f)
55    }
56
57    fn dedup(self) -> Dedup<Self>
58    where
59        Self: Sized,
60        Self::Item: PartialEq,
61    {
62        dedup(self)
63    }
64}
65
66impl<T: ?Sized + Stream> AsyncItertools for T {}
67
68/// Stream for the [`AsyncItertools::zip_longest()`] method.
69pub type ZipLongest<L, R> = self::features::zip_longest::ZipLongest<L, R>;