|
2 | 2 |
|
3 | 3 | use crate::fmt;
|
4 | 4 | use crate::marker::{PhantomData, Unpin};
|
| 5 | +use crate::ptr; |
5 | 6 |
|
6 | 7 | /// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
|
7 | 8 | /// which provides customized wakeup behavior.
|
@@ -277,6 +278,45 @@ impl Waker {
|
277 | 278 | Waker { waker }
|
278 | 279 | }
|
279 | 280 |
|
| 281 | + /// Creates a new `Waker` that does nothing when `wake` is called. |
| 282 | + /// |
| 283 | + /// This is mostly useful for writing tests that need a [`Context`] to poll |
| 284 | + /// some futures, but are not expecting those futures to wake the waker or |
| 285 | + /// do not need to do anything specific if it happens. |
| 286 | + /// |
| 287 | + /// # Examples |
| 288 | + /// |
| 289 | + /// ``` |
| 290 | + /// #![feature(noop_waker)] |
| 291 | + /// |
| 292 | + /// use std::future::Future; |
| 293 | + /// use std::task; |
| 294 | + /// |
| 295 | + /// let waker = task::Waker::noop(); |
| 296 | + /// let mut cx = task::Context::from_waker(&waker); |
| 297 | + /// |
| 298 | + /// let mut future = Box::pin(async { 10 }); |
| 299 | + /// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10)); |
| 300 | + /// ``` |
| 301 | + #[inline] |
| 302 | + #[must_use] |
| 303 | + #[unstable(feature = "noop_waker", issue = "none")] |
| 304 | + pub const fn noop() -> Waker { |
| 305 | + const VTABLE: RawWakerVTable = RawWakerVTable::new( |
| 306 | + // Cloning just returns a new no-op raw waker |
| 307 | + |_| RAW, |
| 308 | + // `wake` does nothing |
| 309 | + |_| {}, |
| 310 | + // `wake_by_ref` does nothing |
| 311 | + |_| {}, |
| 312 | + // Dropping does nothing as we don't allocate anything |
| 313 | + |_| {}, |
| 314 | + ); |
| 315 | + const RAW: RawWaker = RawWaker::new(ptr::null(), &VTABLE); |
| 316 | + |
| 317 | + Waker { waker: RAW } |
| 318 | + } |
| 319 | + |
280 | 320 | /// Get a reference to the underlying [`RawWaker`].
|
281 | 321 | #[inline]
|
282 | 322 | #[must_use]
|
|
0 commit comments