rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.29k stars 607 forks source link

Added future::on_poll, future::on_poll_pending and future::on_poll_ready #2842

Open kulst opened 3 months ago

kulst commented 3 months ago

This pull request adds the possibility to create wrapped futures that additionally invoke a function when they get polled. The function has mutable access to the Context and the returning Poll of the wrapped future.

I can imagine several possible use cases for that:

The futures are created by three free functions. Examples are given in the code comments.

Comments would be really appreciated as

taiki-e commented 3 months ago

Thanks for the PR. We recently received another PR similar to this one (#2840 by @coolreader18). (Although this PR is 3 futures with a post-called function in different conditions, and #2840 is a future with a pre-polled future, so it's not like either one covers the other.)

To be honest, I don't want to accept all of these kinds of similar and slightly different APIs, none of them seem that common, and they seem easy to implement locally.

If any particular one is widely used, I would prefer to accept it, but if not, I would tend to close both for now.