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`.
6
pub use crate::impls::smol::create_runtime as create_runtime_impl;
7

            
8
use crate::{RealCoarseTimeProvider, ToplevelBlockOn, compound::CompoundRuntime};
9
use std::io::Result as IoResult;
10

            
11
#[cfg(feature = "native-tls")]
12
use crate::impls::native_tls::NativeTlsProvider;
13
#[cfg(feature = "rustls")]
14
use crate::impls::rustls::RustlsProvider;
15

            
16
// Bring in our SmolRuntime type.
17
use 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")]
22
pub use SmolNativeTlsRuntime as PreferredRuntime;
23
#[cfg(all(feature = "rustls", not(feature = "native-tls")))]
24
pub use SmolRustlsRuntime as PreferredRuntime;
25

            
26
/// A [`Runtime`](crate::Runtime) powered by smol and native-tls.
27
#[derive(Clone)]
28
#[cfg(feature = "native-tls")]
29
pub struct SmolNativeTlsRuntime {
30
    /// The actual runtime object.
31
    inner: NativeTlsInner,
32
}
33

            
34
/// Implementation type for SmolRuntime using NativeTls.
35
#[cfg(feature = "native-tls")]
36
type NativeTlsInner = CompoundRuntime<
37
    SmolRuntime,
38
    SmolRuntime,
39
    RealCoarseTimeProvider,
40
    SmolRuntime,
41
    SmolRuntime,
42
    NativeTlsProvider,
43
    SmolRuntime,
44
>;
45

            
46
#[cfg(feature = "native-tls")]
47
crate::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")]
54
pub struct SmolRustlsRuntime {
55
    /// The actual runtime object.
56
    inner: RustlsInner,
57
}
58

            
59
/// Implementation type for SmolRuntime using Rustls.
60
#[cfg(feature = "rustls")]
61
type RustlsInner = CompoundRuntime<
62
    SmolRuntime,
63
    SmolRuntime,
64
    RealCoarseTimeProvider,
65
    SmolRuntime,
66
    SmolRuntime,
67
    RustlsProvider,
68
    SmolRuntime,
69
>;
70

            
71
#[cfg(feature = "rustls")]
72
crate::opaque::implement_opaque_runtime! {
73
    SmolRustlsRuntime { inner: RustlsInner }
74
}
75

            
76
#[cfg(feature = "native-tls")]
77
impl SmolNativeTlsRuntime {
78
    /// Create a new `SmolNativeTlsRuntime` (owns its executor).
79
530
    pub fn create() -> IoResult<Self> {
80
530
        let rt = create_runtime_impl();
81
530
        let ct = RealCoarseTimeProvider::new();
82
530
        Ok(SmolNativeTlsRuntime {
83
530
            inner: CompoundRuntime::new(
84
530
                rt.clone(),
85
530
                rt.clone(),
86
530
                ct,
87
530
                rt.clone(),
88
530
                rt.clone(),
89
530
                NativeTlsProvider::default(),
90
530
                rt.clone(),
91
530
            ),
92
530
        })
93
530
    }
94

            
95
    /// Run a single test function in a fresh runtime (Arti-internal API).
96
    #[doc(hidden)]
97
144
    pub fn run_test<P, F, O>(func: P) -> O
98
144
    where
99
144
        P: FnOnce(Self) -> F,
100
144
        F: futures::Future<Output = O>,
101
    {
102
144
        let runtime = Self::create().expect("Failed to create runtime");
103
144
        runtime.clone().block_on(func(runtime))
104
144
    }
105
}
106

            
107
#[cfg(feature = "rustls")]
108
impl SmolRustlsRuntime {
109
    /// Create a new `SmolRustlsRuntime` (owns its executor).
110
516
    pub fn create() -> IoResult<Self> {
111
516
        let rt = create_runtime_impl();
112
516
        let ct = RealCoarseTimeProvider::new();
113
516
        Ok(SmolRustlsRuntime {
114
516
            inner: CompoundRuntime::new(
115
516
                rt.clone(),
116
516
                rt.clone(),
117
516
                ct,
118
516
                rt.clone(),
119
516
                rt.clone(),
120
516
                RustlsProvider::default(),
121
516
                rt.clone(),
122
516
            ),
123
516
        })
124
516
    }
125

            
126
    #[doc(hidden)]
127
144
    pub fn run_test<P, F, O>(func: P) -> O
128
144
    where
129
144
        P: FnOnce(Self) -> F,
130
144
        F: futures::Future<Output = O>,
131
    {
132
144
        let runtime = Self::create().expect("Failed to create runtime");
133
144
        runtime.clone().block_on(func(runtime))
134
144
    }
135
}
136

            
137
#[cfg(all(test, not(miri)))]
138
mod 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
}