Skip to content

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