/rust/registry/src/index.crates.io-1949cf8c6b5b557f/futures-task-0.3.34/src/noop_waker.rs
Line | Count | Source |
1 | | //! Utilities for creating zero-cost wakers that don't do anything. |
2 | | |
3 | | use core::ptr::null; |
4 | | use core::task::{RawWaker, RawWakerVTable, Waker}; |
5 | | |
6 | | #[inline(always)] |
7 | 0 | unsafe fn noop_clone(_data: *const ()) -> RawWaker { |
8 | 0 | noop_raw_waker() |
9 | 0 | } |
10 | | |
11 | 1.99M | unsafe fn noop(_data: *const ()) {} |
12 | | |
13 | | const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop); |
14 | | |
15 | 1.99M | const fn noop_raw_waker() -> RawWaker { |
16 | 1.99M | RawWaker::new(null(), &NOOP_WAKER_VTABLE) |
17 | 1.99M | } |
18 | | |
19 | | /// Create a new [`Waker`] which does |
20 | | /// nothing when `wake()` is called on it. |
21 | | /// |
22 | | /// # Examples |
23 | | /// |
24 | | /// ``` |
25 | | /// use futures::task::noop_waker; |
26 | | /// let waker = noop_waker(); |
27 | | /// waker.wake(); |
28 | | /// ``` |
29 | | #[inline] |
30 | 1.99M | pub fn noop_waker() -> Waker { |
31 | | // FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const. |
32 | 1.99M | unsafe { Waker::from_raw(noop_raw_waker()) } |
33 | 1.99M | } futures_task::noop_waker::noop_waker Line | Count | Source | 30 | 5.08k | pub fn noop_waker() -> Waker { | 31 | | // FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const. | 32 | 5.08k | unsafe { Waker::from_raw(noop_raw_waker()) } | 33 | 5.08k | } |
futures_task::noop_waker::noop_waker Line | Count | Source | 30 | 1.98M | pub fn noop_waker() -> Waker { | 31 | | // FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const. | 32 | 1.98M | unsafe { Waker::from_raw(noop_raw_waker()) } | 33 | 1.98M | } |
Unexecuted instantiation: futures_task::noop_waker::noop_waker |
34 | | |
35 | | /// Get a static reference to a [`Waker`] which |
36 | | /// does nothing when `wake()` is called on it. |
37 | | /// |
38 | | /// # Examples |
39 | | /// |
40 | | /// ``` |
41 | | /// use futures::task::noop_waker_ref; |
42 | | /// let waker = noop_waker_ref(); |
43 | | /// waker.wake_by_ref(); |
44 | | /// ``` |
45 | | #[inline] |
46 | 0 | pub fn noop_waker_ref() -> &'static Waker { |
47 | | struct SyncRawWaker(RawWaker); |
48 | | unsafe impl Sync for SyncRawWaker {} |
49 | | |
50 | | static NOOP_WAKER_INSTANCE: SyncRawWaker = SyncRawWaker(noop_raw_waker()); |
51 | | |
52 | | // SAFETY: `Waker` is #[repr(transparent)] over its `RawWaker`. |
53 | 0 | unsafe { &*(&NOOP_WAKER_INSTANCE.0 as *const RawWaker as *const Waker) } |
54 | 0 | } |
55 | | |
56 | | #[cfg(test)] |
57 | | mod tests { |
58 | | #[test] |
59 | | #[cfg(feature = "std")] |
60 | | fn issue_2091_cross_thread_segfault() { |
61 | | let waker = std::thread::spawn(super::noop_waker_ref).join().unwrap(); |
62 | | waker.wake_by_ref(); |
63 | | } |
64 | | } |