Struct Shared
pub struct Shared<'g, T: 'g + ?Sized + Pointable> { /* private fields */ }
A pointer to an object protected by the epoch GC.
The pointer is valid for use only during the lifetime 'g.
The pointer must be properly aligned. Since it is aligned, a tag can be stored into the unused least significant bits of the address.
Implementations
impl<'g, T> Shared<'g, T>
fn as_raw(&self) -> *const TConverts the pointer to a raw pointer (without the tag).
Examples
use ; use SeqCst; let o = new; let raw = &*o as *const _; let a = from; let guard = &pin; let p = a.load; assert_eq!; # unsafe // avoid leak
impl<'g, T: ?Sized + Pointable> Shared<'g, T>
fn null() -> Shared<'g, T>Returns a new null pointer.
Examples
use Shared; let p = null; assert!;fn is_null(&self) -> boolReturns
trueif the pointer is null.Examples
use ; use SeqCst; let a = null; let guard = &pin; assert!; a.store; assert!; # unsafe // avoid leakunsafe fn deref(&self) -> &'g TDereferences the pointer.
Returns a reference to the pointee that is valid during the lifetime
'g.Safety
Dereferencing a pointer is unsafe because it could be pointing to invalid memory.
Another concern is the possibility of data races due to lack of proper synchronization. For example, consider the following scenario:
- A thread creates a new object:
a.store(Owned::new(10), Relaxed) - Another thread reads it:
*a.load(Relaxed, guard).as_ref().unwrap()
The problem is that relaxed orderings don't synchronize initialization of the object with the read from the second thread. This is a data race. A possible solution would be to use
ReleaseandAcquireorderings.Examples
use ; use SeqCst; let a = new; let guard = &pin; let p = a.load; unsafe # unsafe // avoid leak- A thread creates a new object:
unsafe fn deref_mut(&mut self) -> &'g mut TDereferences the pointer.
Returns a mutable reference to the pointee that is valid during the lifetime
'g.Safety
-
There is no guarantee that there are no more threads attempting to read/write from/to the actual object at the same time.
The user must know that there are no concurrent accesses towards the object itself.
-
Other than the above, all safety concerns of
deref()applies here.
Examples
use ; use SeqCst; let a = new; let guard = &pin; let mut p = a.load; unsafe let p = a.load; unsafe # unsafe // avoid leak-
unsafe fn as_ref(&self) -> Option<&'g T>Converts the pointer to a reference.
Returns
Noneif the pointer is null, or else a reference to the object wrapped inSome.Safety
Dereferencing a pointer is unsafe because it could be pointing to invalid memory.
Another concern is the possibility of data races due to lack of proper synchronization. For example, consider the following scenario:
- A thread creates a new object:
a.store(Owned::new(10), Relaxed) - Another thread reads it:
*a.load(Relaxed, guard).as_ref().unwrap()
The problem is that relaxed orderings don't synchronize initialization of the object with the read from the second thread. This is a data race. A possible solution would be to use
ReleaseandAcquireorderings.Examples
use ; use SeqCst; let a = new; let guard = &pin; let p = a.load; unsafe # unsafe // avoid leak- A thread creates a new object:
unsafe fn into_owned(self) -> Owned<T>Takes ownership of the pointee.
Panics
Panics if this pointer is null, but only in debug mode.
Safety
This method may be called only if the pointer is valid and nobody else is holding a reference to the same object.
Examples
use ; use SeqCst; let a = new; unsafeunsafe fn try_into_owned(self) -> Option<Owned<T>>Takes ownership of the pointee if it is not null.
Safety
This method may be called only if the pointer is valid and nobody else is holding a reference to the same object, or if the pointer is null.
Examples
use ; use SeqCst; let a = new; unsafefn tag(&self) -> usizeReturns the tag stored within the pointer.
Examples
use ; use SeqCst; let a = from; let guard = &pin; let p = a.load; assert_eq!; # unsafe // avoid leakfn with_tag(&self, tag: usize) -> Shared<'g, T>Returns the same pointer, but tagged with
tag.tagis truncated to be fit into the unused bits of the pointer toT.Examples
use ; use SeqCst; let a = new; let guard = &pin; let p1 = a.load; let p2 = p1.with_tag; assert_eq!; assert_eq!; assert_eq!; # unsafe // avoid leak
Trait Implementations
impl<'g, T: ?Sized + Pointable> PartialEq for Shared<'g, T>
fn eq(&self, other: &Self) -> bool
impl<'g, T: ?Sized + Pointable> PartialOrd for Shared<'g, T>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
impl<T> From<*const T> for Shared<'_, T>
fn from(raw: *const T) -> SelfReturns a new pointer pointing to
raw.Panics
Panics if
rawis not properly aligned.Examples
use Shared; let p = from; assert!; # unsafe // avoid leak
impl<T: ?Sized + Pointable> Clone for Shared<'_, T>
fn clone(&self) -> Self
impl<T: ?Sized + Pointable> Copy for Shared<'_, T>
impl<T: ?Sized + Pointable> Debug for Shared<'_, T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: ?Sized + Pointable> Default for Shared<'_, T>
fn default() -> Self
impl<T: ?Sized + Pointable> Eq for Shared<'_, T>
impl<T: ?Sized + Pointable> Ord for Shared<'_, T>
fn cmp(&self, other: &Self) -> Ordering
impl<T: ?Sized + Pointable> Pointer for Shared<'_, T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: ?Sized + Pointable> Pointer<T> for Shared<'_, T>
fn into_usize(self) -> usizeunsafe fn from_usize(data: usize) -> Self
Auto Trait Implementations
impl<'g, T> !Send for Shared<'g, T>
impl<'g, T> !Sync for Shared<'g, T>
impl<'g, T> Freeze for Shared<'g, T>
where
PhantomData<(&'g (), *const T)>: Freeze,
T: ?Sized,
impl<'g, T> RefUnwindSafe for Shared<'g, T>
where
PhantomData<(&'g (), *const T)>: RefUnwindSafe,
T: ?Sized,
impl<'g, T> Unpin for Shared<'g, T>
where
PhantomData<(&'g (), *const T)>: Unpin,
T: ?Sized,
impl<'g, T> UnsafeUnpin for Shared<'g, T>
where
PhantomData<(&'g (), *const T)>: UnsafeUnpin,
T: ?Sized,
impl<'g, T> UnwindSafe for Shared<'g, T>
where
PhantomData<(&'g (), *const T)>: UnwindSafe,
T: ?Sized,
Blanket Implementations
impl<T> Any for Shared<'g, T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Shared<'g, T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Shared<'g, T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Shared<'g, T>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Shared<'g, T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Pointable for Shared<'g, T>
const ALIGN: usize = const ALIGN: usize = mem::align_of::<T>();;type Init = T;unsafe fn init(init: <T as Pointable>::Init) -> usizeunsafe fn deref<'a>(ptr: usize) -> &'a Tunsafe fn deref_mut<'a>(ptr: usize) -> &'a mut Tunsafe fn drop(ptr: usize)
impl<T> ToOwned for Shared<'g, T>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Shared<'g, T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Shared<'g, T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Shared<'g, T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>