use crate::error::Result;
pub trait Poolable {
#[inline]
fn on_acquire(&mut self) {}
#[inline]
fn on_release(&mut self) {}
}
#[doc(hidden)]
pub trait Pool<T> {
fn allocate(&self, value: T) -> Result<Self::Handle>
where
Self::Handle: Sized;
type Handle;
}
#[cfg(feature = "stats")]
pub trait PoolStats {
fn statistics(&self) -> crate::stats::PoolStatistics;
fn reset_statistics(&self);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn poolable_default_impl() {
struct TestType {
value: i32,
}
impl Poolable for TestType {}
let mut obj = TestType { value: 42 };
obj.on_acquire();
obj.on_release();
assert_eq!(obj.value, 42);
}
#[test]
fn poolable_custom_impl() {
struct CustomType {
counter: i32,
}
impl Poolable for CustomType {
fn on_acquire(&mut self) {
self.counter = 0;
}
fn on_release(&mut self) {
self.counter = -1;
}
}
let mut obj = CustomType { counter: 100 };
obj.on_acquire();
assert_eq!(obj.counter, 0);
obj.counter = 50;
obj.on_release();
assert_eq!(obj.counter, -1);
}
}