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

add the async Iterator::filter method #5

wants to merge 3 commits into from

Conversation

yoshuawuyts
Copy link
Owner

This adds an async closure-based filter method. It compiles on the latest nightly, but R-A doesn't like it one bit yet so marking this as a draft PR. This does show that the we can correctly implement async closure-based APIs using an AFIT-based async Iterator trait.

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.

🎉

Copy link

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

It would be useful if there were a test committed that actually shows how this is more powerful than StreamExt::filter. Specifically, maybe something like:

fn test_filter_fn(x: impl crate::Iterator<Item = String>) -> impl crate::Iterator<Item = String> {
    async fn do_a_query(_: &str) -> bool { true }
    x.filter(async |url: &String| do_a_query(url).await)
}

Note that async closures don't infer their signatures yet, so you need to write |url: &String| in the signature, or else you'll get inference errors.

@compiler-errors
Copy link

but R-A doesn't like it one bit yet so marking this as a draft PR.

rust-lang/rust-analyzer#16588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants