use crate::impls::tokio::TokioRuntimeHandle as Handle;
use crate::{CompoundRuntime, RealCoarseTimeProvider, ToplevelBlockOn};
use std::io::{Error as IoError, Result as IoResult};
#[cfg(feature = "native-tls")]
use crate::impls::native_tls::NativeTlsProvider;
#[cfg(feature = "rustls")]
use crate::impls::rustls::RustlsProvider;
#[cfg(feature = "native-tls")]
pub use TokioNativeTlsRuntime as PreferredRuntime;
#[cfg(all(feature = "rustls", not(feature = "native-tls")))]
pub use TokioRustlsRuntime as PreferredRuntime;
#[derive(Clone)]
#[cfg(feature = "native-tls")]
pub struct TokioNativeTlsRuntime {
inner: HandleInner,
}
#[cfg(feature = "native-tls")]
type HandleInner = CompoundRuntime<
Handle,
Handle,
RealCoarseTimeProvider,
Handle,
Handle,
NativeTlsProvider,
Handle,
>;
#[derive(Clone)]
#[cfg(feature = "rustls")]
pub struct TokioRustlsRuntime {
inner: RustlsHandleInner,
}
#[cfg(feature = "rustls")]
type RustlsHandleInner =
CompoundRuntime<Handle, Handle, RealCoarseTimeProvider, Handle, Handle, RustlsProvider, Handle>;
#[cfg(feature = "native-tls")]
crate::opaque::implement_opaque_runtime! {
TokioNativeTlsRuntime { inner : HandleInner }
}
#[cfg(feature = "rustls")]
crate::opaque::implement_opaque_runtime! {
TokioRustlsRuntime { inner : RustlsHandleInner }
}
#[cfg(feature = "native-tls")]
impl From<tokio_crate::runtime::Handle> for TokioNativeTlsRuntime {
fn from(h: tokio_crate::runtime::Handle) -> Self {
let h = Handle::new(h);
TokioNativeTlsRuntime {
inner: CompoundRuntime::new(
h.clone(),
h.clone(),
RealCoarseTimeProvider::new(),
h.clone(),
h.clone(),
NativeTlsProvider::default(),
h,
),
}
}
}
#[cfg(feature = "rustls")]
impl From<tokio_crate::runtime::Handle> for TokioRustlsRuntime {
fn from(h: tokio_crate::runtime::Handle) -> Self {
let h = Handle::new(h);
TokioRustlsRuntime {
inner: CompoundRuntime::new(
h.clone(),
h.clone(),
RealCoarseTimeProvider::new(),
h.clone(),
h.clone(),
RustlsProvider::default(),
h,
),
}
}
}
#[cfg(feature = "native-tls")]
impl TokioNativeTlsRuntime {
pub fn create() -> IoResult<Self> {
crate::impls::tokio::create_runtime().map(|r| TokioNativeTlsRuntime {
inner: CompoundRuntime::new(
r.clone(),
r.clone(),
RealCoarseTimeProvider::new(),
r.clone(),
r.clone(),
NativeTlsProvider::default(),
r,
),
})
}
pub fn current() -> IoResult<Self> {
Ok(current_handle()?.into())
}
#[doc(hidden)]
pub fn run_test<P, F, O>(func: P) -> O
where
P: FnOnce(Self) -> F,
F: futures::Future<Output = O>,
{
let runtime = Self::create().expect("Failed to create runtime");
runtime.clone().block_on(func(runtime))
}
}
#[cfg(feature = "rustls")]
impl TokioRustlsRuntime {
pub fn create() -> IoResult<Self> {
crate::impls::tokio::create_runtime().map(|r| TokioRustlsRuntime {
inner: CompoundRuntime::new(
r.clone(),
r.clone(),
RealCoarseTimeProvider::new(),
r.clone(),
r.clone(),
RustlsProvider::default(),
r,
),
})
}
pub fn current() -> IoResult<Self> {
Ok(current_handle()?.into())
}
#[doc(hidden)]
pub fn run_test<P, F, O>(func: P) -> O
where
P: FnOnce(Self) -> F,
F: futures::Future<Output = O>,
{
let runtime = Self::create().expect("Failed to create runtime");
runtime.clone().block_on(func(runtime))
}
}
#[cfg(any(feature = "native-tls", feature = "rustls"))]
fn current_handle() -> std::io::Result<tokio_crate::runtime::Handle> {
tokio_crate::runtime::Handle::try_current().map_err(IoError::other)
}
#[cfg(all(
test,
not(miri), // tokio makes some syscalls that don't work with miri
))]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_time_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
#[test]
fn no_current() {
#[cfg(feature = "native-tls")]
assert!(TokioNativeTlsRuntime::current().is_err());
#[cfg(feature = "rustls")]
assert!(TokioRustlsRuntime::current().is_err());
}
#[test]
fn current() {
let runtime = PreferredRuntime::create().unwrap();
runtime.block_on(async {
#[cfg(feature = "native-tls")]
assert!(TokioNativeTlsRuntime::current().is_ok());
#[cfg(feature = "rustls")]
assert!(TokioRustlsRuntime::current().is_ok());
});
}
#[test]
fn debug() {
#[cfg(feature = "native-tls")]
assert_eq!(
format!("{:?}", TokioNativeTlsRuntime::create().unwrap()),
"TokioNativeTlsRuntime { .. }"
);
#[cfg(feature = "rustls")]
assert_eq!(
format!("{:?}", TokioRustlsRuntime::create().unwrap()),
"TokioRustlsRuntime { .. }"
);
assert_eq!(
format!("{:?}", PreferredRuntime::create().unwrap().inner),
"CompoundRuntime { .. }"
);
}
}