UnsafeFutureObj impl for PinMut

This commit is contained in:
Josef Reinhard Brandl 2018-07-01 09:28:16 +02:00
parent d8bf222367
commit 042928f0f5
3 changed files with 26 additions and 10 deletions

View file

@ -58,17 +58,16 @@
use core::any::Any;
use core::borrow;
use core::cmp::Ordering;
use core::convert::From;
use core::fmt;
use core::future::Future;
use core::future::{Future, FutureObj, LocalFutureObj, UnsafeFutureObj};
use core::hash::{Hash, Hasher};
use core::iter::FusedIterator;
use core::marker::{Unpin, Unsize};
use core::mem::{self, PinMut};
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
use core::ptr::{self, NonNull, Unique};
use core::future::{FutureObj, LocalFutureObj, UnsafeFutureObj};
use core::task::{Context, Poll};
use core::convert::From;
use raw_vec::RawVec;
use str::from_boxed_utf8_unchecked;
@ -939,14 +938,14 @@ unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox
PinBox::into_raw(self) as *mut ()
}
unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll<T> {
let ptr = task as *mut F;
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(task: *mut ()) {
drop(PinBox::from_raw(task as *mut F))
unsafe fn drop(ptr: *mut ()) {
drop(PinBox::from_raw(ptr as *mut F))
}
}

View file

@ -126,7 +126,7 @@ impl<'a, T> Future for FutureObj<'a, T> {
/// a non-concurrent fashion) with the result of `into_raw` until `drop` is
/// called.
pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
/// Convert a owned instance into a (conceptually owned) void pointer.
/// Convert an owned instance into a (conceptually owned) void pointer.
fn into_raw(self) -> *mut ();
/// Poll the future represented by the given void pointer.
@ -136,7 +136,7 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
/// The trait implementor must guarantee that it is safe to repeatedly call
/// `poll` with the result of `into_raw` until `drop` is called; such calls
/// are not, however, allowed to race with each other or with calls to `drop`.
unsafe fn poll(future: *mut (), cx: &mut Context) -> Poll<T>;
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T>;
/// Drops the future represented by the given void pointer.
///
@ -145,5 +145,5 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
/// The trait implementor must guarantee that it is safe to call this
/// function once per `into_raw` invocation; that call cannot race with
/// other calls to `drop` or `poll`.
unsafe fn drop(future: *mut ());
unsafe fn drop(ptr: *mut ());
}

View file

@ -18,10 +18,12 @@
use clone;
use cmp;
use fmt;
use future::{Future, UnsafeFutureObj};
use hash;
use intrinsics;
use marker::{Copy, PhantomData, Sized, Unpin, Unsize};
use ptr;
use task::{Context, Poll};
use ops::{Deref, DerefMut, CoerceUnsized};
#[stable(feature = "rust1", since = "1.0.0")]
@ -1227,3 +1229,18 @@ impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> for PinM
#[unstable(feature = "pin", issue = "49150")]
impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {}
#[unstable(feature = "futures_api", issue = "50547")]
unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinMut<'a, F> {
fn into_raw(self) -> *mut () {
unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () }
}
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx)
}
unsafe fn drop(ptr: *mut ()) {
drop(PinMut::new_unchecked(&mut *(ptr as *mut F)));
}
}