Sha256: 41246601dab77f69bf09257afc3321031a5a31a7eda51787029870eda9922356

Contents?: true

Size: 1.61 KB

Versions: 27

Compression:

Stored size: 1.61 KB

Contents

//! Utilities for creating zero-cost wakers that don't do anything.

use core::ptr::null;
use core::task::{RawWaker, RawWakerVTable, Waker};

unsafe fn noop_clone(_data: *const ()) -> RawWaker {
    noop_raw_waker()
}

unsafe fn noop(_data: *const ()) {}

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

const fn noop_raw_waker() -> RawWaker {
    RawWaker::new(null(), &NOOP_WAKER_VTABLE)
}

/// Create a new [`Waker`] which does
/// nothing when `wake()` is called on it.
///
/// # Examples
///
/// ```
/// use futures::task::noop_waker;
/// let waker = noop_waker();
/// waker.wake();
/// ```
#[inline]
pub fn noop_waker() -> Waker {
    // FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const.
    unsafe { Waker::from_raw(noop_raw_waker()) }
}

/// Get a static reference to a [`Waker`] which
/// does nothing when `wake()` is called on it.
///
/// # Examples
///
/// ```
/// use futures::task::noop_waker_ref;
/// let waker = noop_waker_ref();
/// waker.wake_by_ref();
/// ```
#[inline]
pub fn noop_waker_ref() -> &'static Waker {
    struct SyncRawWaker(RawWaker);
    unsafe impl Sync for SyncRawWaker {}

    static NOOP_WAKER_INSTANCE: SyncRawWaker = SyncRawWaker(noop_raw_waker());

    // SAFETY: `Waker` is #[repr(transparent)] over its `RawWaker`.
    unsafe { &*(&NOOP_WAKER_INSTANCE.0 as *const RawWaker as *const Waker) }
}

#[cfg(test)]
mod tests {
    #[test]
    #[cfg(feature = "std")]
    fn issue_2091_cross_thread_segfault() {
        let waker = std::thread::spawn(super::noop_waker_ref).join().unwrap();
        waker.wake_by_ref();
    }
}

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/futures-task-0.3.31/src/noop_waker.rs
wasmtime-29.0.0 ./ext/cargo-vendor/futures-task-0.3.31/src/noop_waker.rs
wasmtime-28.0.0 ./ext/cargo-vendor/futures-task-0.3.31/src/noop_waker.rs
wasmtime-27.0.0 ./ext/cargo-vendor/futures-task-0.3.31/src/noop_waker.rs
wasmtime-26.0.0 ./ext/cargo-vendor/futures-task-0.3.31/src/noop_waker.rs
wasmtime-25.0.2 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-task-0.3.30/src/noop_waker.rs