Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #![feature(async_closure)]
  2.  
  3. use std::future::Future;
  4. use std::pin::Pin;
  5. use std::task::{Context, Poll};
  6.  
  7. struct ReadyFuture;
  8.  
  9. impl Future for ReadyFuture {
  10. type Output = ();
  11. fn poll(self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
  12. Poll::Ready(())
  13. }
  14. }
  15.  
  16. trait FnTrait<A> {
  17. type Fut: Future<Output = ()>;
  18. fn call(self, arg: &mut A) -> Self::Fut;
  19. }
  20.  
  21. impl<A, Fut, F> FnTrait<A> for F
  22. where
  23. Fut: Future<Output = ()>,
  24. F: FnOnce(&mut A) -> Fut,
  25. {
  26. type Fut = Fut;
  27. fn call(self, arg: &mut A) -> Fut {
  28. self(arg)
  29. }
  30. }
  31.  
  32. #[derive(Debug)]
  33. struct MyArg;
  34.  
  35. async fn my_fn<'a, /*Fut,*/ F>(closure: F)
  36. where
  37. F: FnTrait<MyArg>,
  38. //Fut: Future<Output = ()>,
  39. //F: FnOnce(&mut MyArg) -> Fut,
  40. {
  41. let mut my_arg = MyArg;
  42. closure.call(&mut my_arg).await;
  43. println!("from my_fn: {:?}", my_arg);
  44. }
  45.  
  46. fn main() {
  47. my_fn(async move |my_arg: &mut MyArg| {
  48. ReadyFuture.await;
  49. println!("from closure: {:?}", my_arg);
  50. });
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement