Skip to content

Commit

Permalink
Implement UnsafeFutureObj for &mut Future
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorBreakfast committed Jul 2, 2018
1 parent 5fde8b9 commit ae40894
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/liballoc/boxed.rs
Expand Up @@ -933,7 +933,9 @@ impl<F: ?Sized + Future> Future for PinBox<F> {
}

#[unstable(feature = "futures_api", issue = "50547")]
unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox<F> {
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinBox<F>
where F: Future<Output = T> + 'a
{
fn into_raw(self) -> *mut () {
PinBox::into_raw(self) as *mut ()
}
Expand Down
16 changes: 15 additions & 1 deletion src/libcore/future/future_obj.rs
Expand Up @@ -14,7 +14,7 @@

use fmt;
use future::Future;
use marker::PhantomData;
use marker::{PhantomData, Unpin};
use mem::PinMut;
use task::{Context, Poll};

Expand Down Expand Up @@ -163,3 +163,17 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
/// other calls to `drop` or `poll`.
unsafe fn drop(ptr: *mut ());
}

unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for &'a mut F
where F: Future<Output = T> + Unpin + 'a
{
fn into_raw(self) -> *mut () {
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 ()) {}
}
4 changes: 3 additions & 1 deletion src/libcore/mem.rs
Expand Up @@ -1231,7 +1231,9 @@ impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> for PinM
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> {
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinMut<'a, F>
where F: Future<Output = T> + 'a
{
fn into_raw(self) -> *mut () {
unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () }
}
Expand Down

0 comments on commit ae40894

Please sign in to comment.