Skip to content

Commit 042928f

Browse files
UnsafeFutureObj impl for PinMut
1 parent d8bf222 commit 042928f

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/liballoc/boxed.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@
5858
use core::any::Any;
5959
use core::borrow;
6060
use core::cmp::Ordering;
61+
use core::convert::From;
6162
use core::fmt;
62-
use core::future::Future;
63+
use core::future::{Future, FutureObj, LocalFutureObj, UnsafeFutureObj};
6364
use core::hash::{Hash, Hasher};
6465
use core::iter::FusedIterator;
6566
use core::marker::{Unpin, Unsize};
6667
use core::mem::{self, PinMut};
6768
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
6869
use core::ptr::{self, NonNull, Unique};
69-
use core::future::{FutureObj, LocalFutureObj, UnsafeFutureObj};
7070
use core::task::{Context, Poll};
71-
use core::convert::From;
7271

7372
use raw_vec::RawVec;
7473
use str::from_boxed_utf8_unchecked;
@@ -939,14 +938,14 @@ unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox
939938
PinBox::into_raw(self) as *mut ()
940939
}
941940

942-
unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll<T> {
943-
let ptr = task as *mut F;
941+
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
942+
let ptr = ptr as *mut F;
944943
let pin: PinMut<F> = PinMut::new_unchecked(&mut *ptr);
945944
pin.poll(cx)
946945
}
947946

948-
unsafe fn drop(task: *mut ()) {
949-
drop(PinBox::from_raw(task as *mut F))
947+
unsafe fn drop(ptr: *mut ()) {
948+
drop(PinBox::from_raw(ptr as *mut F))
950949
}
951950
}
952951

src/libcore/future/future_obj.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a, T> Future for FutureObj<'a, T> {
126126
/// a non-concurrent fashion) with the result of `into_raw` until `drop` is
127127
/// called.
128128
pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
129-
/// Convert a owned instance into a (conceptually owned) void pointer.
129+
/// Convert an owned instance into a (conceptually owned) void pointer.
130130
fn into_raw(self) -> *mut ();
131131

132132
/// Poll the future represented by the given void pointer.
@@ -136,7 +136,7 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
136136
/// The trait implementor must guarantee that it is safe to repeatedly call
137137
/// `poll` with the result of `into_raw` until `drop` is called; such calls
138138
/// are not, however, allowed to race with each other or with calls to `drop`.
139-
unsafe fn poll(future: *mut (), cx: &mut Context) -> Poll<T>;
139+
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T>;
140140

141141
/// Drops the future represented by the given void pointer.
142142
///
@@ -145,5 +145,5 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
145145
/// The trait implementor must guarantee that it is safe to call this
146146
/// function once per `into_raw` invocation; that call cannot race with
147147
/// other calls to `drop` or `poll`.
148-
unsafe fn drop(future: *mut ());
148+
unsafe fn drop(ptr: *mut ());
149149
}

src/libcore/mem.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
use clone;
1919
use cmp;
2020
use fmt;
21+
use future::{Future, UnsafeFutureObj};
2122
use hash;
2223
use intrinsics;
2324
use marker::{Copy, PhantomData, Sized, Unpin, Unsize};
2425
use ptr;
26+
use task::{Context, Poll};
2527
use ops::{Deref, DerefMut, CoerceUnsized};
2628

2729
#[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
12271229

12281230
#[unstable(feature = "pin", issue = "49150")]
12291231
impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {}
1232+
1233+
#[unstable(feature = "futures_api", issue = "50547")]
1234+
unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinMut<'a, F> {
1235+
fn into_raw(self) -> *mut () {
1236+
unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () }
1237+
}
1238+
1239+
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
1240+
PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx)
1241+
}
1242+
1243+
unsafe fn drop(ptr: *mut ()) {
1244+
drop(PinMut::new_unchecked(&mut *(ptr as *mut F)));
1245+
}
1246+
}

0 commit comments

Comments
 (0)