tor_rtcompat/
smol.rs

1//! Entry points for use with smol runtimes.
2//! This crate helps define a slim API around our async runtime so that we
3//! can easily swap it out.
4
5/// Re-export the Smol runtime constructor implemented in `impls/smol.rs`.
6pub use crate::impls::smol::create_runtime as create_runtime_impl;
7
8use crate::{RealCoarseTimeProvider, ToplevelBlockOn, compound::CompoundRuntime};
9use std::io::Result as IoResult;
10
11#[cfg(feature = "native-tls")]
12use crate::impls::native_tls::NativeTlsProvider;
13#[cfg(feature = "rustls")]
14use crate::impls::rustls::RustlsProvider;
15
16// Bring in our SmolRuntime type.
17use crate::impls::smol::SmolRuntime;
18
19/// An alias for the smol runtime that we prefer to use, based on whatever TLS
20/// implementation has been enabled.
21#[cfg(feature = "native-tls")]
22pub use SmolNativeTlsRuntime as PreferredRuntime;
23#[cfg(all(feature = "rustls", not(feature = "native-tls")))]
24pub use SmolRustlsRuntime as PreferredRuntime;
25
26/// A [`Runtime`](crate::Runtime) powered by smol and native-tls.
27#[derive(Clone)]
28#[cfg(feature = "native-tls")]
29pub struct SmolNativeTlsRuntime {
30    /// The actual runtime object.
31    inner: NativeTlsInner,
32}
33
34/// Implementation type for SmolRuntime using NativeTls.
35#[cfg(feature = "native-tls")]
36type NativeTlsInner = CompoundRuntime<
37    SmolRuntime,
38    SmolRuntime,
39    RealCoarseTimeProvider,
40    SmolRuntime,
41    SmolRuntime,
42    NativeTlsProvider,
43    SmolRuntime,
44>;
45
46#[cfg(feature = "native-tls")]
47crate::opaque::implement_opaque_runtime! {
48    SmolNativeTlsRuntime { inner: NativeTlsInner }
49}
50
51/// A [`Runtime`](crate::Runtime) powered by smol and rustls.
52#[derive(Clone)]
53#[cfg(feature = "rustls")]
54pub struct SmolRustlsRuntime {
55    /// The actual runtime object.
56    inner: RustlsInner,
57}
58
59/// Implementation type for SmolRuntime using Rustls.
60#[cfg(feature = "rustls")]
61type RustlsInner = CompoundRuntime<
62    SmolRuntime,
63    SmolRuntime,
64    RealCoarseTimeProvider,
65    SmolRuntime,
66    SmolRuntime,
67    RustlsProvider,
68    SmolRuntime,
69>;
70
71#[cfg(feature = "rustls")]
72crate::opaque::implement_opaque_runtime! {
73    SmolRustlsRuntime { inner: RustlsInner }
74}
75
76#[cfg(feature = "native-tls")]
77impl SmolNativeTlsRuntime {
78    /// Create a new `SmolNativeTlsRuntime` (owns its executor).
79    pub fn create() -> IoResult<Self> {
80        let rt = create_runtime_impl();
81        let ct = RealCoarseTimeProvider::new();
82        Ok(SmolNativeTlsRuntime {
83            inner: CompoundRuntime::new(
84                rt.clone(),
85                rt.clone(),
86                ct,
87                rt.clone(),
88                rt.clone(),
89                NativeTlsProvider::default(),
90                rt.clone(),
91            ),
92        })
93    }
94
95    /// Run a single test function in a fresh runtime (Arti-internal API).
96    #[doc(hidden)]
97    pub fn run_test<P, F, O>(func: P) -> O
98    where
99        P: FnOnce(Self) -> F,
100        F: futures::Future<Output = O>,
101    {
102        let runtime = Self::create().expect("Failed to create runtime");
103        runtime.clone().block_on(func(runtime))
104    }
105}
106
107#[cfg(feature = "rustls")]
108impl SmolRustlsRuntime {
109    /// Create a new `SmolRustlsRuntime` (owns its executor).
110    pub fn create() -> IoResult<Self> {
111        let rt = create_runtime_impl();
112        let ct = RealCoarseTimeProvider::new();
113        Ok(SmolRustlsRuntime {
114            inner: CompoundRuntime::new(
115                rt.clone(),
116                rt.clone(),
117                ct,
118                rt.clone(),
119                rt.clone(),
120                RustlsProvider::default(),
121                rt.clone(),
122            ),
123        })
124    }
125
126    #[doc(hidden)]
127    pub fn run_test<P, F, O>(func: P) -> O
128    where
129        P: FnOnce(Self) -> F,
130        F: futures::Future<Output = O>,
131    {
132        let runtime = Self::create().expect("Failed to create runtime");
133        runtime.clone().block_on(func(runtime))
134    }
135}
136
137#[cfg(all(test, not(miri)))]
138mod test {
139    // @@ begin test lint list maintained by maint/add_warning @@
140    #![allow(clippy::bool_assert_comparison)]
141    #![allow(clippy::clone_on_copy)]
142    #![allow(clippy::dbg_macro)]
143    #![allow(clippy::mixed_attributes_style)]
144    #![allow(clippy::print_stderr)]
145    #![allow(clippy::print_stdout)]
146    #![allow(clippy::single_char_pattern)]
147    #![allow(clippy::unwrap_used)]
148    #![allow(clippy::unchecked_time_subtraction)]
149    #![allow(clippy::useless_vec)]
150    #![allow(clippy::needless_pass_by_value)]
151    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
152    use super::*;
153
154    #[test]
155    fn debug() {
156        #[cfg(feature = "native-tls")]
157        assert_eq!(
158            format!("{:?}", SmolNativeTlsRuntime::create().unwrap()),
159            "SmolNativeTlsRuntime { .. }"
160        );
161        #[cfg(feature = "rustls")]
162        assert_eq!(
163            format!("{:?}", SmolRustlsRuntime::create().unwrap()),
164            "SmolRustlsRuntime { .. }"
165        );
166    }
167}