use std::{
any::TypeId,
future::Future,
pin::Pin,
time::{Duration, Instant},
};
/// A timer which provides timer-like functions. pubtrait Timer { /// Return a future that resolves in `duration` time. fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>>;
/// Return a future that resolves at `deadline`. fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>>;
/// Return an `Instant` representing the current time. /// /// The default implementation returns [`Instant::now()`]. fn now(&self) -> Instant {
Instant::now()
}
/// Reset a future to resolve at `new_deadline` instead. fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
*sleep = self.sleep_until(new_deadline);
}
}
/// A future returned by a `Timer`. pubtrait Sleep: Send + Sync + Future<Output = ()> { #[doc(hidden)] /// This method is private and can not be implemented by downstream crate fn __type_id(&self, _: private::Sealed) -> TypeId where Self: 'static,
{
TypeId::of::<Self>()
}
}
impldyn Sleep { //! This is a re-implementation of downcast methods from std::any::Any
/// Check whether the type is the same as `T` pubfn is<T>(&self) -> bool where
T: Sleep + 'static,
{ self.__type_id(private::Sealed {}) == TypeId::of::<T>()
}
/// Downcast a pinned &mut Sleep object to its original type pubfn downcast_mut_pin<T>(self: Pin<&mutSelf>) -> Option<Pin<&le='color:red'>mut T>> where
T: Sleep + 'static,
{ ifself.is::<T>() { unsafe { let inner = Pin::into_inner_unchecked(self);
Some(Pin::new_unchecked(
&mut *(&mut *inner as *mutdyn Sleep as *mutT),
))
}
} else {
None
}
}
}
mod private { #![allow(missing_debug_implementations)] pubstruct Sealed {}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.