Remove waker_fn
diff --git a/benches/waker_fn.rs b/benches/waker_fn.rs
deleted file mode 100644
index 1de1b97..0000000
--- a/benches/waker_fn.rs
+++ /dev/null
@@ -1,89 +0,0 @@
-#![feature(test)]
-
-extern crate test;
-
-use std::cell::RefCell;
-use std::future::Future;
-use std::pin::Pin;
-use std::task::{Context, Poll, Waker};
-
-use crossbeam::sync::Parker;
-use test::Bencher;
-
-/// Runs a future to completion on the current thread.
-fn block_on<F: Future>(future: F) -> F::Output {
- // Pin the future on the stack.
- futures::pin_mut!(future);
-
- thread_local! {
- // Parker and waker associated with the current thread.
- static CACHE: RefCell<(Parker, Waker)> = {
- let parker = Parker::new();
- let unparker = parker.unparker().clone();
- let waker = async_task::waker_fn(move || unparker.unpark());
- RefCell::new((parker, waker))
- };
- }
-
- CACHE.with(|cache| {
- // Panic if `block_on()` is called recursively.
- let (parker, waker) = &mut *cache.try_borrow_mut().ok().expect("recursive `block_on`");
-
- // Create the task context.
- let cx = &mut Context::from_waker(&waker);
-
- // Keep polling the future until completion.
- loop {
- match future.as_mut().poll(cx) {
- Poll::Ready(output) => return output,
- Poll::Pending => parker.park(),
- }
- }
- })
-}
-
-#[bench]
-fn custom_block_on_0_yields(b: &mut Bencher) {
- b.iter(|| block_on(Yields(0)));
-}
-
-#[bench]
-fn custom_block_on_10_yields(b: &mut Bencher) {
- b.iter(|| block_on(Yields(10)));
-}
-
-#[bench]
-fn custom_block_on_50_yields(b: &mut Bencher) {
- b.iter(|| block_on(Yields(50)));
-}
-
-#[bench]
-fn futures_block_on_0_yields(b: &mut Bencher) {
- b.iter(|| futures::executor::block_on(Yields(0)));
-}
-
-#[bench]
-fn futures_block_on_10_yields(b: &mut Bencher) {
- b.iter(|| futures::executor::block_on(Yields(10)));
-}
-
-#[bench]
-fn futures_block_on_50_yields(b: &mut Bencher) {
- b.iter(|| futures::executor::block_on(Yields(50)));
-}
-
-struct Yields(u32);
-
-impl Future for Yields {
- type Output = ();
-
- fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
- if self.0 == 0 {
- Poll::Ready(())
- } else {
- self.0 -= 1;
- cx.waker().wake_by_ref();
- Poll::Pending
- }
- }
-}
diff --git a/src/lib.rs b/src/lib.rs
index 94afa65..9f7ba85 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -90,24 +90,8 @@
//! The layout of a task is equivalent to 4 `usize`s followed by the schedule function, and then by
//! a union of the future and its output.
//!
-//! # Waking
-//!
-//! The handy [`waker_fn`] constructor converts any function into a [`Waker`]. Every time it is
-//! woken, the function gets called:
-//!
-//! ```
-//! let waker = async_task::waker_fn(|| println!("Wake!"));
-//!
-//! // Prints "Wake!" twice.
-//! waker.wake_by_ref();
-//! waker.wake_by_ref();
-//! ```
-//!
-//! This is useful for implementing single-future executors like [`block_on`].
-//!
//! [`spawn`]: fn.spawn.html
//! [`spawn_local`]: fn.spawn_local.html
-//! [`waker_fn`]: fn.waker_fn.html
//! [`Task`]: struct.Task.html
//! [`JoinHandle`]: struct.JoinHandle.html
//! [`Waker`]: https://doc.rust-lang.org/std/task/struct.Waker.html
@@ -126,11 +110,9 @@
mod state;
mod task;
mod utils;
-mod waker_fn;
pub use crate::join_handle::JoinHandle;
pub use crate::task::{spawn, Task};
-pub use crate::waker_fn::waker_fn;
#[cfg(feature = "std")]
pub use crate::task::spawn_local;
diff --git a/src/task.rs b/src/task.rs
index 6fb4cad..0507a90 100644
--- a/src/task.rs
+++ b/src/task.rs
@@ -61,9 +61,7 @@
RawTask::<F, R, S>::allocate(future, schedule)
};
- let task = Task {
- raw_task,
- };
+ let task = Task { raw_task };
let handle = JoinHandle {
raw_task,
_marker: PhantomData,
@@ -177,9 +175,7 @@
RawTask::<_, R, S>::allocate(future, schedule)
};
- let task = Task {
- raw_task,
- };
+ let task = Task { raw_task };
let handle = JoinHandle {
raw_task,
_marker: PhantomData,
diff --git a/src/waker_fn.rs b/src/waker_fn.rs
deleted file mode 100644
index 37105f1..0000000
--- a/src/waker_fn.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-use alloc::sync::Arc;
-use core::mem::{self, ManuallyDrop};
-use core::task::{RawWaker, RawWakerVTable, Waker};
-
-/// Creates a waker from a wake function.
-///
-/// The function gets called every time the waker is woken.
-pub fn waker_fn<F: Fn() + Send + Sync + 'static>(f: F) -> Waker {
- let raw = Arc::into_raw(Arc::new(f)) as *const ();
- let vtable = &Helper::<F>::VTABLE;
- unsafe { Waker::from_raw(RawWaker::new(raw, vtable)) }
-}
-
-struct Helper<F>(F);
-
-impl<F: Fn() + Send + Sync + 'static> Helper<F> {
- const VTABLE: RawWakerVTable = RawWakerVTable::new(
- Self::clone_waker,
- Self::wake,
- Self::wake_by_ref,
- Self::drop_waker,
- );
-
- unsafe fn clone_waker(ptr: *const ()) -> RawWaker {
- let arc = ManuallyDrop::new(Arc::from_raw(ptr as *const F));
- mem::forget(arc.clone());
- RawWaker::new(ptr, &Self::VTABLE)
- }
-
- unsafe fn wake(ptr: *const ()) {
- let arc = Arc::from_raw(ptr as *const F);
- (arc)();
- }
-
- unsafe fn wake_by_ref(ptr: *const ()) {
- let arc = ManuallyDrop::new(Arc::from_raw(ptr as *const F));
- (arc)();
- }
-
- unsafe fn drop_waker(ptr: *const ()) {
- drop(Arc::from_raw(ptr as *const F));
- }
-}