Struct sdd::Shared

source ·
pub struct Shared<T> { /* private fields */ }
Expand description

Shared is a reference-counted handle to an instance.

The instance is passed to the EBR garbage collector when the last strong reference is dropped.

Implementations§

source§

impl<T: 'static> Shared<T>

source

pub fn new(t: T) -> Self

Creates a new Shared.

The type of the instance must be determined at compile-time, must not contain non-static references, and must not be a non-static reference since the instance can, theoretically, survive the process. For instance, struct Disallowed<'l, T>(&'l T) is not allowed, because an instance of the type cannot outlive 'l whereas the garbage collector does not guarantee that the instance is dropped within 'l.

§Examples
use sdd::Shared;

let shared: Shared<usize> = Shared::new(31);
source§

impl<T> Shared<T>

source

pub unsafe fn new_unchecked(t: T) -> Self

Creates a new Shared without checking the lifetime of T.

§Safety

T::drop can be run after the last strong reference is dropped, therefore it is safe only if T::drop does not access short-lived data or std::mem::needs_drop is false for T. Otherwise, the instance must be manually dropped by invoking Self::drop_in_place before the lifetime of T is reached.

§Examples
use sdd::Shared;

let hello = String::from("hello");
let shared: Shared<&str> = unsafe { Shared::new_unchecked(hello.as_str()) };

assert!(unsafe { shared.drop_in_place() });
source

pub fn get_guarded_ptr<'g>(&self, _guard: &'g Guard) -> Ptr<'g, T>

Returns a Ptr to the instance that may live as long as the supplied Guard.

§Examples
use sdd::{Guard, Shared};

let shared: Shared<usize> = Shared::new(37);
let guard = Guard::new();
let ptr = shared.get_guarded_ptr(&guard);
drop(shared);

assert_eq!(*ptr.as_ref().unwrap(), 37);
source

pub fn get_guarded_ref<'g>(&self, _guard: &'g Guard) -> &'g T

Returns a reference to the instance that may live as long as the supplied Guard.

§Examples
use sdd::{Guard, Shared};

let shared: Shared<usize> = Shared::new(37);
let guard = Guard::new();
let ref_b = shared.get_guarded_ref(&guard);
drop(shared);

assert_eq!(*ref_b, 37);
source

pub unsafe fn get_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the instance if the Shared is holding the only strong reference.

§Safety

The method is unsafe since there can be a Ptr to the instance without holding a strong reference.

§Examples
use sdd::Shared;

let mut shared: Shared<usize> = Shared::new(38);
unsafe {
    *shared.get_mut().unwrap() += 1;
}
assert_eq!(*shared, 39);
source

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

Provides a raw pointer to the instance.

§Examples
use sdd::Shared;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;

let shared: Shared<usize> = Shared::new(10);
let shared_clone: Shared<usize> = shared.clone();

assert_eq!(shared.as_ptr(), shared_clone.as_ptr());
assert_eq!(unsafe { *shared.as_ptr() }, unsafe { *shared_clone.as_ptr() });
source

pub fn release(self, guard: &Guard) -> bool

Releases the strong reference by passing self to the given Guard.

Returns true if the last reference was released.

§Examples
use sdd::{Guard, Shared};

let shared: Shared<usize> = Shared::new(47);
let shared_clone = shared.clone();
let guard = Guard::new();
assert!(!shared.release(&guard));
assert!(shared_clone.release(&guard));
source

pub unsafe fn drop_in_place(self) -> bool

Drops the instance immediately if it has held the last reference to the instance.

Returns true if the instance was dropped.

§Safety

The caller must ensure that there is no Ptr pointing to the instance.

§Examples
use sdd::Shared;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;

static DROPPED: AtomicBool = AtomicBool::new(false);
struct T(&'static AtomicBool);
impl Drop for T {
    fn drop(&mut self) {
        self.0.store(true, Relaxed);
    }
}

let shared: Shared<T> = Shared::new(T(&DROPPED));
let shared_clone = shared.clone();

unsafe {
    assert!(!shared.drop_in_place());
    assert!(!DROPPED.load(Relaxed));
    assert!(shared_clone.drop_in_place());
    assert!(DROPPED.load(Relaxed));
}

Trait Implementations§

source§

impl<T> AsRef<T> for Shared<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T> Clone for Shared<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<T: Debug> Debug for Shared<T>

source§

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

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

impl<T> Deref for Shared<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> Drop for Shared<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'g, T> TryFrom<Ptr<'g, T>> for Shared<T>

§

type Error = Ptr<'g, T>

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

fn try_from(ptr: Ptr<'g, T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Send> Send for Shared<T>

source§

impl<T: Sync> Sync for Shared<T>

source§

impl<T: UnwindSafe> UnwindSafe for Shared<T>

Auto Trait Implementations§

§

impl<T> Freeze for Shared<T>

§

impl<T> !RefUnwindSafe for Shared<T>

§

impl<T> Unpin for Shared<T>

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.