use std::future::Future;
use std::time::Instant;
#[derive(Debug, Clone, Copy)]
pub struct ObjectStatus {
created: Instant,
pub(crate) recycled: Option<Instant>,
pub(crate) recycle_count: usize,
}
impl Default for ObjectStatus {
fn default() -> Self {
Self {
created: Instant::now(),
recycled: None,
recycle_count: 0,
}
}
}
impl ObjectStatus {
pub fn created(&self) -> Instant {
self.created
}
pub fn last_used(&self) -> Instant {
self.recycled.unwrap_or(self.created)
}
pub fn recycle_count(&self) -> usize {
self.recycle_count
}
}
pub trait ManageObject: Send + Sync {
type Object: Send;
type Error: Send;
fn create(&self) -> impl Future<Output = Result<Self::Object, Self::Error>> + Send;
fn is_recyclable(
&self,
o: &mut Self::Object,
status: &ObjectStatus,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn on_detached(&self, _o: &mut Self::Object) {}
}
#[derive(Debug, Default, Clone, Copy)]
pub enum QueueStrategy {
#[default]
Fifo,
Lifo,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum RecycleCancelledStrategy {
#[default]
Detach,
ReturnToPool,
}