Struct elysees::ArcRef[][src]

#[repr(transparent)]
pub struct ArcRef<'a, T: Erasable> { /* fields omitted */ }
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

Construct an [ArcRef<'a, T>]

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);

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.

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

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

Gets the number of Arc pointers to this allocation

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

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

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);

Construct an ArcRef<'a, T> from an 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);

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

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!

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));

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);

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);

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);

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);

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);

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);

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);

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

Performs the conversion.

Immutably borrows from an owned value. Read more

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

The resulting type after dereferencing.

Dereferences the value.

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Performs the conversion.

Feeds this value into the given Hasher. Read more

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

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 !=.

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

Formats the value using the given formatter.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Unerase this erased pointer. Read more

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

Turn this erasable pointer into an erased pointer. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

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