pub struct Shared<'g, T>where
    T: 'g + Pointable + ?Sized,
{ /* private fields */ }
Expand description

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

Converts the pointer to a raw pointer (without the tag).

Examples
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let o = Owned::new(1234);
let raw = &*o as *const _;
let a = Atomic::from(o);

let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
assert_eq!(p.as_raw(), raw);

Returns a new null pointer.

Examples
use crossbeam_epoch::Shared;

let p = Shared::<i32>::null();
assert!(p.is_null());

Returns true if the pointer is null.

Examples
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::null();
let guard = &epoch::pin();
assert!(a.load(SeqCst, guard).is_null());
a.store(Owned::new(1234), SeqCst);
assert!(!a.load(SeqCst, guard).is_null());

Dereferences 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:

  1. A thread creates a new object: a.store(Owned::new(10), Relaxed)
  2. 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 Release and Acquire orderings.

Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
unsafe {
    assert_eq!(p.deref(), &1234);
}

Dereferences 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 crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(vec![1, 2, 3, 4]);
let guard = &epoch::pin();

let mut p = a.load(SeqCst, guard);
unsafe {
    assert!(!p.is_null());
    let b = p.deref_mut();
    assert_eq!(b, &vec![1, 2, 3, 4]);
    b.push(5);
    assert_eq!(b, &vec![1, 2, 3, 4, 5]);
}

let p = a.load(SeqCst, guard);
unsafe {
    assert_eq!(p.deref(), &vec![1, 2, 3, 4, 5]);
}

Converts the pointer to a reference.

Returns None if the pointer is null, or else a reference to the object wrapped in Some.

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:

  1. A thread creates a new object: a.store(Owned::new(10), Relaxed)
  2. 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 Release and Acquire orderings.

Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
unsafe {
    assert_eq!(p.as_ref(), Some(&1234));
}

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 crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
unsafe {
    let guard = &epoch::unprotected();
    let p = a.load(SeqCst, guard);
    drop(p.into_owned());
}

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 crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
unsafe {
    let guard = &epoch::unprotected();
    let p = a.load(SeqCst, guard);
    if let Some(x) = p.try_into_owned() {
        drop(x);
    }
}

Returns the tag stored within the pointer.

Examples
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::<u64>::from(Owned::new(0u64).with_tag(2));
let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
assert_eq!(p.tag(), 2);

Returns the same pointer, but tagged with tag. tag is truncated to be fit into the unused bits of the pointer to T.

Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(0u64);
let guard = &epoch::pin();
let p1 = a.load(SeqCst, guard);
let p2 = p1.with_tag(2);

assert_eq!(p1.tag(), 0);
assert_eq!(p2.tag(), 2);
assert_eq!(p1.as_raw(), p2.as_raw());

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Returns a new pointer pointing to raw.

Panics

Panics if raw is not properly aligned.

Examples
use crossbeam_epoch::Shared;

let p = Shared::from(Box::into_raw(Box::new(1234)) as *const _);
assert!(!p.is_null());

Returns a new atomic pointer pointing to ptr.

Examples
use crossbeam_epoch::{Atomic, Shared};

let a = Atomic::<i32>::from(Shared::<i32>::null());
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Returns the machine representation of the pointer.
Returns a new pointer pointing to the tagged pointer data. Read more
Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Return the T [ShaderType] for self. When used in [AsBindGroup] derives, it is safe to assume that all images in self exist. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more
Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more
Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more
Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Creates Self using data from the given [World]
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more