Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(pin, arbitrary_self_types, specialization)]
- use std::marker::Unpin;
- use std::mem::PinMut;
- use std::boxed::PinBox;
- trait MoveFuture: Unpin {
- type Output;
- fn poll(&mut self) -> Self::Output;
- }
- trait Future {
- type Output;
- fn poll_pinned(self: PinMut<Self>) -> Self::Output;
- }
- impl<F: Future> Future for Option<F> {
- type Output = Option<F::Output>;
- fn poll_pinned(self: PinMut<Self>) -> Self::Output {
- match unsafe { PinMut::get_mut(self) } {
- Some(x) => {
- Some(unsafe { PinMut::new_unchecked(x).poll_pinned() })
- }
- None => None,
- }
- }
- }
- impl<F: Future + ?Sized + Unpin> MoveFuture for Box<F> {
- type Output = F::Output;
- fn poll(&mut self) -> Self::Output {
- F::poll_pinned(PinMut::new(self))
- }
- }
- impl<'a, F: Future + ?Sized> MoveFuture for PinMut<'a, F> {
- type Output = F::Output;
- fn poll(&mut self) -> Self::Output {
- F::poll_pinned(self.reborrow())
- }
- }
- impl<F: Future + ?Sized> MoveFuture for PinBox<F> {
- type Output = F::Output;
- fn poll(&mut self) -> Self::Output {
- F::poll_pinned(self.as_pin_mut())
- }
- }
- impl<F: MoveFuture> Future for F {
- type Output = <F as MoveFuture>::Output;
- fn poll_pinned(mut self: PinMut<Self>) -> Self::Output {
- (*self).poll()
- }
- }
- trait IntoMoveFuture: Future + Unpin {
- fn into_move_future(self) -> MoveFutureWrap<Self>
- where Self: Sized
- {
- MoveFutureWrap(self)
- }
- }
- impl<T: Future + Unpin> IntoMoveFuture for T {}
- struct MoveFutureWrap<T>(pub T);
- impl<T: Future + Unpin> MoveFuture for MoveFutureWrap<T> {
- type Output = T::Output;
- fn poll(&mut self) -> Self::Output {
- PinMut::new(&mut self.0).poll_pinned()
- }
- }
- struct Foo;
- impl Future for Foo {
- type Output = u8;
- fn poll_pinned(self: PinMut<Self>) -> Self::Output { 5 }
- }
- fn main() {
- Foo.into_move_future().poll();
- }
Add Comment
Please, Sign In to add comment