Guest User

Untitled

a guest
May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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<F: Future> Future for Option<F> {
  18. type Output = Option<F::Output>;
  19. fn poll_pinned(self: PinMut<Self>) -> Self::Output {
  20. match unsafe { PinMut::get_mut(self) } {
  21. Some(x) => {
  22. Some(unsafe { PinMut::new_unchecked(x).poll_pinned() })
  23. }
  24. None => None,
  25. }
  26. }
  27. }
  28.  
  29. impl<F: Future + ?Sized + Unpin> MoveFuture for Box<F> {
  30. type Output = F::Output;
  31. fn poll(&mut self) -> Self::Output {
  32. F::poll_pinned(PinMut::new(self))
  33. }
  34. }
  35.  
  36. impl<'a, F: Future + ?Sized> MoveFuture for PinMut<'a, F> {
  37. type Output = F::Output;
  38. fn poll(&mut self) -> Self::Output {
  39. F::poll_pinned(self.reborrow())
  40. }
  41. }
  42.  
  43. impl<F: Future + ?Sized> MoveFuture for PinBox<F> {
  44. type Output = F::Output;
  45. fn poll(&mut self) -> Self::Output {
  46. F::poll_pinned(self.as_pin_mut())
  47. }
  48. }
  49.  
  50. impl<F: MoveFuture> Future for F {
  51. type Output = <F as MoveFuture>::Output;
  52. fn poll_pinned(mut self: PinMut<Self>) -> Self::Output {
  53. (*self).poll()
  54. }
  55. }
  56.  
  57. trait IntoMoveFuture: Future + Unpin {
  58. fn into_move_future(self) -> MoveFutureWrap<Self>
  59. where Self: Sized
  60. {
  61. MoveFutureWrap(self)
  62. }
  63. }
  64. impl<T: Future + Unpin> IntoMoveFuture for T {}
  65.  
  66. struct MoveFutureWrap<T>(pub T);
  67.  
  68. impl<T: Future + Unpin> MoveFuture for MoveFutureWrap<T> {
  69. type Output = T::Output;
  70. fn poll(&mut self) -> Self::Output {
  71. PinMut::new(&mut self.0).poll_pinned()
  72. }
  73. }
  74.  
  75. struct Foo;
  76. impl Future for Foo {
  77. type Output = u8;
  78. fn poll_pinned(self: PinMut<Self>) -> Self::Output { 5 }
  79. }
  80.  
  81. fn main() {
  82. Foo.into_move_future().poll();
  83. }
Add Comment
Please, Sign In to add comment