Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make from_waker, waker and from_raw unstably const #101798

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#![feature(core_intrinsics)]
#![feature(const_eval_select)]
#![feature(const_pin)]
#![feature(const_waker)]
#![feature(cstr_from_bytes_until_nul)]
#![feature(dispatch_from_dyn)]
#![cfg_attr(not(bootstrap), feature(error_generic_member_access))]
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
#![feature(const_default_impls)]
#![feature(const_unicode_case_lookup)]
#![feature(const_unsafecell_get_mut)]
#![feature(const_waker)]
#![feature(core_panic)]
#![feature(duration_consts_float)]
#![feature(maybe_uninit_uninit_array)]
Expand Down
9 changes: 6 additions & 3 deletions library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,19 @@ pub struct Context<'a> {
impl<'a> Context<'a> {
/// Create a new `Context` from a [`&Waker`](Waker).
#[stable(feature = "futures_api", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_waker", issue = "none")]
y86-dev marked this conversation as resolved.
Show resolved Hide resolved
#[must_use]
#[inline]
pub fn from_waker(waker: &'a Waker) -> Self {
pub const fn from_waker(waker: &'a Waker) -> Self {
Context { waker, _marker: PhantomData }
}

/// Returns a reference to the [`Waker`] for the current task.
#[stable(feature = "futures_api", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_waker", issue = "none")]
#[must_use]
#[inline]
pub fn waker(&self) -> &'a Waker {
pub const fn waker(&self) -> &'a Waker {
&self.waker
}
}
Expand Down Expand Up @@ -311,7 +313,8 @@ impl Waker {
#[inline]
#[must_use]
#[stable(feature = "futures_api", since = "1.36.0")]
pub unsafe fn from_raw(waker: RawWaker) -> Waker {
#[rustc_const_unstable(feature = "const_waker", issue = "none")]
pub const unsafe fn from_raw(waker: RawWaker) -> Waker {
Waker { waker }
}

Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#![feature(iterator_try_reduce)]
#![feature(const_mut_refs)]
#![feature(const_pin)]
#![feature(const_waker)]
#![feature(never_type)]
#![feature(unwrap_infallible)]
#![feature(pointer_byte_offsets)]
Expand Down
17 changes: 16 additions & 1 deletion library/core/tests/task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::task::Poll;
use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};

#[test]
fn poll_const() {
Expand All @@ -12,3 +12,18 @@ fn poll_const() {
const IS_PENDING: bool = POLL.is_pending();
assert!(IS_PENDING);
}

#[test]
fn waker_const() {
const VOID_TABLE: RawWakerVTable = RawWakerVTable::new(|_| VOID_WAKER, |_| {}, |_| {}, |_| {});

const VOID_WAKER: RawWaker = RawWaker::new(&(), &VOID_TABLE);

static WAKER: Waker = unsafe { Waker::from_raw(VOID_WAKER) };

static CONTEXT: Context<'static> = Context::from_waker(&WAKER);

static WAKER_REF: &'static Waker = CONTEXT.waker();

WAKER_REF.wake_by_ref();
}
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@
#![feature(strict_provenance)]
#![feature(maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(const_waker)]
//
// Library features (alloc):
#![feature(alloc_layout_extra)]
Expand Down