jtp 0.1.0

A simple implementation of a thread pool.
Documentation
//! # Thread Pool
//!
//! A thread pool allows you to execute asyncronous tasks without
//! creating new threads for each one. It improves the performance and
//! the resource utilization by limiting the number of threads.
//!
//! # Build a thread pool
//!
//! You can use the [`ThreadPoolBuilder`] to build a thread pool with
//! the configuration.
//!
//! # Examples
//!
//! ```
//! use jtp::ThreadPoolBuilder;
//! let mut thread_pool = ThreadPoolBuilder::default()
//!     .set_core_pool_size(5)
//!     .set_max_pool_size(10)
//!     .set_channel_capacity(100)
//!     .build()
//!     .unwrap();
//!
//! // Even you start 50 tasks, there only be 10 threads at most at
//! // a time(the max_pool_size is setted as 10).
//! for _ in 0..50 {
//!     thread_pool.execute(|| println!("Hello World")).unwrap();
//! }
//! ```

mod builder;
mod thread_pool;

pub mod task;
pub(crate) mod worker;

#[cfg(test)]
mod tests;

pub use builder::*;
pub use thread_pool::*;

type TPResult<T> = Result<T, TPError>;

/// An error returned from the [`ThreadPool::execute`].
///
/// [`ThreadPool::execute`]: crate::ThreadPool::execute
#[derive(Debug)]
pub enum TPError {
    /// The task could not be executed because the task is rejected
    /// by [`RejectedTaskHandler::Abort`] when the thread pool and the
    /// task channel are full.
    AbortError,

    /// The task could not be executed because the task channel is
    /// disconnected.
    DisconnectedError,
}

impl std::error::Error for TPError {}

impl std::fmt::Display for TPError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            TPError::AbortError => writeln!(f, "task abortion error."),
            TPError::DisconnectedError => writeln!(f, "the channel is disconnected"),
        }
    }
}