Guest User

Untitled

a guest
May 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #![feature(pin, arbitrary_self_types, specialization)]
  2.  
  3. use std::marker::Unpin;
  4. use std::mem::PinMut;
  5. use std::boxed::PinBox;
  6.  
  7. trait MoveFuture: Unpin {
  8. type Output;
  9. fn poll(&mut self) -> Self::Output;
  10. }
  11.  
  12. trait Future {
  13. type Output;
  14. fn poll_pinned(self: PinMut<Self>) -> Self::Output;
  15. }
  16.  
  17. impl<'a, F: Future> MoveFuture for PinMut<'a, F> {
  18. type Output = F::Output;
  19. fn poll(&mut self) -> Self::Output {
  20. F::poll_pinned(self.reborrow())
  21. }
  22. }
  23.  
  24. impl<F: Future> MoveFuture for PinBox<F> {
  25. type Output = F::Output;
  26. fn poll(&mut self) -> Self::Output {
  27. F::poll_pinned(self.as_pin_mut())
  28. }
  29. }
  30.  
  31. impl<F: MoveFuture> Future for F {
  32. type Output = <F as MoveFuture>::Output;
  33. fn poll_pinned(mut self: PinMut<Self>) -> Self::Output {
  34. (*self).poll()
  35. }
  36. }
  37.  
  38. trait IntoMoveFuture: Future + Unpin {
  39. fn into_move_future(self) -> MoveFutureWrap<Self>
  40. where Self: Sized
  41. {
  42. MoveFutureWrap(self)
  43. }
  44. }
  45. impl<T: Future + Unpin> IntoMoveFuture for T {}
  46.  
  47. struct MoveFutureWrap<T>(pub T);
  48.  
  49. impl<T: Future + Unpin> MoveFuture for MoveFutureWrap<T> {
  50. type Output = T::Output;
  51. fn poll(&mut self) -> Self::Output {
  52. PinMut::new(&mut self.0).poll_pinned()
  53. }
  54. }
  55.  
  56. struct Foo;
  57. impl Future for Foo {
  58. type Output = u8;
  59. fn poll_pinned(self: PinMut<Self>) -> Self::Output { 5 }
  60. }
  61.  
  62. fn main() {
  63. Foo.into_move_future().poll();
  64. }
Add Comment
Please, Sign In to add comment