Struct wrc::WRC [] [src]

pub struct WRC<T: ?Sized> { /* fields omitted */ }

WRC

A thread-safe weight reference counting smart-pointer for Rust.

Weighted Reference Counting

By using weights instead of direct reference counting WRC requires roughly half as many synchronisation operations and writes to the heap. Every time a WRC is cloned it's weight is split in two, with half allocated to the parent and half allocated to the child. When a WRC is dropped it's weight is removed from the total. When the total weight declines to zero then the referenced object is dropped.

The type WRC<T> provides shared ownership of type T, allocated on the heap. Invoking clone on WRC produces a new pointer to the shared value in the heap. When the last WRC pointer is dropped then the pointed-to value is also dropped.

See Wikipedia for more information about weighted reference counting.

Thread Safety

WRC<T> uses atomic operations for it's weight manipulations. This means that it is thread-safe. The disadvantage is that atomic operations are more expensive than ordinary memory accesses. The use of the weighted reference counting algorithm drastically reduces the number of synchronisation operations required to keep track of references.

Cycles

Currently there is no support in WRC for weak pointers or any other cycle breaking. If your usage causes you to create cycles of references you may want to investigate an alternative solution or manually force destruction of those objects.

Cloning references

Creating a new reference from an existing reference counted pointer is done using the Clone trait, implemented for WRC<T>.

Deref behaviour

WRC<T> automatically dereferences to T (via the Deref trait), so you can call T's methods on a value of type WTC<T>.

Examples

Sharing some immutable data between threads:

use wrc::WRC;
use std::thread;

let five = WRC::new(5);

for _ in 0..10 {
    let five = five.clone();

    thread::spawn(move || {
        println!("{:?}", five);
    });
}

Sharing a mutable AtomicUsize:

use wrc::WRC;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

let val = WRC::new(AtomicUsize::new(5));

for _ in 0..10 {
    let val = val.clone();

    thread::spawn(move || {
        let v = val.fetch_add(1, Ordering::SeqCst);
        println!("{:?}", v);
    });
}

See the rc documentation for more examples of reference counting in general.

Methods

impl<T> WRC<T>
[src]

[src]

Construct a new WRC<T>.

Examples

use wrc::WRC;
let five = WRC::new(5);

[src]

Return the total weight of all references. Only used for testing. You're unlikely to need it.

[src]

Makes a clone of the WRC pointer.

This creates another pointer with the same ptr value, dividing the weight evenly between the new reference and the old reference.

Examples

use wrc::WRC;

let five = WRC::new(5);
WRC::clone(&five);

Trait Implementations

impl<T: ?Sized + Sync + Send> Send for WRC<T>
[src]

impl<T: ?Sized + Sync + Send> Sync for WRC<T>
[src]

impl<T> Clone for WRC<T>
[src]

[src]

Makes a clone of the WRC pointer.

This creates another pointer with the same ptr value, dividing the weight evenly between the new reference and the old reference.

Examples

use wrc::WRC;

let five = WRC::new(5);
five.clone();

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T: ?Sized> Drop for WRC<T>
[src]

[src]

Drops the WRC.

This will decrement the total weight of the referenced object by the weight of this reference.

Examples

use wrc::WRC;

struct Foo;

impl Drop for Foo {
    fn drop(&mut self) {
        println!("dropped!");
    }
}

let foo = WRC::new(Foo);
let foo2 = foo.clone();

drop(foo);  // Doesn't print anything
drop(foo2); // Prints "dropped!"

impl<T> Deref for WRC<T>
[src]

The resulting type after dereferencing

[src]

The method called to dereference a value

impl<T: Display> Display for WRC<T>
[src]

[src]

Formats the value using the given formatter

impl<T: Debug> Debug for WRC<T>
[src]

[src]

Formats the value using the given formatter.

impl<T: PartialEq> PartialEq for WRC<T>
[src]

[src]

Equality for WRC<T>

Two WRC's are equal if their ptr values are equal.

[src]

Inequality for WRC<T>

Two WRC's are unequal if their ptr values are unequal.

impl<T: PartialOrd> PartialOrd for WRC<T>
[src]

[src]

Partial comparison for two WRC's

The two are compared by calling partial_cmp() on their ptr values.

[src]

Less-than comparison for two WRC's

The two are compared by calling < on their ptr values.

[src]

Less-than or equal to comparison for two WRC's

The two are compared by calling <= on their ptr values.

[src]

Greater-than comparison for two WRC's

The two are compared by calling > on their ptr values.

[src]

Greater-than or equal comparison for two WRC's

The two are compared by calling >= on their ptr values.

impl<T: Ord> Ord for WRC<T>
[src]

[src]

Comparison for two WRC's

The two are compared by calling cmp() on their ptr values.

1.21.0
[src]

Compares and returns the maximum of two values. Read more

1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl<T: Eq> Eq for WRC<T>
[src]

impl<T: Default> Default for WRC<T>
[src]

[src]

Creates a new WRC<T> with the Default value of T.

impl<T: Hash> Hash for WRC<T>
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

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