X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fwakers.rs;h=602c2ee04b7d0fb2b85554e8d5566aef8f3fd6d6;hb=c1825672ed27839e95bb42bf8e75c9068da585d8;hp=0385adc0c5b0c01e7239fa6ad24cc8b887f61d65;hpb=efcb5e02dc5bbdb92e917234336ce37a204e1d57;p=rust-lightning diff --git a/lightning/src/util/wakers.rs b/lightning/src/util/wakers.rs index 0385adc0..602c2ee0 100644 --- a/lightning/src/util/wakers.rs +++ b/lightning/src/util/wakers.rs @@ -15,11 +15,13 @@ use alloc::sync::Arc; use core::mem; -use crate::sync::{Condvar, Mutex}; +use crate::sync::Mutex; use crate::prelude::*; -#[cfg(any(test, feature = "std"))] +#[cfg(feature = "std")] +use crate::sync::Condvar; +#[cfg(feature = "std")] use std::time::Duration; use core::future::Future as StdFuture; @@ -160,6 +162,7 @@ impl Future { } /// Waits until this [`Future`] completes. + #[cfg(feature = "std")] pub fn wait(self) { Sleeper::from_single_future(self).wait(); } @@ -167,10 +170,19 @@ impl Future { /// Waits until this [`Future`] completes or the given amount of time has elapsed. /// /// Returns true if the [`Future`] completed, false if the time elapsed. - #[cfg(any(test, feature = "std"))] + #[cfg(feature = "std")] pub fn wait_timeout(self, max_wait: Duration) -> bool { Sleeper::from_single_future(self).wait_timeout(max_wait) } + + #[cfg(test)] + pub fn poll_is_complete(&self) -> bool { + let mut state = self.state.lock().unwrap(); + if state.complete { + state.callbacks_made = true; + true + } else { false } + } } use core::task::Waker; @@ -198,10 +210,12 @@ impl<'a> StdFuture for Future { /// A struct which can be used to select across many [`Future`]s at once without relying on a full /// async context. +#[cfg(feature = "std")] pub struct Sleeper { notifiers: Vec>>, } +#[cfg(feature = "std")] impl Sleeper { /// Constructs a new sleeper from one future, allowing blocking on it. pub fn from_single_future(future: Future) -> Self { @@ -252,7 +266,6 @@ impl Sleeper { /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the /// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time /// elapsed. - #[cfg(any(test, feature = "std"))] pub fn wait_timeout(&self, max_wait: Duration) -> bool { let (cv, notified_fut_mtx) = self.setup_wait(); let notified_fut = @@ -478,6 +491,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_dropped_future_doesnt_count() { // Tests that if a Future gets drop'd before it is poll()ed `Ready` it doesn't count as // having been woken, leaving the notify-required flag set. @@ -569,6 +583,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_multi_future_sleep() { // Tests the `Sleeper` with multiple futures. let notifier_a = Notifier::new();