Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the async Iterator::filter method #5

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/iter/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// An async iterator that filters the elements of `iter` with `predicate`.
///
/// This `struct` is created by the [`filter`] method on [`Iterator`]. See its
/// documentation for more.
///
/// [`filter`]: crate::Iterator::filter
/// [`Iterator`]: crate::Iterator
#[derive(Debug)]
pub struct Filter<I, P>
where
I: crate::Iterator,
P: async FnMut(&I::Item) -> bool,
{
iter: I,
predicate: P,
}

impl<I, P> crate::Iterator for Filter<I, P>
where
I: crate::Iterator,
P: async FnMut(&I::Item) -> bool,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

{
type Item = I::Item;

async fn next(&mut self) -> Option<Self::Item> {
loop {
let item = self.iter.next().await?;
if (self.predicate)(&item).await {
return Some(item);
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
}

impl<I, P> Filter<I, P>
where
I: crate::Iterator,
P: async FnMut(&I::Item) -> bool,
{
pub(crate) fn new(iter: I, predicate: P) -> Self {
Self { iter, predicate }
}
}
13 changes: 13 additions & 0 deletions src/iter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod filter;
mod lend;
mod lend_mut;
mod map;

pub use filter::Filter;
pub use lend::Lend;
pub use lend_mut::LendMut;
pub use map::Map;
Expand Down Expand Up @@ -33,6 +35,17 @@ pub trait Iterator {
Map::new(self, f)
}

/// Creates an iterator which uses a closure to determine if an element should be yielded.
#[must_use = "iterators do nothing unless iterated over"]
fn filter<P>(self, predicate: P) -> Filter<Self, P>
where
P: async FnMut(&Self::Item) -> bool,
// P: FnMut(&Self::Item) -> bool,
Self: Sized,
{
Filter::new(self, predicate)
}

/// Transforms an iterator into a collection.
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
async fn collect<B: FromIterator<Self::Item>>(self) -> B
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
//! the traits, use `async_trait`.
//!
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(incomplete_features)]
#![feature(return_position_impl_trait_in_trait)]
#![feature(async_fn_in_trait)]
#![allow(async_fn_in_trait)]
#![feature(async_closure)]
#![forbid(unsafe_code, future_incompatible)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs)]
Expand Down
Loading