diff --git a/src/blocking.rs b/src/blocking.rs index abf4282c..01be30dd 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -1,10 +1,15 @@ //! Thread pool for blocking operations +use std::fmt; + +use derive_more::Display; use futures::sync::oneshot; use futures::{Async, Future, Poll}; use parking_lot::Mutex; use threadpool::ThreadPool; +use crate::ResponseError; + /// Env variable for default cpu pool size const ENV_CPU_POOL_VAR: &str = "ACTIX_CPU_POOL"; @@ -36,18 +41,23 @@ thread_local! { }; } -pub enum BlockingError { +#[derive(Debug, Display)] +pub enum BlockingError { + #[display(fmt = "{:?}", _0)] Error(E), + #[display(fmt = "Thread pool is gone")] Canceled, } +impl ResponseError for BlockingError {} + /// Execute blocking function on a thread pool, returns future that resolves /// to result of the function execution. pub fn run(f: F) -> CpuFuture where F: FnOnce() -> Result + Send + 'static, I: Send + 'static, - E: Send + 'static, + E: Send + fmt::Debug + 'static, { let (tx, rx) = oneshot::channel(); POOL.with(|pool| { @@ -63,7 +73,7 @@ pub struct CpuFuture { rx: oneshot::Receiver>, } -impl Future for CpuFuture { +impl Future for CpuFuture { type Item = I; type Error = BlockingError;