Skip to content

Commit 232ed5f

Browse files
author
KaiJewson
committed
Avoid once_cell in static wakers
1 parent 1803948 commit 232ed5f

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

futures-task/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Tools for working with tasks.
1313

1414
[features]
1515
default = ["std"]
16-
std = ["alloc", "once_cell"]
16+
std = ["alloc"]
1717
alloc = []
1818

1919
# Unstable features
@@ -23,7 +23,6 @@ unstable = []
2323
cfg-target-has-atomic = []
2424

2525
[dependencies]
26-
once_cell = { version = "1.3.1", default-features = false, features = ["std"], optional = true }
2726

2827
[dev-dependencies]
2928
futures = { path = "../futures" }

futures-task/src/noop_waker.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Utilities for creating zero-cost wakers that don't do anything.
22
3-
use core::task::{RawWaker, RawWakerVTable, Waker};
43
use core::ptr::null;
5-
#[cfg(feature = "std")]
6-
use once_cell::sync::Lazy;
4+
use core::task::{RawWaker, RawWakerVTable, Waker};
75

86
unsafe fn noop_clone(_data: *const ()) -> RawWaker {
97
noop_raw_waker()
@@ -13,7 +11,7 @@ unsafe fn noop(_data: *const ()) {}
1311

1412
const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop);
1513

16-
fn noop_raw_waker() -> RawWaker {
14+
const fn noop_raw_waker() -> RawWaker {
1715
RawWaker::new(null(), &NOOP_WAKER_VTABLE)
1816
}
1917

@@ -29,9 +27,8 @@ fn noop_raw_waker() -> RawWaker {
2927
/// ```
3028
#[inline]
3129
pub fn noop_waker() -> Waker {
32-
unsafe {
33-
Waker::from_raw(noop_raw_waker())
34-
}
30+
// FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const.
31+
unsafe { Waker::from_raw(noop_raw_waker()) }
3532
}
3633

3734
/// Get a static reference to a [`Waker`] which
@@ -45,10 +42,14 @@ pub fn noop_waker() -> Waker {
4542
/// waker.wake_by_ref();
4643
/// ```
4744
#[inline]
48-
#[cfg(feature = "std")]
4945
pub fn noop_waker_ref() -> &'static Waker {
50-
static NOOP_WAKER_INSTANCE: Lazy<Waker> = Lazy::new(noop_waker);
51-
&*NOOP_WAKER_INSTANCE
46+
struct SyncRawWaker(RawWaker);
47+
unsafe impl Sync for SyncRawWaker {}
48+
49+
static NOOP_WAKER_INSTANCE: SyncRawWaker = SyncRawWaker(noop_raw_waker());
50+
51+
// SAFETY: `Waker` is #[repr(transparent)] over its `RawWaker`.
52+
unsafe { &*(&NOOP_WAKER_INSTANCE.0 as *const RawWaker as *const Waker) }
5253
}
5354

5455
#[cfg(test)]

futures-test/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ futures-util = { version = "0.3.12", path = "../futures-util", default-features
1919
futures-executor = { version = "0.3.12", path = "../futures-executor", default-features = false }
2020
futures-sink = { version = "0.3.12", path = "../futures-sink", default-features = false }
2121
pin-utils = { version = "0.1.0", default-features = false }
22-
once_cell = { version = "1.3.1", default-features = false, features = ["std"], optional = true }
2322
pin-project = "1.0.1"
2423

2524
[dev-dependencies]
2625
futures = { path = "../futures", default-features = false, features = ["std", "executor"] }
2726

2827
[features]
2928
default = ["std"]
30-
std = ["futures-core/std", "futures-task/std", "futures-io/std", "futures-util/std", "futures-util/io", "futures-executor/std", "once_cell"]
29+
std = ["futures-core/std", "futures-task/std", "futures-io/std", "futures-util/std", "futures-util/io", "futures-executor/std"]
3130

3231
[package.metadata.docs.rs]
3332
all-features = true

futures-test/src/task/panic_waker.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use futures_core::task::{Waker, RawWaker, RawWakerVTable};
21
use core::ptr::null;
3-
use once_cell::sync::Lazy;
2+
use futures_core::task::{RawWaker, RawWakerVTable, Waker};
43

54
unsafe fn clone_panic_waker(_data: *const ()) -> RawWaker {
65
raw_panic_waker()
@@ -9,19 +8,15 @@ unsafe fn clone_panic_waker(_data: *const ()) -> RawWaker {
98
unsafe fn noop(_data: *const ()) {}
109

1110
unsafe fn wake_panic(_data: *const ()) {
12-
if ! std::thread::panicking() {
11+
if !std::thread::panicking() {
1312
panic!("should not be woken");
1413
}
1514
}
1615

17-
const PANIC_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
18-
clone_panic_waker,
19-
wake_panic,
20-
wake_panic,
21-
noop,
22-
);
16+
const PANIC_WAKER_VTABLE: RawWakerVTable =
17+
RawWakerVTable::new(clone_panic_waker, wake_panic, wake_panic, noop);
2318

24-
fn raw_panic_waker() -> RawWaker {
19+
const fn raw_panic_waker() -> RawWaker {
2520
RawWaker::new(null(), &PANIC_WAKER_VTABLE)
2621
}
2722

@@ -38,6 +33,7 @@ fn raw_panic_waker() -> RawWaker {
3833
/// waker.wake(); // Will panic
3934
/// ```
4035
pub fn panic_waker() -> Waker {
36+
// FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const.
4137
unsafe { Waker::from_raw(raw_panic_waker()) }
4238
}
4339

@@ -54,8 +50,13 @@ pub fn panic_waker() -> Waker {
5450
/// waker.wake_by_ref(); // Will panic
5551
/// ```
5652
pub fn panic_waker_ref() -> &'static Waker {
57-
static PANIC_WAKER_INSTANCE: Lazy<Waker> = Lazy::new(panic_waker);
58-
&*PANIC_WAKER_INSTANCE
53+
struct SyncRawWaker(RawWaker);
54+
unsafe impl Sync for SyncRawWaker {}
55+
56+
static PANIC_WAKER_INSTANCE: SyncRawWaker = SyncRawWaker(raw_panic_waker());
57+
58+
// SAFETY: `Waker` is #[repr(transparent)] over its `RawWaker`.
59+
unsafe { &*(&PANIC_WAKER_INSTANCE.0 as *const RawWaker as *const Waker) }
5960
}
6061

6162
#[cfg(test)]

0 commit comments

Comments
 (0)