lockfree_object_pool/
mutex_reusable.rs

1use crate::mutex_object_pool::MutexObjectPool;
2use std::mem::ManuallyDrop;
3use std::ops::{Deref, DerefMut};
4
5/// Wrapper over T used by [`MutexObjectPool`].
6///
7/// Access is allowed with [`std::ops::Deref`] or [`std::ops::DerefMut`]
8/// # Example
9/// ```rust
10///  use lockfree_object_pool::MutexObjectPool;
11///
12///  let pool = MutexObjectPool::<u32>::new(
13///    ||  Default::default(),
14///    |v| {
15///      *v = 0;
16///    }
17///  );
18///  let mut item = pool.pull();
19///
20///  *item = 5;
21///  let work = *item * 5;
22/// ```
23pub struct MutexReusable<'a, T> {
24    pool: &'a MutexObjectPool<T>,
25    data: ManuallyDrop<T>,
26}
27
28impl<'a, T> MutexReusable<'a, T> {
29    /// Create new element
30    ///
31    /// # Arguments
32    /// * `pool` object pool owner
33    /// * `data` element to wrap
34    #[inline]
35    pub fn new(pool: &'a MutexObjectPool<T>, data: ManuallyDrop<T>) -> Self {
36        Self { pool, data }
37    }
38}
39
40impl<'a, T> DerefMut for MutexReusable<'a, T> {
41    #[inline]
42    fn deref_mut(&mut self) -> &mut Self::Target {
43        &mut self.data
44    }
45}
46
47impl<'a, T> Deref for MutexReusable<'a, T> {
48    type Target = T;
49
50    #[inline]
51    fn deref(&self) -> &Self::Target {
52        &self.data
53    }
54}
55
56impl<'a, T> Drop for MutexReusable<'a, T> {
57    #[inline]
58    fn drop(&mut self) {
59        let data = unsafe {
60            // SAFETY: self.data is never referenced again and it isn't dropped
61            ManuallyDrop::take(&mut self.data)
62        };
63        self.pool.attach(data);
64    }
65}