Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(async_closure)]
- use std::future::Future;
- use std::pin::Pin;
- use std::task::{Context, Poll};
- struct ReadyFuture;
- impl Future for ReadyFuture {
- type Output = ();
- fn poll(self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
- Poll::Ready(())
- }
- }
- trait FnTrait<A> {
- type Fut: Future<Output = ()>;
- fn call(self, arg: &mut A) -> Self::Fut;
- }
- impl<A, Fut, F> FnTrait<A> for F
- where
- Fut: Future<Output = ()>,
- F: FnOnce(&mut A) -> Fut,
- {
- type Fut = Fut;
- fn call(self, arg: &mut A) -> Fut {
- self(arg)
- }
- }
- #[derive(Debug)]
- struct MyArg;
- async fn my_fn<'a, /*Fut,*/ F>(closure: F)
- where
- F: FnTrait<MyArg>,
- //Fut: Future<Output = ()>,
- //F: FnOnce(&mut MyArg) -> Fut,
- {
- let mut my_arg = MyArg;
- closure.call(&mut my_arg).await;
- println!("from my_fn: {:?}", my_arg);
- }
- fn main() {
- my_fn(async move |my_arg: &mut MyArg| {
- ReadyFuture.await;
- println!("from closure: {:?}", my_arg);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement