Struct Ptr

Source
pub struct Ptr<'scope, T: 'scope> { /* private fields */ }
Expand description

A pointer to an object protected by the epoch GC.

The pointer is valid for use only within 'scope.

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§

Source§

impl<'scope, T> Ptr<'scope, T>

Source

pub unsafe fn destroy(self)

Source

pub fn null() -> Self

Returns a new null pointer.

§Examples
use coco::epoch::Ptr;

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

pub unsafe fn from_raw(raw: *const T) -> Self

Returns a new pointer initialized with raw.

§Panics

Panics if raw is not properly aligned.

§Examples
use coco::epoch::Ptr;

let p = unsafe { Ptr::from_raw(Box::into_raw(Box::new(1234))) };
assert!(!p.is_null());
Source

pub fn is_null(&self) -> bool

Returns true if the pointer is null.

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

let a = Atomic::null();
epoch::pin(|scope| {
    assert!(a.load(SeqCst, scope).is_null());
    a.store_owned(Owned::new(1234), SeqCst);
    assert!(!a.load(SeqCst, scope).is_null());
});
Source

pub fn as_raw(&self) -> *const T

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

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

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

epoch::pin(|scope| {
    let p = a.load(SeqCst, scope);
    assert_eq!(p.as_raw(), raw);
});
Source

pub unsafe fn deref(&self) -> &'scope T

Dereferences the pointer.

Returns a reference to the pointee that is valid in 'scope.

§Safety

Dereferencing a pointer to an invalid object is not a concern, since invalid Ptrs can only be constructed via other unsafe functions.

However, this method doesn’t check whether the pointer is null, so dereferencing a null pointer is unsafe.

Another source of unsafety is the possibility of unsynchronized reads to the objects. For example, the following scenario is unsafe:

  • A thread stores a new object: a.store_owned(Owned::new(10), Relaxed)
  • Another thread reads it: *a.load(Relaxed, scope).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 (or stronger).

§Examples
use coco::epoch::{self, Atomic};
use std::sync::atomic::Ordering::SeqCst;

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

pub unsafe fn as_ref(&self) -> Option<&'scope T>

Converts the pointer to a reference.

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

§Safety

This method checks whether the pointer is null, and if not, assumes that it’s pointing to a valid object. However, this is not considered a source of unsafety because invalid Ptrs can only be constructed via other unsafe functions.

The only source of unsafety is the possibility of unsynchronized reads to the objects. For example, the following scenario is unsafe:

  • A thread stores a new object: a.store_owned(Owned::new(10), Relaxed)
  • Another thread reads it: *a.load(Relaxed, scope).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 (or stronger).

§Examples
use coco::epoch::{self, Atomic};
use std::sync::atomic::Ordering::SeqCst;

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

pub fn tag(&self) -> usize

Returns the tag stored within the pointer.

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

let a = Atomic::from_owned(Owned::new(0u32).with_tag(3));
epoch::pin(|scope| {
    let p = a.load(SeqCst, scope);
    assert_eq!(p.tag(), 3);
});
Source

pub fn with_tag(&self, tag: usize) -> Self

Returns the same pointer, but tagged with tag.

§Examples
use coco::epoch::{self, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(0u32);
epoch::pin(|scope| {
    let p1 = a.load(SeqCst, scope);
    let p2 = p1.with_tag(3);

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

Trait Implementations§

Source§

impl<'scope, T> Clone for Ptr<'scope, T>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'scope, T: Debug + 'scope> Debug for Ptr<'scope, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'scope, T> Default for Ptr<'scope, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'scope, T> Copy for Ptr<'scope, T>

Auto Trait Implementations§

§

impl<'scope, T> Freeze for Ptr<'scope, T>

§

impl<'scope, T> RefUnwindSafe for Ptr<'scope, T>
where T: RefUnwindSafe,

§

impl<'scope, T> Send for Ptr<'scope, T>
where T: Sync,

§

impl<'scope, T> Sync for Ptr<'scope, T>
where T: Sync,

§

impl<'scope, T> Unpin for Ptr<'scope, T>

§

impl<'scope, T> UnwindSafe for Ptr<'scope, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.