Skip to content

Commit

Permalink
Made from_waker, waker, from_raw const
Browse files Browse the repository at this point in the history
  • Loading branch information
y86-dev committed Sep 14, 2022
1 parent c97922d commit 9a78fab
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
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")]
#[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

0 comments on commit 9a78fab

Please sign in to comment.