summaryrefslogtreecommitdiff
path: root/crates/tor-rtcompat/src/tokio.rs
blob: 673e3c1dd77f9bc08d98c51ed691bf05cba2abf5 (plain)
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
//! Entry points for use with Tokio runtimes.
pub use crate::impls::tokio::create_runtime as create_tokio_runtime;
pub use crate::impls::tokio::TokioRuntimeHandle;

use crate::Runtime;
use std::io::{Error as IoError, ErrorKind};

/// Create a new Tokio-based [`Runtime`].
///
/// Generally you should call this function only once, and then use
/// [`Clone::clone()`] to create additional references to that
/// runtime.
///
/// Tokio users may want to avoid this function and instead make a
/// runtime using [`current_runtime()`] or
/// [`TokioRuntimeHandle::new()`]: this function always _builds_ a
/// runtime, and if you already have a runtime, that isn't what you
/// want with Tokio.
pub fn create_runtime() -> std::io::Result<impl Runtime> {
    create_tokio_runtime()
}

/// Try to return an instance of the currently running tokio [`Runtime`].
///
/// # Usage note
///
/// We should never call this from inside other Arti crates, or from
/// library crates that want to support multiple runtimes!  This
/// function is for Arti _users_ who want to wrap some existing Tokio
/// runtime as a [`Runtime`].  It is not for library
/// crates that want to work with multiple runtimes.
///
/// Once you have a runtime returned by this function, you should
/// just create more handles to it via [`Clone`].
pub fn current_runtime() -> std::io::Result<impl Runtime> {
    let handle = tokio_crate::runtime::Handle::try_current()
        .map_err(|e| IoError::new(ErrorKind::Other, e))?;
    Ok(TokioRuntimeHandle::new(handle))
}

/// Run a test function using a freshly created tokio runtime.
///
/// # Panics
///
/// Panics if we can't create a tokio runtime.
pub fn test_with_runtime<P, F, O>(func: P) -> O
where
    P: FnOnce(async_executors::TokioTp) -> F,
    F: futures::Future<Output = O>,
{
    let runtime = create_tokio_runtime().expect("Failed to create a tokio runtime");
    runtime.block_on(func(runtime.clone()))
}