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