Struct Rc

Source
pub struct Rc<T: PtrEq + ?Sized>(/* private fields */);
Expand description

A single-threaded reference-counting pointer. ‘Rc’ stands for ‘Reference Counted’.

See the module-level documentation for more details.

The inherent methods of Rc are all associated functions, which means that you have to call them as e.g., Rc::get_mut(&mut value) instead of value.get_mut(). This avoids conflicts with methods of the inner type T.

Implementations§

Source§

impl<T: PtrEq> Rc<T>

Source

pub fn new(value: T) -> Rc<T>

Constructs a new Rc<T>.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq)]
struct Test(i64);
 
let five = Rc::new(Test(5));
Source

pub fn pin(value: T) -> Pin<Rc<T>>

Constructs a new Pin<Rc<T>>. If T does not implement Unpin, then value will be pinned in memory and unable to be moved.

Source

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

Returns the inner value, if the Rc has exactly one strong reference.

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

This will succeed even if there are outstanding weak references.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);

let x = Rc::new(Test(3));
assert_eq!(Rc::try_unwrap(x).unwrap().0, 3);

let x = Rc::new(Test(4));
let _y = Rc::clone(&x);
assert_eq!(Rc::try_unwrap(x).unwrap_err().0, 4);
Source§

impl<T: PtrEq + ?Sized> Rc<T>

Source

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

Consumes the Rc, returning the wrapped pointer.

To avoid a memory leak the pointer must be converted back to an Rc using Rc::from_raw.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);
 
let x = Rc::new(Test(42));
let x_ptr = Rc::into_raw(x);
assert_eq!(unsafe { &*x_ptr }.0, 42);
Source

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

Provides a raw pointer to the data.

The counts are not affected in any way and the Rc is not consumed. The pointer is valid for as long there are strong counts in the Rc.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);

let x = Rc::new(Test(42));
let y = Rc::clone(&x);
let x_ptr = Rc::as_ptr(&x);
assert_eq!(x_ptr, Rc::as_ptr(&y));
assert_eq!(unsafe { &*x_ptr }.0, 42);
Source

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

Constructs an Rc<T> from a raw pointer.

The raw pointer must have been previously returned by a call to Rc<U>::into_raw where U must have the same size and alignment as T. This is trivially true if U is T. Note that if U is not T but has the same size and alignment, this is basically like transmuting references of different types. See mem::transmute for more information on what restrictions apply in this case.

The user of from_raw has to make sure a specific value of T is only dropped once.

This function is unsafe because improper use may lead to memory unsafety, even if the returned Rc<T> is never accessed.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);
 
let x = Rc::new(Test(42));
let x_ptr = Rc::into_raw(x);

unsafe {
    // Convert back to an `Rc` to prevent leak.
    let x = Rc::from_raw(x_ptr);
    assert_eq!(x.0, 42);

    // Further calls to `Rc::from_raw(x_ptr)` would be memory-unsafe.
}

// The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
Source

pub fn downgrade(this: &Self) -> Weak<T>

Creates a new Weak pointer to this allocation.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);
 
let five = Rc::new(Test(5));

let weak_five = Rc::downgrade(&five);
Source

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

Gets the number of Weak pointers to this allocation.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);
 
let five = Rc::new(Test(5));
let _weak_five = Rc::downgrade(&five);

assert_eq!(1, Rc::weak_count(&five));
Source

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

Gets the number of strong (Rc) pointers to this allocation.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);
 
let five = Rc::new(Test(5));
let _also_five = Rc::clone(&five);

assert_eq!(2, Rc::strong_count(&five));
Source

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

Returns a mutable reference into the given Rc, if there are no other Rc or Weak pointers to the same allocation.

Returns None otherwise, because it is not safe to mutate a shared value.

See also make_mut, which will clone the inner value when there are other pointers.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug)]
struct Test(i64);
 
let mut x = Rc::new(Test(3));
*Rc::get_mut(&mut x).unwrap() = Test(4);
assert_eq!(x.0, 4);

let _y = Rc::clone(&x);
assert!(Rc::get_mut(&mut x).is_none());
Source§

impl<T: PtrEq + Clone> Rc<T>

Source

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

Makes a mutable reference into the given Rc.

If there are other Rc pointers to the same allocation, then make_mut will clone the inner value to a new allocation to ensure unique ownership. This is also referred to as clone-on-write.

If there are no other Rc pointers to this allocation, then Weak pointers to this allocation will be disassociated.

See also get_mut, which will fail rather than cloning.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug, Clone)]
struct Test(i64);

let mut data = Rc::new(Test(5));

Rc::make_mut(&mut data).0 += 1;         // Won't clone anything
let mut other_data = Rc::clone(&data);  // Won't clone inner data
Rc::make_mut(&mut data).0 += 1;         // Clones inner data
Rc::make_mut(&mut data).0 += 1;         // Won't clone anything
Rc::make_mut(&mut other_data).0 *= 2;   // Won't clone anything

// Now `data` and `other_data` point to different allocations.
assert_eq!(data.0, 8);
assert_eq!(other_data.0, 12);

Weak pointers will be disassociated:

use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Debug, Clone)]
struct Test(i64);

let mut data = Rc::new(Test(75));
let weak = Rc::downgrade(&data);

assert!(75 == data.0);
assert!(75 == weak.upgrade().unwrap().0);

Rc::make_mut(&mut data).0 += 1;

assert!(76 == data.0);
assert!(weak.upgrade().is_none());

Trait Implementations§

Source§

impl<T: PtrEq + ?Sized> AsRef<T> for Rc<T>

Source§

fn as_ref(&self) -> &T

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

impl<T: PtrEq + ?Sized> Borrow<T> for Rc<T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: PtrEq + ?Sized> Clone for Rc<T>

Source§

fn clone(&self) -> Rc<T>

Makes a clone of the Rc pointer.

This creates another pointer to the same allocation, increasing the strong reference count.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq)]
struct Test(i64);

let five = Rc::new(Test(5));

let _ = Rc::clone(&five);
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + PtrEq + ?Sized> Debug for Rc<T>

Source§

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

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

impl<T: PtrEq + Default> Default for Rc<T>

Source§

fn default() -> Rc<T>

Creates a new Rc<T>, with the Default value for T.

§Examples
use ptr_eq::rc::Rc;
use ptr_eq::PtrEq;
 
#[derive(PtrEq, Default)]
struct Test(i64);

let x: Rc<Test> = Default::default();
assert_eq!(x.0, 0);
Source§

impl<T: PtrEq + ?Sized> Deref for Rc<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: PtrEq> From<T> for Rc<T>

Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for Rc<[T]>
where [T]: PtrEq,

Source§

fn from(v: Vec<T>) -> Rc<[T]>

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for Rc<[T]>
where [T]: PtrEq,

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Rc<[T]>

Creates a value from an iterator. Read more
Source§

impl<T: PtrEq + ?Sized> Pointer for Rc<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Rc<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Rc<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> !Send for Rc<T>

§

impl<T> !Sync for Rc<T>

§

impl<T> Unpin for Rc<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Rc<T>
where T: RefUnwindSafe + ?Sized,

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