Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. use std::future::Future;
  2.  
  3. pub trait X: Clone + Send + Unpin + Sync {
  4. fn x(&self) -> Box<dyn Future<Output = ()> + Unpin + Send>;
  5. }
  6.  
  7. pub fn c<'a, X1: X + 'a>(x: &X1) -> Box<dyn Future<Output = ()> + Unpin + Send + 'a> {
  8. let x = x.clone();
  9. let res = x.x().then(move |_| x.x());
  10. Box::new(res)
  11. }
  12.  
  13. impl X for &() {
  14. fn x(&self) -> Box<dyn Future<Output = ()> + Unpin + Send> {
  15. Box::new(Box::pin(async {}))
  16. }
  17. }
  18.  
  19. fn main() {
  20. let _ = {
  21. let val = ();
  22. let reference = &val;
  23. c(&reference)
  24. };
  25. }
  26.  
  27. /// ----
  28.  
  29. use std::pin::Pin;
  30.  
  31. trait FutureExt: Future {
  32. fn then<'a, F: Future + Send>(self, f: impl FnOnce(Self::Output) -> F + Send + 'a) -> Pin<Box<dyn Future<Output = F::Output> + Send + 'a>> where Self: Send + Sized + 'a, Self::Output: Send {
  33. Box::pin(async { f(self.await).await })
  34. }
  35. }
  36.  
  37. impl<T: Future + ?Sized> FutureExt for T {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement