|
7 | 7 | #![deny(private_intra_doc_links)]
|
8 | 8 |
|
9 | 9 | #![deny(missing_docs)]
|
10 |
| -#![deny(unsafe_code)] |
| 10 | +#![cfg_attr(not(feature = "futures"), deny(unsafe_code))] |
11 | 11 |
|
12 | 12 | #![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
13 | 13 |
|
@@ -52,8 +52,6 @@ use std::thread::{self, JoinHandle};
|
52 | 52 | #[cfg(feature = "std")]
|
53 | 53 | use std::time::Instant;
|
54 | 54 |
|
55 |
| -#[cfg(feature = "futures")] |
56 |
| -use futures_util::task; |
57 | 55 | #[cfg(not(feature = "std"))]
|
58 | 56 | use alloc::vec::Vec;
|
59 | 57 |
|
@@ -385,38 +383,45 @@ macro_rules! define_run_body {
|
385 | 383 | }
|
386 | 384 |
|
387 | 385 | #[cfg(feature = "futures")]
|
388 |
| -use core::future::Future; |
389 |
| -#[cfg(feature = "futures")] |
390 |
| -use core::task::Poll; |
391 |
| -#[cfg(feature = "futures")] |
392 |
| -use core::pin::Pin; |
393 |
| -#[cfg(feature = "futures")] |
394 |
| -use core::marker::Unpin; |
395 |
| -#[cfg(feature = "futures")] |
396 |
| -struct Selecter<A: Future<Output=()> + Unpin, B: Future<Output=bool> + Unpin> { |
397 |
| - a: A, |
398 |
| - b: B, |
399 |
| -} |
400 |
| -#[cfg(feature = "futures")] |
401 |
| -enum SelecterOutput { |
402 |
| - A, B(bool), |
403 |
| -} |
| 386 | +pub(crate) mod futures_util { |
| 387 | + use core::future::Future; |
| 388 | + use core::task::{Poll, Waker, RawWaker, RawWakerVTable}; |
| 389 | + use core::pin::Pin; |
| 390 | + use core::marker::Unpin; |
| 391 | + pub(crate) struct Selecter<A: Future<Output=()> + Unpin, B: Future<Output=bool> + Unpin> { |
| 392 | + pub a: A, |
| 393 | + pub b: B, |
| 394 | + } |
| 395 | + pub(crate) enum SelecterOutput { |
| 396 | + A, B(bool), |
| 397 | + } |
404 | 398 |
|
405 |
| -#[cfg(feature = "futures")] |
406 |
| -impl<A: Future<Output=()> + Unpin, B: Future<Output=bool> + Unpin> Future for Selecter<A, B> { |
407 |
| - type Output = SelecterOutput; |
408 |
| - fn poll(mut self: Pin<&mut Self>, ctx: &mut core::task::Context<'_>) -> Poll<SelecterOutput> { |
409 |
| - match Pin::new(&mut self.a).poll(ctx) { |
410 |
| - Poll::Ready(()) => { return Poll::Ready(SelecterOutput::A); }, |
411 |
| - Poll::Pending => {}, |
412 |
| - } |
413 |
| - match Pin::new(&mut self.b).poll(ctx) { |
414 |
| - Poll::Ready(res) => { return Poll::Ready(SelecterOutput::B(res)); }, |
415 |
| - Poll::Pending => {}, |
| 399 | + impl<A: Future<Output=()> + Unpin, B: Future<Output=bool> + Unpin> Future for Selecter<A, B> { |
| 400 | + type Output = SelecterOutput; |
| 401 | + fn poll(mut self: Pin<&mut Self>, ctx: &mut core::task::Context<'_>) -> Poll<SelecterOutput> { |
| 402 | + match Pin::new(&mut self.a).poll(ctx) { |
| 403 | + Poll::Ready(()) => { return Poll::Ready(SelecterOutput::A); }, |
| 404 | + Poll::Pending => {}, |
| 405 | + } |
| 406 | + match Pin::new(&mut self.b).poll(ctx) { |
| 407 | + Poll::Ready(res) => { return Poll::Ready(SelecterOutput::B(res)); }, |
| 408 | + Poll::Pending => {}, |
| 409 | + } |
| 410 | + Poll::Pending |
416 | 411 | }
|
417 |
| - Poll::Pending |
418 | 412 | }
|
| 413 | + |
| 414 | + fn dummy_waker_clone(_: *const ()) -> RawWaker { RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE) } |
| 415 | + fn dummy_waker_action(_: *const ()) { } |
| 416 | + |
| 417 | + const DUMMY_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new( |
| 418 | + dummy_waker_clone, dummy_waker_action, dummy_waker_action, dummy_waker_action); |
| 419 | + pub(crate) fn dummy_waker() -> Waker { unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE)) } } |
419 | 420 | }
|
| 421 | +#[cfg(feature = "futures")] |
| 422 | +use futures_util::{Selecter, SelecterOutput, dummy_waker}; |
| 423 | +#[cfg(feature = "futures")] |
| 424 | +use core::task; |
420 | 425 |
|
421 | 426 | /// Processes background events in a future.
|
422 | 427 | ///
|
@@ -517,7 +522,7 @@ where
|
517 | 522 | }
|
518 | 523 | }, |t| sleeper(Duration::from_secs(t)),
|
519 | 524 | |fut: &mut SleepFuture, _| {
|
520 |
| - let mut waker = task::noop_waker(); |
| 525 | + let mut waker = dummy_waker(); |
521 | 526 | let mut ctx = task::Context::from_waker(&mut waker);
|
522 | 527 | core::pin::Pin::new(fut).poll(&mut ctx).is_ready()
|
523 | 528 | })
|
|
0 commit comments