1 use crate::runtime::time::TimeSource; 2 use std::fmt; 3 4 /// Handle to time driver instance. 5 pub(crate) struct Handle { 6 pub(super) time_source: TimeSource, 7 pub(super) inner: super::Inner, 8 } 9 10 impl Handle { 11 /// Returns the time source associated with this handle. time_source(&self) -> &TimeSource12 pub(crate) fn time_source(&self) -> &TimeSource { 13 &self.time_source 14 } 15 16 /// Checks whether the driver has been shutdown. is_shutdown(&self) -> bool17 pub(super) fn is_shutdown(&self) -> bool { 18 self.inner.is_shutdown() 19 } 20 21 /// Track that the driver is being unparked unpark(&self)22 pub(crate) fn unpark(&self) { 23 #[cfg(feature = "test-util")] 24 self.inner 25 .did_wake 26 .store(true, std::sync::atomic::Ordering::SeqCst); 27 } 28 } 29 30 cfg_not_rt! { 31 impl Handle { 32 /// Tries to get a handle to the current timer. 33 /// 34 /// # Panics 35 /// 36 /// This function panics if there is no current timer set. 37 /// 38 /// It can be triggered when [`Builder::enable_time`] or 39 /// [`Builder::enable_all`] are not included in the builder. 40 /// 41 /// It can also panic whenever a timer is created outside of a 42 /// Tokio runtime. That is why `rt.block_on(sleep(...))` will panic, 43 /// since the function is executed outside of the runtime. 44 /// Whereas `rt.block_on(async {sleep(...).await})` doesn't panic. 45 /// And this is because wrapping the function on an async makes it lazy, 46 /// and so gets executed inside the runtime successfully without 47 /// panicking. 48 /// 49 /// [`Builder::enable_time`]: crate::runtime::Builder::enable_time 50 /// [`Builder::enable_all`]: crate::runtime::Builder::enable_all 51 #[track_caller] 52 pub(crate) fn current() -> Self { 53 panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR) 54 } 55 } 56 } 57 58 impl fmt::Debug for Handle { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 60 write!(f, "Handle") 61 } 62 } 63