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