deadpool/managed/
config.rs
1use std::{fmt, time::Duration};
2
3use super::BuildError;
4
5#[derive(Clone, Copy, Debug)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
10pub struct PoolConfig {
11 pub max_size: usize,
15
16 #[cfg_attr(feature = "serde", serde(default))]
20 pub timeouts: Timeouts,
21}
22
23impl PoolConfig {
24 #[must_use]
27 pub fn new(max_size: usize) -> Self {
28 Self {
29 max_size,
30 timeouts: Timeouts::default(),
31 }
32 }
33}
34
35impl Default for PoolConfig {
36 fn default() -> Self {
39 Self::new(num_cpus::get_physical() * 4)
40 }
41}
42
43#[derive(Clone, Copy, Debug)]
48#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
49pub struct Timeouts {
50 pub wait: Option<Duration>,
52
53 pub create: Option<Duration>,
55
56 pub recycle: Option<Duration>,
58}
59
60impl Timeouts {
61 #[must_use]
63 pub fn new() -> Self {
64 Self::default()
65 }
66
67 #[must_use]
70 pub fn wait_millis(wait: u64) -> Self {
71 Self {
72 create: None,
73 wait: Some(Duration::from_millis(wait)),
74 recycle: None,
75 }
76 }
77}
78
79impl Default for Timeouts {
81 fn default() -> Self {
83 Self {
84 create: None,
85 wait: None,
86 recycle: None,
87 }
88 }
89}
90
91#[derive(Debug)]
94pub enum CreatePoolError<C, B> {
95 Config(C),
97 Build(BuildError<B>),
99}
100
101impl<C, B> fmt::Display for CreatePoolError<C, B>
102where
103 C: fmt::Display,
104 B: fmt::Display,
105{
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 match self {
108 Self::Config(e) => write!(f, "Config: {}", e),
109 Self::Build(e) => write!(f, "Build: {}", e),
110 }
111 }
112}
113
114impl<C, B> std::error::Error for CreatePoolError<C, B>
115where
116 C: std::error::Error,
117 B: std::error::Error,
118{
119}