/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-1.9.0/src/common/task.rs
Line | Count | Source |
1 | | use std::task::{Context, Poll}; |
2 | | #[cfg(feature = "client")] |
3 | | use std::task::{RawWaker, RawWakerVTable, Waker}; |
4 | | |
5 | | /// A function to help "yield" a future, such that it is re-scheduled immediately. |
6 | | /// |
7 | | /// Useful for spin counts, so a future doesn't hog too much time. |
8 | 0 | pub(crate) fn yield_now(cx: &mut Context<'_>) -> Poll<std::convert::Infallible> { |
9 | 0 | cx.waker().wake_by_ref(); |
10 | 0 | Poll::Pending |
11 | 0 | } |
12 | | |
13 | | // TODO: replace with `std::task::Waker::noop()` once MSRV >= 1.85 |
14 | | #[cfg(feature = "client")] |
15 | 0 | fn noop_waker() -> Waker { |
16 | | const NOOP_RAW_WAKER: RawWaker = RawWaker::new(std::ptr::null(), &NOOP_VTABLE); |
17 | | const NOOP_VTABLE: RawWakerVTable = RawWakerVTable::new( |
18 | | // `clone` returns the same noop waker again |
19 | | |_: *const ()| NOOP_RAW_WAKER, |
20 | | // `wake`, `wake_by_ref`, and `drop` do nothing |
21 | 0 | |_: *const ()| {}, |
22 | 0 | |_: *const ()| {}, |
23 | 0 | |_: *const ()| {}, |
24 | | ); |
25 | | |
26 | | // SAFETY: all functions in the vtable are safe to call, and Waker's safety does not require |
27 | | // them to actually do anything. |
28 | 0 | unsafe { Waker::from_raw(NOOP_RAW_WAKER) } |
29 | 0 | } |
30 | | |
31 | | /// Poll the future once and return `Some` if it is ready, else `None`. |
32 | | /// |
33 | | /// If the future wasn't ready, it future likely can't be driven to completion any more: the polling |
34 | | /// uses a no-op waker, so knowledge of what the pending future was waiting for is lost. |
35 | | #[cfg(feature = "client")] |
36 | 0 | pub(crate) fn now_or_never<F: std::future::Future>(fut: F) -> Option<F::Output> { |
37 | 0 | let waker = noop_waker(); |
38 | 0 | let mut cx = Context::from_waker(&waker); |
39 | | // TODO: replace with std::pin::pin! once MSRV >= 1.68 |
40 | 0 | tokio::pin!(fut); |
41 | 0 | match fut.poll(&mut cx) { |
42 | 0 | Poll::Ready(res) => Some(res), |
43 | 0 | Poll::Pending => None, |
44 | | } |
45 | 0 | } Unexecuted instantiation: hyper::common::task::now_or_never::<<tokio::sync::mpsc::unbounded::UnboundedReceiver<hyper::client::dispatch::Envelope<http::request::Request<reqwest::async_impl::body::Body>, http::response::Response<hyper::body::incoming::Incoming>>>>::recv::{closure#0}>Unexecuted instantiation: hyper::common::task::now_or_never::<_> |