use std::pin::Pin;
use futures::Stream;
#[cfg(all(target_family = "wasm", target_feature = "atomics"))]
compile_error!(
"rig-http does not support threaded wasm (`+atomics`): its wasm-compat markers assume a \
single-threaded target"
);
#[cfg(not(target_family = "wasm"))]
#[diagnostic::on_unimplemented(
message = "`{Self}` is not `Send`, and Rig needs `Send` natively",
label = "not `Send`",
note = "handlers and transports run inside a driver's task: hold shared state behind an `Arc` (never an `Rc`), or use a `!Send` value only on browser wasm, where this marker is a no-op"
)]
pub trait WasmCompatSend: Send {}
#[cfg(target_family = "wasm")]
pub trait WasmCompatSend {}
#[cfg(not(target_family = "wasm"))]
impl<T> WasmCompatSend for T where T: Send {}
#[cfg(target_family = "wasm")]
impl<T> WasmCompatSend for T {}
#[cfg(not(target_family = "wasm"))]
#[diagnostic::on_unimplemented(
message = "`{Self}` is not `Sync`, and Rig needs `Sync` natively",
label = "not `Sync`",
note = "handlers and transports are shared between a driver and its in-flight tasks: use `Mutex`/atomics instead of `Cell`/`RefCell`, or use a `!Sync` value only on browser wasm, where this marker is a no-op"
)]
pub trait WasmCompatSync: Sync {}
#[cfg(target_family = "wasm")]
pub trait WasmCompatSync {}
#[cfg(not(target_family = "wasm"))]
impl<T> WasmCompatSync for T where T: Sync {}
#[cfg(target_family = "wasm")]
impl<T> WasmCompatSync for T {}
#[cfg(not(target_family = "wasm"))]
pub type WasmBoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[cfg(target_family = "wasm")]
pub type WasmBoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
#[cfg(not(target_family = "wasm"))]
pub type WasmBoxedStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
#[cfg(target_family = "wasm")]
pub type WasmBoxedStream<'a, T> = Pin<Box<dyn Stream<Item = T> + 'a>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("future timed out")]
pub struct Elapsed;
pub async fn timeout<F>(duration: std::time::Duration, future: F) -> Result<F::Output, Elapsed>
where
F: Future,
{
use futures::future::{Either, select};
let delay = futures_timer::Delay::new(duration);
futures::pin_mut!(future);
futures::pin_mut!(delay);
match select(future, delay).await {
Either::Left((output, _)) => Ok(output),
Either::Right(((), _)) => Err(Elapsed),
}
}
pub async fn sleep(duration: std::time::Duration) {
futures_timer::Delay::new(duration).await;
}
#[cfg(test)]
mod tests;