Skip to main content

rust_elm/runtime/
error.rs

1use std::fmt;
2use std::io;
3
4/// Errors starting or configuring a live runtime.
5#[derive(Debug)]
6pub enum RuntimeError {
7    TokioBuild(io::Error),
8    ThreadSpawn(io::Error),
9}
10
11impl fmt::Display for RuntimeError {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        match self {
14            Self::TokioBuild(err) => write!(f, "failed to create Tokio runtime: {err}"),
15            Self::ThreadSpawn(err) => write!(f, "failed to spawn reducer thread: {err}"),
16        }
17    }
18}
19
20impl std::error::Error for RuntimeError {
21    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
22        match self {
23            Self::TokioBuild(err) | Self::ThreadSpawn(err) => Some(err),
24        }
25    }
26}
27
28pub(crate) fn build_tokio(
29    worker_threads: usize,
30    thread_name: &str,
31) -> Result<std::sync::Arc<tokio::runtime::Runtime>, RuntimeError> {
32    tokio::runtime::Builder::new_multi_thread()
33        .worker_threads(worker_threads.max(1))
34        .thread_name(thread_name)
35        .enable_all()
36        .build()
37        .map(std::sync::Arc::new)
38        .map_err(RuntimeError::TokioBuild)
39}