deadpool/managed/manager.rs
1use std::future::Future;
2
3use crate::managed::{Metrics, RecycleError};
4
5/// Manager responsible for creating new [`super::Object`]s or recycling existing ones.
6pub trait Manager: Sync + Send {
7 /// Type of [`super::Object`]s that this [`Manager`] creates and recycles.
8 type Type: Send;
9 /// Error that this [`Manager`] can return when creating and/or recycling
10 /// [`super::Object`]s.
11 type Error: Send;
12
13 /// Creates a new instance of [`Manager::Type`].
14 fn create(&self) -> impl Future<Output = Result<Self::Type, Self::Error>> + Send;
15
16 /// Tries to recycle an instance of [`Manager::Type`].
17 ///
18 /// # Errors
19 ///
20 /// Returns [`Manager::Error`] if the instance couldn't be recycled.
21 fn recycle(
22 &self,
23 obj: &mut Self::Type,
24 metrics: &Metrics,
25 ) -> impl Future<Output = RecycleResult<Self::Error>> + Send;
26
27 /// Detaches an instance of [`Manager::Type`] from this [`Manager`].
28 ///
29 /// This method is called when using the [`super::Object::take()`] method for
30 /// removing an [`super::Object`] from a [`super::Pool`]. If the [`Manager`] doesn't hold
31 /// any references to the handed out [`super::Object`]s then the default
32 /// implementation can be used which does nothing.
33 fn detach(&self, _obj: &mut Self::Type) {}
34}
35
36/// Result type of the [`Manager::recycle()`] method.
37pub type RecycleResult<E> = Result<(), RecycleError<E>>;