Lines
100 %
Functions
90.51 %
Branches
//! 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};
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(all(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)]
pub struct AsyncStdNativeTlsRuntime {
/// The actual runtime object.
inner: NativeTlsInner,
}
/// Implementation type for AsyncStdRuntime.
type NativeTlsInner = CompoundRuntime<AsyncStd, AsyncStd, AsyncStd, NativeTlsProvider>;
crate::opaque::implement_opaque_runtime! {
AsyncStdNativeTlsRuntime { inner : NativeTlsInner }
/// A [`Runtime`](crate::Runtime) powered by `async_std` and `rustls`.
pub struct AsyncStdRustlsRuntime {
inner: RustlsInner,
/// Implementation type for AsyncStdRustlsRuntime.
type RustlsInner = CompoundRuntime<AsyncStd, AsyncStd, AsyncStd, RustlsProvider>;
AsyncStdRustlsRuntime { inner: RustlsInner }
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();
Ok(AsyncStdNativeTlsRuntime {
inner: CompoundRuntime::new(rt, rt, rt, NativeTlsProvider::default()),
})
/// 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))
impl AsyncStdRustlsRuntime {
/// Return a new [`AsyncStdRustlsRuntime`]
Ok(AsyncStdRustlsRuntime {
inner: CompoundRuntime::new(rt, rt, rt, RustlsProvider::default()),
/// Return an [`AsyncStdRustlsRuntime`] for the currently running
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
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 {
assert!(AsyncStdNativeTlsRuntime::current().is_ok());
assert!(AsyncStdRustlsRuntime::current().is_ok());
});
fn debug() {
assert_eq!(
format!("{:?}", AsyncStdNativeTlsRuntime::create().unwrap()),
"AsyncStdNativeTlsRuntime { .. }"
);
format!("{:?}", AsyncStdRustlsRuntime::create().unwrap()),
"AsyncStdRustlsRuntime { .. }"