diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-09-19 17:55:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-19 17:55:19 +0200 |
| commit | ea076a4f9fef9f9d799530aaa6bef70d6fb435eb (patch) | |
| tree | fa667cc9f526440d29c1753bae8ffbdae2ecac0e | |
| parent | 27b1b04065edbb2c33f3790defc9f93fb4cefd02 (diff) | |
| parent | 8e848dc23f7c5e809553dc81072178c34066cc77 (diff) | |
| download | rust-ea076a4f9fef9f9d799530aaa6bef70d6fb435eb.tar.gz rust-ea076a4f9fef9f9d799530aaa6bef70d6fb435eb.zip | |
Rollup merge of #101798 - y86-dev:const_waker, r=lcnr
Make `from_waker`, `waker` and `from_raw` unstably `const` Make - `Context::from_waker` - `Context::waker` - `Waker::from_raw` `const`. Also added a small test.
| -rw-r--r-- | library/alloc/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/core/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/core/src/task/wake.rs | 9 | ||||
| -rw-r--r-- | library/core/tests/lib.rs | 1 | ||||
| -rw-r--r-- | library/core/tests/task.rs | 17 | ||||
| -rw-r--r-- | library/std/src/lib.rs | 1 |
6 files changed, 26 insertions, 4 deletions
diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 8619467c2d9..ce71cd600fc 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -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))] diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 5621d15c1cd..c912b933065 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -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)] diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 60ecc9c0bdb..0cff972df3a 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -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 = "102012")] #[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 = "102012")] #[must_use] #[inline] - pub fn waker(&self) -> &'a Waker { + pub const fn waker(&self) -> &'a Waker { &self.waker } } @@ -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 = "102012")] + pub const unsafe fn from_raw(waker: RawWaker) -> Waker { Waker { waker } } diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 4a0e162bc4a..46f603eaeba 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -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)] diff --git a/library/core/tests/task.rs b/library/core/tests/task.rs index d71fef9e5c8..56be30e9282 100644 --- a/library/core/tests/task.rs +++ b/library/core/tests/task.rs @@ -1,4 +1,4 @@ -use core::task::Poll; +use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; #[test] fn poll_const() { @@ -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(); +} diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index bc4f1b27c2a..07b40f64ac2 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -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)] |
