use std::future::Future;
pub type JoinHandle<T> = tokio::task::JoinHandle<T>;
pub type Duration = tokio::time::Duration;
pub type Instant = tokio::time::Instant;
pub async fn sleep(dur: Duration) {
tokio::time::sleep(dur).await;
}
pub type Interval = tokio::time::Interval;
pub fn interval(dur: Duration) -> Interval {
tokio::time::interval(dur)
}
pub type JoinSet<T> = tokio::task::JoinSet<T>;
pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
spawn_named(None, future)
}
pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
{
tokio::task::spawn_local(future)
}
pub fn spawn_named<F>(name: Option<&str>, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
#[cfg(tokio_unstable)]
{
let mut builder = tokio::task::Builder::new();
if let Some(name) = name {
builder = builder.name(name);
}
builder.spawn(future).expect("Tokio task spawn failed")
}
#[cfg(not(tokio_unstable))]
{
let _ = name;
tokio::task::spawn(future)
}
}
pub async fn timeout<F, T>(dur: super::Duration, future: F) -> Result<T, super::Timeout>
where
F: Future<Output = T>,
{
tokio::time::timeout(dur, future)
.await
.map_err(|_| super::Timeout)
}
macro_rules! select {
($($tokens:tt)*) => {{
tokio::select! {
biased;
$( $tokens )*
}
}}
}
pub(crate) use select;
pub use tokio::test;