lockfree_object_pool/
mutex_reusable.rs
1use crate::mutex_object_pool::MutexObjectPool;
2use std::mem::ManuallyDrop;
3use std::ops::{Deref, DerefMut};
4
5pub struct MutexReusable<'a, T> {
24 pool: &'a MutexObjectPool<T>,
25 data: ManuallyDrop<T>,
26}
27
28impl<'a, T> MutexReusable<'a, T> {
29 #[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 ManuallyDrop::take(&mut self.data)
62 };
63 self.pool.attach(data);
64 }
65}