|
2 | 2 |
|
3 | 3 | use std::future::Future; |
4 | 4 |
|
5 | | -/// Builds a multi-threaded Tokio runtime with all features enabled. |
6 | | -pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> { |
7 | | - tokio::runtime::Builder::new_multi_thread() |
8 | | - .enable_all() |
9 | | - .build() |
10 | | - .map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e)) |
11 | | -} |
| 5 | +/// A runtime manager. |
| 6 | +#[derive(Debug, Clone, Copy)] |
| 7 | +pub struct RuntimeManager; |
| 8 | + |
| 9 | +impl RuntimeManager { |
| 10 | + /// Builds a multi-threaded Tokio runtime with all features enabled. |
| 11 | + pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> { |
| 12 | + tokio::runtime::Builder::new_multi_thread() |
| 13 | + .enable_all() |
| 14 | + .build() |
| 15 | + .map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e)) |
| 16 | + } |
12 | 17 |
|
13 | | -/// Runs a future to completion, returning early on Ctrl+C. |
14 | | -pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()> |
15 | | -where |
16 | | - F: Future<Output = ()>, |
17 | | -{ |
18 | | - let ctrl_c = async { |
19 | | - tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler"); |
20 | | - }; |
| 18 | + /// Runs a future to completion, returning early on Ctrl+C. |
| 19 | + pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()> |
| 20 | + where |
| 21 | + F: Future<Output = ()>, |
| 22 | + { |
| 23 | + let ctrl_c = async { |
| 24 | + tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler"); |
| 25 | + }; |
21 | 26 |
|
22 | | - tokio::select! { |
23 | | - biased; |
24 | | - () = ctrl_c => Ok(()), |
25 | | - () = fut => Ok(()), |
| 27 | + tokio::select! { |
| 28 | + biased; |
| 29 | + () = ctrl_c => Ok(()), |
| 30 | + () = fut => Ok(()), |
| 31 | + } |
26 | 32 | } |
27 | | -} |
28 | 33 |
|
29 | | -/// Runs a fallible future to completion, returning early on Ctrl+C. |
30 | | -pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()> |
31 | | -where |
32 | | - F: Future<Output = eyre::Result<()>>, |
33 | | -{ |
34 | | - let ctrl_c = async { |
35 | | - tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler"); |
36 | | - }; |
| 34 | + /// Runs a fallible future to completion, returning early on Ctrl+C. |
| 35 | + pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()> |
| 36 | + where |
| 37 | + F: Future<Output = eyre::Result<()>>, |
| 38 | + { |
| 39 | + let ctrl_c = async { |
| 40 | + tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler"); |
| 41 | + }; |
37 | 42 |
|
38 | | - tokio::select! { |
39 | | - biased; |
40 | | - () = ctrl_c => Ok(()), |
41 | | - result = fut => result, |
| 43 | + tokio::select! { |
| 44 | + biased; |
| 45 | + () = ctrl_c => Ok(()), |
| 46 | + result = fut => result, |
| 47 | + } |
42 | 48 | } |
43 | 49 | } |
0 commit comments