ArcRef

Struct ArcRef 

Source
pub struct ArcRef<'a, T: Erasable> { /* private fields */ }
Expand description

An atomically reference counted shared pointer, which may hold either exactly 0 references (in which case it is analogous to an ArcBorrow) or 1 (in which case it is analogous to an Arc)

Implementations§

Source§

impl<'a, T: Erasable> ArcRef<'a, T>

Source

pub fn new(data: T) -> Self

Construct an ArcRef<'a, T>

Source

pub fn try_unwrap(this: Self) -> Result<T, Self>

Returns the inner value, if the ArcRef is owned and has exactly one strong reference.

Otherwise, an Err is returned with the same ArcRef that was passed in.

§Examples
use elysees::ArcRef;

let x = ArcRef::new(3);
assert_eq!(ArcRef::try_unwrap(x), Ok(3));

let x = ArcRef::new(4);
let _y = ArcRef::clone(&x);
assert_eq!(*ArcRef::try_unwrap(x).unwrap_err(), 4);
Source

pub fn make_mut(this: &mut Self) -> &mut T
where T: Clone,

Makes a mutable reference to the ArcRef, cloning if necessary.

This is similar to ArcRef::make_mut from the standard library.

If this ArcRef is uniquely owned, make_mut() will provide a mutable reference to the contents. If not, make_mut() will create a new ArcRef with a copy of the contents, update this to point to it, and provide a mutable reference to its contents.

This is useful for implementing copy-on-write schemes where you wish to avoid copying things if your ArcRef is not shared.

Source

pub fn get_mut(this: &mut Self) -> Option<&mut T>

Provides mutable access to the contents if the ArcRef is uniquely owned.

Source

pub fn is_unique(this: &Self) -> bool

Whether or not the ArcRef is uniquely owned (is the refcount 1, and is ArcBorrow itself owned?).

Source

pub fn count(this: &Self) -> usize

Gets the number of Arc pointers to this allocation

Source

pub fn load_count(this: &Self, order: Ordering) -> usize

Gets the number of Arc pointers to this allocation, with a given load ordering

Source

pub fn try_unique(this: Self) -> Result<ArcBox<T>, Self>

Returns an ArcBox if the ArcRef has exactly one strong, owned reference.

Otherwise, an Err is returned with the same ArcRef that was passed in.

§Examples
use elysees::{ArcRef, ArcBox};

let x = ArcRef::new(3);
assert_eq!(ArcBox::into_inner(ArcRef::try_unique(x).unwrap()), 3);

let x = ArcRef::new(4);
let _y = ArcRef::clone(&x);
assert_eq!(
    *ArcRef::try_unique(x).map(ArcBox::into_inner).unwrap_err(),
    4,
);
Source

pub fn from_arc(arc: Arc<T>) -> Self

Construct an ArcRef<'a, T> from an Arc<T>

§Examples
use elysees::{Arc, ArcRef};

let x = Arc::new(3);
let y = ArcRef::from_arc(x.clone());
assert_eq!(ArcRef::count(&y), 2);
Source

pub fn from_borrow(arc: ArcBorrow<'a, T>) -> Self

Construct an ArcRef<'a, T> from an ArcBorrow<'a, T>

Source

pub fn try_into_arc(this: Self) -> Result<Arc<T>, ArcBorrow<'a, T>>

Try to convert this ArcRef<'a, T> into an Arc<T> if owned; otherwise, return it as an ArcBorrow

§Examples
use elysees::ArcRef;

let x = ArcRef::new(3);
assert_eq!(*ArcRef::try_into_arc(x.clone()).unwrap(), 3);
Source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Test pointer equality between the two ArcRefs, i.e. they must be the same allocation

Source

pub fn leak(this: ArcRef<'_, T>) -> ArcBorrow<'static, T>

Leak this ArcRef, getting an ArcBorrow<'static, T>

You can call the get method on the returned ArcBorrow to get an &'static T. Note that using this can (obviously) cause memory leaks!

Source

pub fn is_owned(this: &Self) -> bool

Get whether this ArcRef is owned

§Examples
use elysees::ArcRef;

let x = ArcRef::new(3);
assert!(ArcRef::is_owned(&x));
let y = x.clone();
assert!(ArcRef::is_owned(&y));
let z = ArcRef::into_borrow(&x);
assert!(!ArcRef::is_owned(&z));
Source

pub fn borrow_arc(this: &'a Self) -> ArcBorrow<'a, T>

Borrow this as an ArcBorrow. This does not bump the refcount.

§Examples
use elysees::{ArcBorrow, ArcRef};

let x: ArcRef<u64> = ArcRef::new(3);
assert_eq!(ArcRef::count(&x), 1);
let y: ArcBorrow<u64> = ArcRef::borrow_arc(&x);
assert_eq!(ArcRef::as_ptr(&x), ArcBorrow::into_raw(y));
assert_eq!(ArcRef::count(&x), 1);
assert_eq!(ArcBorrow::count(y), 1);
Source

pub fn into_arc(this: ArcRef<'a, T>) -> Arc<T>

Get this as an Arc, bumping the refcount if necessary.

§Examples
use elysees::{Arc, ArcRef};

let x = ArcRef::new(3);
let y = ArcRef::into_borrow(&x);
assert_eq!(ArcRef::as_ptr(&x), ArcRef::as_ptr(&y));
assert_eq!(ArcRef::count(&x), 1);
assert_eq!(ArcRef::count(&y), 1);
let z = ArcRef::into_arc(y);
assert_eq!(ArcRef::as_ptr(&x), Arc::as_ptr(&z));
assert_eq!(ArcRef::count(&x), 2);
assert_eq!(Arc::count(&z), 2);
let w = ArcRef::into_arc(x);
assert_eq!(Arc::count(&w), 2);
assert_eq!(Arc::count(&z), 2);
Source

pub fn clone_arc(this: &'a Self) -> Arc<T>

Clone this as an Arc.

§Examples
use elysees::{Arc, ArcRef};

let x: ArcRef<u64> = ArcRef::new(3);
assert_eq!(ArcRef::count(&x), 1);
let y: Arc<u64> = ArcRef::clone_arc(&x);
assert_eq!(ArcRef::as_ptr(&x), Arc::as_ptr(&y));
assert_eq!(ArcRef::count(&x), 2);
assert_eq!(Arc::count(&y), 2);
Source

pub fn into_owned(this: Self) -> ArcRef<'static, T>

Get this as an owned ArcRef, with the 'static lifetime

§Examples
use elysees::ArcRef;

let x = ArcRef::new(7);
assert_eq!(ArcRef::count(&x), 1);
let y = ArcRef::into_borrow(&x);
assert_eq!(ArcRef::count(&x), 1);
assert_eq!(ArcRef::count(&y), 1);
let z = ArcRef::into_owned(y);
assert_eq!(ArcRef::as_ptr(&x), ArcRef::as_ptr(&z));
assert_eq!(ArcRef::count(&x), 2);
assert_eq!(ArcRef::count(&z), 2);
Source

pub fn into_borrow(this: &'a ArcRef<'a, T>) -> ArcRef<'a, T>

Borrow this as an ArcRef. This does not bump the refcount.

§Examples
use elysees::ArcRef;

let x = ArcRef::new(8);
assert_eq!(ArcRef::count(&x), 1);
let y = ArcRef::into_borrow(&x);
assert_eq!(ArcRef::as_ptr(&x), ArcRef::as_ptr(&y));
assert_eq!(ArcRef::count(&x), 1);
assert_eq!(ArcRef::count(&y), 1);
Source

pub fn clone_into_owned(this: &Self) -> ArcRef<'static, T>

Clone this into an owned ArcRef, with the 'static lifetime

§Examples
use elysees::ArcRef;

let x = ArcRef::new(7);
assert_eq!(ArcRef::count(&x), 1);
let y = ArcRef::into_borrow(&x);
assert_eq!(ArcRef::count(&x), 1);
assert_eq!(ArcRef::count(&y), 1);
let z = ArcRef::clone_into_owned(&y);
assert_eq!(ArcRef::as_ptr(&x), ArcRef::as_ptr(&z));
assert_eq!(ArcRef::count(&x), 2);
assert_eq!(ArcRef::count(&y), 2);
assert_eq!(ArcRef::count(&z), 2);
Source

pub fn into_raw(this: Self) -> *const T

Get the internal pointer of an ArcBorrow. This does not bump the refcount.

§Examples
use elysees::{Arc, ArcRef};

let x = ArcRef::new(7);
assert_eq!(ArcRef::count(&x), 1);
let x_ = x.clone();
assert_eq!(ArcRef::count(&x), 2);
let p = ArcRef::into_raw(x_);
assert_eq!(ArcRef::count(&x), 2);
assert_eq!(ArcRef::as_ptr(&x), p);
let y = unsafe { Arc::from_raw(p) };
assert_eq!(ArcRef::as_ptr(&x), Arc::as_ptr(&y));
assert_eq!(ArcRef::count(&x), 2);
std::mem::drop(y);
assert_eq!(ArcRef::count(&x), 1);
Source

pub fn as_ptr(this: &Self) -> *const T

Get the internal pointer of an ArcBorrow. This does not bump the refcount.

§Examples
use elysees::ArcRef;
let x = ArcRef::new(7);
assert_eq!(ArcRef::count(&x), 1);
let p = ArcRef::as_ptr(&x);
assert_eq!(ArcRef::count(&x), 1);

Trait Implementations§

Source§

impl<'a, T: Erasable> AsRef<T> for ArcRef<'a, T>

Source§

fn as_ref(&self) -> &T

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

impl<'a, T: Erasable> Borrow<T> for ArcRef<'a, T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'a, T: Erasable> Clone for ArcRef<'a, T>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<'a, T: Erasable + Debug> Debug for ArcRef<'a, T>

Source§

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

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

impl<'a, T: Erasable + Default> Default for ArcRef<'a, T>

Source§

fn default() -> ArcRef<'a, T>

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

impl<'a, T: Erasable> Deref for ArcRef<'a, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'a, 'de, T: Deserialize<'de>> Deserialize<'de> for ArcRef<'a, T>

Source§

fn deserialize<D>(deserializer: D) -> Result<ArcRef<'a, T>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, T: Erasable + Display> Display for ArcRef<'a, T>

Source§

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

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

impl<'a, T: Erasable> Drop for ArcRef<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T> From<T> for ArcRef<'a, T>

Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: Erasable + Hash> Hash for ArcRef<'a, T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Erasable + Ord> Ord for ArcRef<'a, T>

Source§

fn cmp(&self, other: &ArcRef<'a, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, 'b, T: Erasable, U: Erasable + PartialEq<T>> PartialEq<ArcRef<'a, T>> for ArcRef<'b, U>

Source§

fn eq(&self, other: &ArcRef<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &ArcRef<'a, T>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b, T: Erasable, U: Erasable + PartialOrd<T>> PartialOrd<ArcRef<'a, T>> for ArcRef<'b, U>

Source§

fn partial_cmp(&self, other: &ArcRef<'a, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &ArcRef<'a, T>) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &ArcRef<'a, T>) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &ArcRef<'a, T>) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &ArcRef<'a, T>) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T: Erasable> Pointer for ArcRef<'a, T>

Source§

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

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

impl<'a, T: Serialize> Serialize for ArcRef<'a, T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'a, T: Erasable> CloneStableDeref for ArcRef<'a, T>

Source§

impl<'a, T: Erasable + Eq> Eq for ArcRef<'a, T>

Source§

impl<'a, T: Erasable + Sync + Send> Send for ArcRef<'a, T>

Source§

impl<'a, T: Erasable> StableDeref for ArcRef<'a, T>

Source§

impl<'a, T: Erasable + Sync + Send> Sync for ArcRef<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for ArcRef<'a, T>

§

impl<'a, T> RefUnwindSafe for ArcRef<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Unpin for ArcRef<'a, T>

§

impl<'a, T> UnwindSafe for ArcRef<'a, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T, A, P> Access<T> for P
where A: Access<T> + ?Sized, P: Deref<Target = A>,

Source§

type Guard = <A as Access<T>>::Guard

A guard object containing the value and keeping it alive. Read more
Source§

fn load(&self) -> <P as Access<T>>::Guard

The loading method. Read more
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, A> DynAccess<T> for A
where A: Access<T>, <A as Access<T>>::Guard: 'static,

Source§

fn load(&self) -> DynGuard<T>

The equivalent of Access::load.
Source§

impl<T> Erasable for T

Source§

const ACK_1_1_0: bool = true

Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more
Source§

unsafe fn unerase(this: NonNull<Erased>) -> NonNull<T>

Unerase this erased pointer. Read more
Source§

fn erase(this: NonNull<Self>) -> NonNull<Erased>

Turn this erasable pointer into an erased pointer. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,