aws_smithy_async/rt/
sleep.rsuse std::fmt::{Debug, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
pub trait AsyncSleep: Debug + Send + Sync {
fn sleep(&self, duration: Duration) -> Sleep;
}
impl<T> AsyncSleep for Box<T>
where
T: AsyncSleep,
T: ?Sized,
{
fn sleep(&self, duration: Duration) -> Sleep {
T::sleep(self, duration)
}
}
impl<T> AsyncSleep for Arc<T>
where
T: AsyncSleep,
T: ?Sized,
{
fn sleep(&self, duration: Duration) -> Sleep {
T::sleep(self, duration)
}
}
#[derive(Clone, Debug)]
pub struct SharedAsyncSleep(Arc<dyn AsyncSleep>);
impl SharedAsyncSleep {
pub fn new(sleep: impl AsyncSleep + 'static) -> Self {
Self(Arc::new(sleep))
}
}
impl AsRef<dyn AsyncSleep> for SharedAsyncSleep {
fn as_ref(&self) -> &(dyn AsyncSleep + 'static) {
self.0.as_ref()
}
}
impl From<Arc<dyn AsyncSleep>> for SharedAsyncSleep {
fn from(sleep: Arc<dyn AsyncSleep>) -> Self {
SharedAsyncSleep(sleep)
}
}
impl AsyncSleep for SharedAsyncSleep {
fn sleep(&self, duration: Duration) -> Sleep {
self.0.sleep(duration)
}
}
#[cfg(feature = "rt-tokio")]
pub fn default_async_sleep() -> Option<SharedAsyncSleep> {
Some(SharedAsyncSleep::from(sleep_tokio()))
}
#[cfg(not(feature = "rt-tokio"))]
pub fn default_async_sleep() -> Option<SharedAsyncSleep> {
None
}
#[non_exhaustive]
#[must_use]
pub struct Sleep(Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>);
impl Debug for Sleep {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Sleep")
}
}
impl Sleep {
pub fn new(future: impl Future<Output = ()> + Send + Sync + 'static) -> Sleep {
Sleep(Box::pin(future))
}
}
impl Future for Sleep {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.0.as_mut().poll(cx)
}
}
#[non_exhaustive]
#[cfg(feature = "rt-tokio")]
#[derive(Debug, Default)]
pub struct TokioSleep;
#[cfg(feature = "rt-tokio")]
impl TokioSleep {
pub fn new() -> TokioSleep {
Default::default()
}
}
#[cfg(feature = "rt-tokio")]
impl AsyncSleep for TokioSleep {
fn sleep(&self, duration: Duration) -> Sleep {
Sleep::new(tokio::time::sleep(duration))
}
}
#[cfg(feature = "rt-tokio")]
fn sleep_tokio() -> Arc<dyn AsyncSleep> {
Arc::new(TokioSleep::new())
}