lockfree_object_pool/linear_owned_reusable.rs
1use crate::linear_object_pool::LinearObjectPool;
2use crate::page::{Page, PageId};
3use std::ops::{Deref, DerefMut};
4use std::sync::Arc;
5
6/// Wrapper over T used by [`LinearObjectPool`].
7///
8/// Access is allowed with [`std::ops::Deref`] or [`std::ops::DerefMut`]
9/// # Example
10/// ```rust
11/// use lockfree_object_pool::LinearObjectPool;
12/// use std::sync::Arc;
13///
14/// let pool = Arc::new(LinearObjectPool::<u32>::new(
15/// || Default::default(),
16/// |v| {
17/// *v = 0;
18/// }
19/// ));
20/// let mut item = pool.pull_owned();
21///
22/// *item = 5;
23/// let work = *item * 5;
24/// ```
25pub struct LinearOwnedReusable<T> {
26 pool: Arc<LinearObjectPool<T>>,
27 page_id: PageId,
28 page: *const Page<T>,
29}
30
31impl<T> LinearOwnedReusable<T> {
32 /// Create new element
33 ///
34 /// # Arguments
35 /// * `pool` object pool owner
36 /// * `page_id` page id
37 /// * `page` page that contains data
38 /// # Safety
39 /// * `page` has to be a valid pointer to a page in `pool`
40 /// * `pool_id` has to be a valid id for `page`
41 #[inline]
42 pub(crate) unsafe fn new(
43 pool: Arc<LinearObjectPool<T>>,
44 page_id: PageId,
45 page: &Page<T>,
46 ) -> Self {
47 Self {
48 pool,
49 page_id,
50 page,
51 }
52 }
53}
54
55impl<T> DerefMut for LinearOwnedReusable<T> {
56 #[inline]
57 fn deref_mut(&mut self) -> &mut Self::Target {
58 unsafe {
59 // SAFETY: there exists only this `LinearOwnedReusable` with this page_id
60 self.page.as_ref().unwrap().get_mut(&self.page_id)
61 }
62 }
63}
64
65impl<T> Deref for LinearOwnedReusable<T> {
66 type Target = T;
67
68 #[inline]
69 fn deref(&self) -> &Self::Target {
70 unsafe {
71 // SAFETY: there exists only this `LinearOwnedReusable` with this page_id
72 self.page.as_ref().unwrap().get(&self.page_id)
73 }
74 }
75}
76
77impl<T> Drop for LinearOwnedReusable<T> {
78 #[inline]
79 fn drop(&mut self) {
80 unsafe {
81 // SAFETY: there exists only this `LinearOwnedReusable` with this page_id
82 let page = self.page.as_ref().unwrap();
83 (self.pool.get_reset_callback())(page.get_mut(&self.page_id));
84 page.free(&self.page_id);
85 }
86 }
87}
88
89unsafe impl<T: Send> Send for LinearOwnedReusable<T> {} // SAFETY: sending the data is allowed if it's Send
90unsafe impl<T: Send> Sync for LinearOwnedReusable<T> {} // SAFETY: the Mutex manages synchronization so only Send is required