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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
use crate::tag::{read_tag, set_tag, strip, Tag}; use std::marker::PhantomData; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Shared<'shield, V, T> where V: 'shield, T: Tag, { pub(crate) data: usize, _m0: PhantomData<&'shield ()>, _m1: PhantomData<V>, _m2: PhantomData<T>, } impl<'shield, V, T> Shared<'shield, V, T> where V: 'shield, T: Tag, { pub fn null() -> Self { Self::from_raw(0) } pub unsafe fn from_ptr(ptr: *mut V) -> Self { Self::from_raw(ptr as usize) } pub fn from_raw(data: usize) -> Self { Self { data, _m0: PhantomData, _m1: PhantomData, _m2: PhantomData, } } pub fn into_raw(self) -> usize { self.data } pub fn as_ptr(&self) -> *mut V { strip::<T>(self.data) as *mut V } pub unsafe fn as_ref(&self) -> Option<&'shield V> { self.as_ptr().as_ref() } pub unsafe fn as_mut_ref(&mut self) -> Option<&'shield mut V> { let ptr = self.as_ptr(); if !ptr.is_null() { Some(&mut *ptr) } else { None } } pub unsafe fn as_ref_unchecked(&self) -> &'shield V { &*self.as_ptr() } pub unsafe fn as_mut_ref_unchecked(&mut self) -> &'shield mut V { &mut *self.as_ptr() } pub fn is_null(&self) -> bool { self.as_ptr().is_null() } pub fn tag(&self) -> T { let bits = read_tag::<T>(self.data); Tag::deserialize(bits) } pub fn with_tag(&self, tag: T) -> Self { let bits = tag.serialize(); let data = set_tag::<T>(self.data, bits); Self::from_raw(data) } }