polars_utils/
sync.rs

1/// Utility that allows use to send pointers to another thread.
2/// This is better than going through `usize` as MIRI can follow these.
3#[derive(Debug, PartialEq, Eq)]
4#[repr(transparent)]
5pub struct SyncPtr<T>(*mut T);
6
7impl<T> SyncPtr<T> {
8    /// # Safety
9    ///
10    /// This will make a pointer sync and send.
11    /// Ensure that you don't break aliasing rules.
12    pub unsafe fn new(ptr: *mut T) -> Self {
13        Self(ptr)
14    }
15
16    pub fn from_const(ptr: *const T) -> Self {
17        Self(ptr as *mut T)
18    }
19
20    pub fn new_null() -> Self {
21        Self(std::ptr::null_mut())
22    }
23
24    #[inline(always)]
25    pub fn get(&self) -> *mut T {
26        self.0
27    }
28
29    pub fn is_null(&self) -> bool {
30        self.0.is_null()
31    }
32
33    /// # Safety
34    /// Derefs a raw pointer, no guarantees whatsoever.
35    pub unsafe fn deref_unchecked(&self) -> &'static T {
36        unsafe { &*(self.0 as *const T) }
37    }
38}
39
40impl<T> Copy for SyncPtr<T> {}
41impl<T> Clone for SyncPtr<T> {
42    fn clone(&self) -> SyncPtr<T> {
43        *self
44    }
45}
46unsafe impl<T> Sync for SyncPtr<T> {}
47unsafe impl<T> Send for SyncPtr<T> {}
48
49impl<T> From<*const T> for SyncPtr<T> {
50    fn from(value: *const T) -> Self {
51        Self::from_const(value)
52    }
53}