SendPtr<T>: Send wrapper around NonNull<T>
This commit is contained in:
		
							parent
							
								
									52fea2e306
								
							
						
					
					
						commit
						cd4c5467ba
					
				|  | @ -4,6 +4,7 @@ mod util { | |||
|         marker::PhantomData, | ||||
|         mem::ManuallyDrop, | ||||
|         num::NonZero, | ||||
|         ops::{Deref, DerefMut}, | ||||
|         ptr::NonNull, | ||||
|         sync::atomic::{AtomicPtr, Ordering}, | ||||
|     }; | ||||
|  | @ -30,6 +31,41 @@ mod util { | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     #[repr(transparent)] | ||||
|     #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||||
|     pub struct SendPtr<T>(NonNull<T>); | ||||
| 
 | ||||
|     impl<T> std::fmt::Pointer for SendPtr<T> { | ||||
|         fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||
|             <NonNull<T> as core::fmt::Pointer>::fmt(&self.0, f) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     unsafe impl<T> Send for SendPtr<T> {} | ||||
| 
 | ||||
|     impl<T> Deref for SendPtr<T> { | ||||
|         type Target = NonNull<T>; | ||||
| 
 | ||||
|         fn deref(&self) -> &Self::Target { | ||||
|             &self.0 | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     impl<T> DerefMut for SendPtr<T> { | ||||
|         fn deref_mut(&mut self) -> &mut Self::Target { | ||||
|             &mut self.0 | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     impl<T> SendPtr<T> { | ||||
|         pub fn new(ptr: *mut T) -> Option<Self> { | ||||
|             NonNull::new(ptr).map(Self) | ||||
|         } | ||||
|         pub unsafe fn new_unchecked(ptr: *mut T) -> Self { | ||||
|             unsafe { Self(NonNull::new_unchecked(ptr)) } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     #[repr(transparent)] | ||||
|     pub struct TaggedAtomicPtr<T, const BITS: usize>(AtomicPtr<()>, PhantomData<T>); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue