easy_pool/
pool_mutex.rs

1use std::sync::Arc;
2
3use parking_lot::Mutex;
4
5use crate::{Clear, PoolObjectContainer, PoolType};
6
7#[derive(Debug)]
8pub struct PoolMutex<T> {
9    pub(crate) values: Mutex<Vec<T>>,
10    pub(crate) max_size: usize,
11}
12
13impl<T> PoolMutex<T>
14where
15    T: Clear,
16{
17    pub fn new() -> Self {
18        Self::with_config(0, 4096)
19    }
20
21    pub fn with_config(capacity: usize, max_size: usize) -> Self {
22        Self {
23            values: Mutex::new(Vec::with_capacity(capacity)),
24            max_size,
25        }
26    }
27
28    // Create an object (default).
29    #[inline]
30    pub fn create(self: &Arc<Self>) -> PoolObjectContainer<T>
31    where
32        T: Clear + Default,
33    {
34        self.create_with(|| Default::default())
35    }
36
37    /// Create an object.
38    /// The FnOnce that you pass as an argument does not affect the object if it is already present in the pool.
39    /// It is interesting for example when creating a "vector" to specify a capacity.
40    pub fn create_with<F: FnOnce() -> T>(self: &Arc<Self>, f: F) -> PoolObjectContainer<T> {
41        let val = self.values.lock().pop().unwrap_or_else(f);
42        PoolObjectContainer::new(val, PoolType::Mutex(Arc::clone(&self)))
43    }
44
45    #[inline]
46    pub fn len(&self) -> usize {
47        self.values.lock().len()
48    }
49}
50
51impl<T: Clear> Default for PoolMutex<T> {
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test() {
63        let pool = Arc::new(PoolMutex::<Vec<u8>>::new());
64        let mut new_vec = pool.create();
65        new_vec.extend_from_slice(&[0, 0, 0, 0, 1, 1]);
66        let capacity = new_vec.capacity();
67        drop(new_vec);
68        assert!(!pool.values.lock().is_empty());
69        let new_vec = pool.create();
70        assert!(new_vec.capacity() > 0 && new_vec.capacity() == capacity);
71        assert!(new_vec.is_empty());
72        assert!(pool.values.lock().is_empty());
73        drop(new_vec);
74    }
75
76    #[test]
77    fn test_create_with() {
78        let pool = Arc::new(PoolMutex::<Vec<u8>>::new());
79        let r = pool.create_with(|| Vec::with_capacity(4096));
80        assert_eq!(r.capacity(), 4096);
81    }
82
83    #[test]
84    fn test_create() {
85        let pool = Arc::new(PoolMutex::<Vec<u8>>::new());
86        let r = pool.create();
87        assert_eq!(r.capacity(), Vec::<u8>::default().capacity());
88    }
89
90    #[test]
91    fn test_len() {
92        let pool = Arc::new(PoolMutex::<Vec<u8>>::new());
93        assert_eq!(pool.len(), 0);
94        let new_vec = pool.create();
95        drop(new_vec);
96        assert_eq!(pool.len(), 1);
97    }
98}
99
100impl Clear for String {
101    #[inline]
102    fn clear(&mut self) {
103        self.clear()
104    }
105}
106
107impl<T> Clear for Vec<T> {
108    #[inline]
109    fn clear(&mut self) {
110        self.clear();
111        debug_assert!(self.is_empty());
112    }
113}