deadpool/managed/
metrics.rs

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