deadpool/managed/
metrics.rs

1use std::time::{Duration, Instant};
2
3/// Statistics regarding an object returned by the pool
4#[derive(Clone, Copy, Debug)]
5#[must_use]
6pub struct Metrics {
7    /// The instant when this object was created
8    pub created: Instant,
9    /// The instant when this object was last used
10    pub recycled: Option<Instant>,
11    /// The number of times the objects was recycled
12    pub recycle_count: usize,
13}
14
15impl Metrics {
16    /// Access the age of this object
17    pub fn age(&self) -> Duration {
18        self.created.elapsed()
19    }
20    /// Get the time elapsed when this object was last used
21    pub fn last_used(&self) -> Duration {
22        self.recycled.unwrap_or(self.created).elapsed()
23    }
24}
25
26impl Default for Metrics {
27    fn default() -> Self {
28        Self {
29            created: Instant::now(),
30            recycled: None,
31            recycle_count: 0,
32        }
33    }
34}