#[cfg(feature = "blocking-default")]
use crate::blocking::DefaultBlockingThreadPool;
use crate::{blocking::BlockingThreadPool, driver::AnyDriver};
#[derive(Clone)]
pub enum DriverKind {
#[cfg(unix)]
Mio,
#[cfg(windows)]
Iocp,
Mock,
#[cfg(target_os = "linux")]
IoUring,
#[cfg(target_os = "linux")]
IoUringCustom(io_uring::Builder),
}
impl DriverKind {
#[inline]
pub(crate) fn into_driver(self) -> Result<AnyDriver, std::io::Error> {
match self {
#[cfg(unix)]
DriverKind::Mio => AnyDriver::new_mio(),
#[cfg(windows)]
DriverKind::Iocp => AnyDriver::new_iocp(),
DriverKind::Mock => Ok(AnyDriver::new_mock()),
#[cfg(target_os = "linux")]
DriverKind::IoUring => AnyDriver::new_uring(),
#[cfg(target_os = "linux")]
DriverKind::IoUringCustom(builder) => AnyDriver::new_uring_custom(builder),
}
}
}
pub struct RuntimeBuilder {
driver_kind: Option<DriverKind>,
enable_timer: bool,
enable_fs_offload: bool,
blocking_pool: Option<Box<dyn BlockingThreadPool>>,
}
impl RuntimeBuilder {
pub fn new() -> Self {
Self {
driver_kind: None,
enable_timer: false,
enable_fs_offload: false,
blocking_pool: None,
}
}
pub fn driver(mut self, driver_kind: DriverKind) -> Self {
self.driver_kind = Some(driver_kind);
self
}
pub fn enable_timer(mut self, enable: bool) -> Self {
self.enable_timer = enable;
self
}
pub fn enable_fs_offload(mut self, enable: bool) -> Self {
self.enable_fs_offload = enable;
self
}
pub fn blocking_pool(mut self, blocking_pool: Box<dyn BlockingThreadPool>) -> Self {
self.blocking_pool = Some(blocking_pool);
self
}
#[cfg(feature = "blocking-default")]
pub fn default_blocking_pool(mut self, max_threads: usize) -> Self {
self.blocking_pool = Some(Box::new(DefaultBlockingThreadPool::with_max_threads(
max_threads,
)));
self
}
pub fn build(self) -> Result<crate::executor::Runtime, std::io::Error> {
let driver = if let Some(driver_kind) = self.driver_kind {
driver_kind.into_driver()?
} else {
AnyDriver::new_best()?
};
Ok(crate::executor::Runtime::with_options(
driver,
self.enable_timer,
self.blocking_pool,
self.enable_fs_offload,
))
}
}
impl Default for RuntimeBuilder {
fn default() -> Self {
Self::new()
}
}