Implemented UnsafeFutureObj on Box

This commit is contained in:
Josef Reinhard Brandl
2018-07-02 19:21:32 +02:00
parent ae408947de
commit e666c2bd07
+21 -2
View File
@@ -932,6 +932,25 @@ fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
}
}
#[unstable(feature = "futures_api", issue = "50547")]
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
where F: Future<Output = T> + 'a
{
fn into_raw(self) -> *mut () {
Box::into_raw(self) as *mut ()
}
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
let ptr = ptr as *mut F;
let pin: PinMut<F> = 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<F>
where F: Future<Output = T> + 'a
@@ -961,7 +980,7 @@ fn from(boxed: PinBox<F>) -> Self {
#[unstable(feature = "futures_api", issue = "50547")]
impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> {
fn from(boxed: Box<F>) -> Self {
FutureObj::new(PinBox::from(boxed))
FutureObj::new(boxed)
}
}
@@ -975,6 +994,6 @@ fn from(boxed: PinBox<F>) -> Self {
#[unstable(feature = "futures_api", issue = "50547")]
impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
fn from(boxed: Box<F>) -> Self {
LocalFutureObj::new(PinBox::from(boxed))
LocalFutureObj::new(boxed)
}
}