Skip to content

Commit b907d96

Browse files
committed
Auto merge of #51532 - MajorBreakfast:task-future, r=cramertj
Improve core::task::TaskObj - Rename `UnsafePoll` to `UnsafeTask` to avoid confusion with `Poll` - Rename `TaskObj::from_poll_task()` to `TaskObj::new()` - Rename `TaskObj`'s `poll` and `drop` fields to `poll_fn` and `drop_fn` - Implement `Future` for `TaskObj`. Reason: It's a custom trait object for a future, so it should implement future - Remove `unsafe impl Sync` for `TaskObj`. I don't think we need it. Was this safe? `UnsafeTask` only requires to implement `Send` @cramertj @aturon
2 parents e12733b + 2177378 commit b907d96

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/liballoc/boxed.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use core::marker::{Unpin, Unsize};
6666
use core::mem::{self, PinMut};
6767
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
6868
use core::ptr::{self, NonNull, Unique};
69-
use core::task::{Context, Poll, UnsafePoll, TaskObj};
69+
use core::task::{Context, Poll, UnsafeTask, TaskObj};
7070
use core::convert::From;
7171

7272
use raw_vec::RawVec;
@@ -933,7 +933,7 @@ impl<'a, F: ?Sized + Future> Future for PinBox<F> {
933933
}
934934

935935
#[unstable(feature = "futures_api", issue = "50547")]
936-
unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafePoll for PinBox<F> {
936+
unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafeTask for PinBox<F> {
937937
fn into_raw(self) -> *mut () {
938938
PinBox::into_raw(self) as *mut ()
939939
}
@@ -952,13 +952,13 @@ unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafePoll for PinBox<F> {
952952
#[unstable(feature = "futures_api", issue = "50547")]
953953
impl<F: Future<Output = ()> + Send + 'static> From<PinBox<F>> for TaskObj {
954954
fn from(boxed: PinBox<F>) -> Self {
955-
TaskObj::from_poll_task(boxed)
955+
TaskObj::new(boxed)
956956
}
957957
}
958958

959959
#[unstable(feature = "futures_api", issue = "50547")]
960960
impl<F: Future<Output = ()> + Send + 'static> From<Box<F>> for TaskObj {
961961
fn from(boxed: Box<F>) -> Self {
962-
TaskObj::from_poll_task(PinBox::from(boxed))
962+
TaskObj::new(PinBox::from(boxed))
963963
}
964964
}

src/libcore/task.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
1717
use fmt;
1818
use ptr::NonNull;
19+
use future::Future;
20+
use mem::PinMut;
1921

2022
/// Indicates whether a value is available or if the current task has been
2123
/// scheduled to receive a wakeup instead.
@@ -455,8 +457,8 @@ pub trait Executor {
455457
/// `Box<Future<Output = ()> + Send>`.
456458
pub struct TaskObj {
457459
ptr: *mut (),
458-
poll: unsafe fn(*mut (), &mut Context) -> Poll<()>,
459-
drop: unsafe fn(*mut ()),
460+
poll_fn: unsafe fn(*mut (), &mut Context) -> Poll<()>,
461+
drop_fn: unsafe fn(*mut ()),
460462
}
461463

462464
impl fmt::Debug for TaskObj {
@@ -467,7 +469,6 @@ impl fmt::Debug for TaskObj {
467469
}
468470

469471
unsafe impl Send for TaskObj {}
470-
unsafe impl Sync for TaskObj {}
471472

472473
/// A custom implementation of a task trait object for `TaskObj`, providing
473474
/// a hand-rolled vtable.
@@ -478,7 +479,7 @@ unsafe impl Sync for TaskObj {}
478479
/// The implementor must guarantee that it is safe to call `poll` repeatedly (in
479480
/// a non-concurrent fashion) with the result of `into_raw` until `drop` is
480481
/// called.
481-
pub unsafe trait UnsafePoll: Send + 'static {
482+
pub unsafe trait UnsafeTask: Send + 'static {
482483
/// Convert a owned instance into a (conceptually owned) void pointer.
483484
fn into_raw(self) -> *mut ();
484485

@@ -504,30 +505,30 @@ pub unsafe trait UnsafePoll: Send + 'static {
504505
impl TaskObj {
505506
/// Create a `TaskObj` from a custom trait object representation.
506507
#[inline]
507-
pub fn from_poll_task<T: UnsafePoll>(t: T) -> TaskObj {
508+
pub fn new<T: UnsafeTask>(t: T) -> TaskObj {
508509
TaskObj {
509510
ptr: t.into_raw(),
510-
poll: T::poll,
511-
drop: T::drop,
511+
poll_fn: T::poll,
512+
drop_fn: T::drop,
512513
}
513514
}
515+
}
516+
517+
impl Future for TaskObj {
518+
type Output = ();
514519

515-
/// Poll the task.
516-
///
517-
/// The semantics here are identical to that for futures, but unlike
518-
/// futures only an `&mut self` reference is needed here.
519520
#[inline]
520-
pub fn poll_task(&mut self, cx: &mut Context) -> Poll<()> {
521+
fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<()> {
521522
unsafe {
522-
(self.poll)(self.ptr, cx)
523+
(self.poll_fn)(self.ptr, cx)
523524
}
524525
}
525526
}
526527

527528
impl Drop for TaskObj {
528529
fn drop(&mut self) {
529530
unsafe {
530-
(self.drop)(self.ptr)
531+
(self.drop_fn)(self.ptr)
531532
}
532533
}
533534
}

0 commit comments

Comments
 (0)