Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions library/alloc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,44 @@ impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
}
}

/// Converts a closure into a [`Waker`].
///
/// The closure gets called every time the waker is woken.
///
/// # Examples
///
/// ```
/// #![feature(waker_fn)]
/// use std::task::waker_fn;
///
/// let waker = waker_fn(|| println!("woken"));
///
/// waker.wake_by_ref(); // Prints "woken".
/// waker.wake(); // Prints "woken".
/// ```
#[cfg(target_has_atomic = "ptr")]
#[unstable(feature = "waker_fn", issue = "149580")]
pub fn waker_fn<F: Fn() + Send + Sync + 'static>(f: F) -> Waker {
struct WakeFn<F> {
f: F,
}

impl<F> Wake for WakeFn<F>
where
F: Fn(),
{
fn wake(self: Arc<Self>) {
(self.f)()
}

fn wake_by_ref(self: &Arc<Self>) {
(self.f)()
}
}

Waker::from(Arc::new(WakeFn { f }))
}

// NB: This private function for constructing a RawWaker is used, rather than
// inlining this into the `From<Arc<W>> for RawWaker` impl, to ensure that
// the safety of `From<Arc<W>> for Waker` does not depend on the correct
Expand Down Expand Up @@ -306,6 +344,44 @@ impl<W: LocalWake + 'static> From<Rc<W>> for RawWaker {
}
}

/// Converts a closure into a [`LocalWaker`].
///
/// The closure gets called every time the local waker is woken.
///
/// # Examples
///
/// ```
/// #![feature(local_waker)]
/// #![feature(waker_fn)]
/// use std::task::local_waker_fn;
///
/// let waker = local_waker_fn(|| println!("woken"));
///
/// waker.wake_by_ref(); // Prints "woken".
/// waker.wake(); // Prints "woken".
/// ```
#[unstable(feature = "waker_fn", issue = "149580")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we can have two feature gateway waker_fn & local_waker. Or as long as the returned value is unstable, this method cannot be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or in case we stablized this feature before local_waker, we change the feature flag to local_waker at that time.

pub fn local_waker_fn<F: Fn() + Send + Sync + 'static>(f: F) -> LocalWaker {
struct LocalWakeFn<F> {
f: F,
}

impl<F> LocalWake for LocalWakeFn<F>
where
F: Fn(),
{
fn wake(self: Rc<Self>) {
(self.f)()
}

fn wake_by_ref(self: &Rc<Self>) {
(self.f)()
}
}

LocalWaker::from(Rc::new(LocalWakeFn { f }))
}

// NB: This private function for constructing a RawWaker is used, rather than
// inlining this into the `From<Rc<W>> for RawWaker` impl, to ensure that
// the safety of `From<Rc<W>> for Waker` does not depend on the correct
Expand Down
Loading