macro_rules! const_assert {
($name:ident, $($xs:expr),+ $(,)*) => {
#[allow(unknown_lints, clippy::eq_op)]
const $name: [(); 0 - !($($xs)&&+) as usize] = [];
};
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct PoolOpts {
constraints: PoolConstraints,
reset_connection: bool,
check_health: bool,
}
impl PoolOpts {
pub fn new() -> Self {
Self::default()
}
pub fn with_constraints(mut self, constraints: PoolConstraints) -> Self {
self.constraints = constraints;
self
}
pub fn constraints(&self) -> PoolConstraints {
self.constraints
}
pub fn with_reset_connection(mut self, reset_connection: bool) -> Self {
self.reset_connection = reset_connection;
self
}
pub fn reset_connection(&self) -> bool {
self.reset_connection
}
pub fn with_check_health(mut self, check_health: bool) -> Self {
self.check_health = check_health;
self
}
pub fn check_health(&self) -> bool {
self.check_health
}
}
impl Default for PoolOpts {
fn default() -> Self {
Self {
constraints: PoolConstraints::DEFAULT,
reset_connection: true,
check_health: true,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct PoolConstraints {
min: usize,
max: usize,
}
const_assert!(
_DEFAULT_POOL_CONSTRAINTS_ARE_CORRECT,
PoolConstraints::DEFAULT.min <= PoolConstraints::DEFAULT.max,
);
const_assert!(
_POOL_CONSTRAINTS_MAX_IS_NONZERO,
PoolConstraints::DEFAULT.max > 0
);
pub struct Assert<const L: usize, const R: usize>;
impl<const L: usize, const R: usize> Assert<L, R> {
pub const LEQ: usize = R - L;
}
#[allow(path_statements)]
pub const fn gte<const M: usize, const N: usize>() {
#[allow(clippy::no_effect)]
Assert::<M, N>::LEQ;
}
impl PoolConstraints {
pub const DEFAULT: PoolConstraints = PoolConstraints { min: 10, max: 100 };
pub fn new(min: usize, max: usize) -> Option<PoolConstraints> {
match (min, max) {
(0, 0) => None,
(min, max) if min <= max => Some(PoolConstraints { min, max }),
_ => None,
}
}
pub const fn new_const<const MIN: usize, const MAX: usize>() -> PoolConstraints {
gte::<MIN, MAX>();
assert!(MAX > 0);
PoolConstraints { min: MIN, max: MAX }
}
pub const fn min(&self) -> usize {
self.min
}
pub const fn max(&self) -> usize {
self.max
}
}
impl Default for PoolConstraints {
fn default() -> Self {
PoolConstraints::DEFAULT
}
}
impl From<PoolConstraints> for (usize, usize) {
fn from(PoolConstraints { min, max }: PoolConstraints) -> Self {
(min, max)
}
}