use super::CallbackExecutor;
use crate::common::common_engine::LazyCallbackPool;
use crate::common::CommonEngine;
use crate::{NetError, Result};
pub(crate) struct CommonCallbackExecutor {
pool: LazyCallbackPool,
runtime: tokio::runtime::Handle,
}
impl CommonCallbackExecutor {
pub(crate) fn new(engine: &CommonEngine) -> Self {
Self {
pool: engine.cb_pool.clone(),
runtime: engine.runtime_handle(),
}
}
}
impl CallbackExecutor for CommonCallbackExecutor {
fn ensure_ready(&self) -> Result<()> {
self.pool.ensure_ready().map_err(NetError::from)
}
fn submit(&self, job: Box<dyn FnOnce() + Send + 'static>) -> Result<()> {
let runtime = self.runtime.clone();
self.pool
.execute(move || {
let _runtime_guard = runtime.enter();
job();
})
.map_err(NetError::from)
}
}
#[cfg(test)]
#[path = "common_executor_tests.rs"]
mod tests;