tor_rtcompat/async_std.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
//! Entry points for use with async_std runtimes.
pub use crate::impls::async_std::create_runtime as create_runtime_impl;
use crate::{compound::CompoundRuntime, BlockOn, RealCoarseTimeProvider};
use std::io::Result as IoResult;
#[cfg(feature = "native-tls")]
use crate::impls::native_tls::NativeTlsProvider;
#[cfg(feature = "rustls")]
use crate::impls::rustls::RustlsProvider;
use async_executors::AsyncStd;
/// An alias for the async_std runtime that we prefer to use, based on whatever TLS
/// implementation has been enabled.
///
/// If only one of `native_tls` and `rustls` bas been enabled within the
/// `tor-rtcompat` crate, that will be the TLS backend that this uses.
///
/// Currently, `native_tls` is preferred over `rustls` when both are available,
/// because of its maturity within Arti. However, this might change in the
/// future.
#[cfg(feature = "native-tls")]
pub use AsyncStdNativeTlsRuntime as PreferredRuntime;
#[cfg(all(feature = "rustls", not(feature = "native-tls")))]
pub use AsyncStdRustlsRuntime as PreferredRuntime;
/// A [`Runtime`](crate::Runtime) powered by `async_std` and `native_tls`.
#[derive(Clone)]
#[cfg(feature = "native-tls")]
pub struct AsyncStdNativeTlsRuntime {
/// The actual runtime object.
inner: NativeTlsInner,
}
/// Implementation type for AsyncStdRuntime.
#[cfg(feature = "native-tls")]
type NativeTlsInner = CompoundRuntime<
AsyncStd,
AsyncStd,
RealCoarseTimeProvider,
AsyncStd,
AsyncStd,
NativeTlsProvider,
AsyncStd,
>;
#[cfg(feature = "native-tls")]
crate::opaque::implement_opaque_runtime! {
AsyncStdNativeTlsRuntime { inner : NativeTlsInner }
}
#[cfg(feature = "rustls")]
/// A [`Runtime`](crate::Runtime) powered by `async_std` and `rustls`.
#[derive(Clone)]
pub struct AsyncStdRustlsRuntime {
/// The actual runtime object.
inner: RustlsInner,
}
/// Implementation type for AsyncStdRustlsRuntime.
#[cfg(feature = "rustls")]
type RustlsInner = CompoundRuntime<
AsyncStd,
AsyncStd,
RealCoarseTimeProvider,
AsyncStd,
AsyncStd,
RustlsProvider,
AsyncStd,
>;
#[cfg(feature = "rustls")]
crate::opaque::implement_opaque_runtime! {
AsyncStdRustlsRuntime { inner: RustlsInner }
}
#[cfg(feature = "native-tls")]
impl AsyncStdNativeTlsRuntime {
/// Return a new [`AsyncStdNativeTlsRuntime`]
///
/// Generally you should call this function only once, and then use
/// [`Clone::clone()`] to create additional references to that
/// runtime.
pub fn create() -> IoResult<Self> {
let rt = create_runtime_impl();
let ct = RealCoarseTimeProvider::new();
Ok(AsyncStdNativeTlsRuntime {
inner: CompoundRuntime::new(rt, rt, ct, rt, rt, NativeTlsProvider::default(), rt),
})
}
/// Return an [`AsyncStdNativeTlsRuntime`] for the currently running
/// `async_std` executor.
///
/// Note that since async_std executors are global, there is no distinction
/// between this method and [`AsyncStdNativeTlsRuntime::create()`]: it is
/// provided only for API consistency with the Tokio runtimes.
pub fn current() -> IoResult<Self> {
Self::create()
}
/// Helper to run a single test function in a freshly created runtime.
///
/// # Panics
///
/// Panics if we can't create this runtime.
///
/// # Warning
///
/// This API is **NOT** for consumption outside Arti. Semver guarantees are not provided.
#[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 AsyncStdRustlsRuntime {
/// Return a new [`AsyncStdRustlsRuntime`]
///
/// Generally you should call this function only once, and then use
/// [`Clone::clone()`] to create additional references to that
/// runtime.
pub fn create() -> IoResult<Self> {
let rt = create_runtime_impl();
let ct = RealCoarseTimeProvider::new();
Ok(AsyncStdRustlsRuntime {
inner: CompoundRuntime::new(rt, rt, ct, rt, rt, RustlsProvider::default(), rt),
})
}
/// Return an [`AsyncStdRustlsRuntime`] for the currently running
/// `async_std` executor.
///
/// Note that since async_std executors are global, there is no distinction
/// between this method and [`AsyncStdNativeTlsRuntime::create()`]: it is
/// provided only for API consistency with the Tokio runtimes.
pub fn current() -> IoResult<Self> {
Self::create()
}
/// Helper to run a single test function in a freshly created runtime.
///
/// # Panics
///
/// Panics if we can't create this runtime.
///
/// # Warning
///
/// This API is **NOT** for consumption outside Arti. Semver guarantees are not provided.
#[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(not(miri))] // async_ztd startup seems to fail under miri
#[cfg(test)]
mod test {
// @@ begin test lint list maintained by maint/add_warning @@
#![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_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*;
#[test]
fn current() {
// We should actually have to run this inside a runtime with async_std,
// but let's do it anyway to make sure that "current" works.
let runtime = PreferredRuntime::create().unwrap();
runtime.block_on(async {
#[cfg(feature = "native-tls")]
assert!(AsyncStdNativeTlsRuntime::current().is_ok());
#[cfg(feature = "rustls")]
assert!(AsyncStdRustlsRuntime::current().is_ok());
});
}
#[test]
fn debug() {
#[cfg(feature = "native-tls")]
assert_eq!(
format!("{:?}", AsyncStdNativeTlsRuntime::create().unwrap()),
"AsyncStdNativeTlsRuntime { .. }"
);
#[cfg(feature = "rustls")]
assert_eq!(
format!("{:?}", AsyncStdRustlsRuntime::create().unwrap()),
"AsyncStdRustlsRuntime { .. }"
);
}
}