From e666c2bd0742cbf88ff9fa26cfc194099a139589 Mon Sep 17 00:00:00 2001 From: Josef Reinhard Brandl Date: Mon, 2 Jul 2018 19:21:32 +0200 Subject: Implemented `UnsafeFutureObj` on `Box` --- src/liballoc/boxed.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index fabeaa1c144..fb16bdf0ab4 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -932,6 +932,25 @@ impl Future for PinBox { } } +#[unstable(feature = "futures_api", issue = "50547")] +unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box + where F: Future + 'a +{ + fn into_raw(self) -> *mut () { + Box::into_raw(self) as *mut () + } + + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll { + let ptr = ptr as *mut F; + let pin: PinMut = PinMut::new_unchecked(&mut *ptr); + pin.poll(cx) + } + + unsafe fn drop(ptr: *mut ()) { + drop(Box::from_raw(ptr as *mut F)) + } +} + #[unstable(feature = "futures_api", issue = "50547")] unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinBox where F: Future + 'a @@ -961,7 +980,7 @@ impl<'a, F: Future + Send + 'a> From> for FutureObj<'a, ( #[unstable(feature = "futures_api", issue = "50547")] impl<'a, F: Future + Send + 'a> From> for FutureObj<'a, ()> { fn from(boxed: Box) -> Self { - FutureObj::new(PinBox::from(boxed)) + FutureObj::new(boxed) } } @@ -975,6 +994,6 @@ impl<'a, F: Future + 'a> From> for LocalFutureObj<'a, ()> #[unstable(feature = "futures_api", issue = "50547")] impl<'a, F: Future + 'a> From> for LocalFutureObj<'a, ()> { fn from(boxed: Box) -> Self { - LocalFutureObj::new(PinBox::from(boxed)) + LocalFutureObj::new(boxed) } } -- cgit 1.4.1-3-g733a5