use crate::mutex_object_pool::MutexObjectPool;
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
pub struct MutexReusable<'a, T> {
pool: &'a MutexObjectPool<T>,
data: ManuallyDrop<T>,
}
impl<'a, T> MutexReusable<'a, T> {
#[inline]
pub fn new(pool: &'a MutexObjectPool<T>, data: ManuallyDrop<T>) -> Self {
Self { pool, data }
}
}
impl<'a, T> DerefMut for MutexReusable<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<'a, T> Deref for MutexReusable<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<'a, T> Drop for MutexReusable<'a, T> {
#[inline]
fn drop(&mut self) {
let data = unsafe {
ManuallyDrop::take(&mut self.data)
};
self.pool.attach(data);
}
}