v0.4.0

Shared ownership v0.1.0

Source
Reference-counted handles: Rc within one thread and Arc across threads.

Rc<T>

pub struct Rc<T> {
    // private fields
}

A shared, counted handle to one value, for use on one thread. A handle is move-only; Clone makes another handle to the same value. Its count is not atomic, so a handle is not Sendable: a thread's context cannot hold one.

Source

Type parameters

NameDescription
Tthe value's type

Constructors

Rc

pub func Rc(allocator: Allocator, value: T) -> Rc<T>

The first handle to value, moved onto the heap. Terminates the program when the allocator refuses; TryCreate reports the refusal instead.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
valueTthe value to share

Returns: the handle

Source

Associated functions

TryCreate

pub func TryCreate(
    allocator: Allocator,
    value: T
) -> Rc<T> throws AllocError

The first handle to value, moved onto the heap, or why there was no storage.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
valueTthe value to share

Returns: the handle, or throws why there was no storage; the value is destroyed then

Source

Methods

Get

pub func Get(&self) -> &T

The shared value, borrowed for reading. self stays borrowed while the reference is in use, so this handle, and with it the value, outlives it.

Returns: a reference to the value

Source

GetMutable

pub func GetMutable(&var self) -> &var T?

The value borrowed for writing, while this is the only handle to it. self stays borrowed exclusively while the reference is in use, so no other handle can be made from it.

Returns: a writable reference to the value, or none while other handles share it

Source

Count

pub func Count(&self) -> uint

How many handles share the value.

Returns: the count

Source

SameAs

pub func SameAs(&self, other: &Rc<T>) -> bool

Whether other shares the same value, rather than an equal one.

NameTypeDescription
other&Rc<T>the other handle

Returns: whether both point at one value

Source

Implements Cloneable

Rc<T> conforms to Cloneable.

Clone

pub func Clone(&self) -> Rc<T>

Another handle to the same value, adding one to the count. Borrows this handle and allocates nothing; the value lives until the last handle is destroyed.

Returns: a new handle sharing the value

Source

Destructor

~Rc

func ~Rc(&var self)

Source

Arc<T>

pub struct Arc<T> {
    // private fields
}

A shared, counted handle to one value whose handles may live on different threads. A handle is move-only; Clone makes another handle to the same value, and the count is kept with atomic operations. The value is only read through it, so a value that changes while shared needs its own synchronization, such as a Sync::Mutex.

Source

Type parameters

NameDescription
Tthe value's type

Constructors

Arc

pub func Arc(allocator: Allocator, value: T) -> Arc<T>

The first handle to value, moved onto the heap. Terminates the program when the allocator refuses; TryCreate reports the refusal instead.

NameTypeDescription
allocatorAllocatorwhere the storage comes from, which must be usable from every thread a handle reaches
valueTthe value to share

Returns: the handle

Source

Associated functions

TryCreate

pub func TryCreate(
    allocator: Allocator,
    value: T
) -> Arc<T> throws AllocError

The first handle to value, moved onto the heap, or why there was no storage.

NameTypeDescription
allocatorAllocatorwhere the storage comes from, which must be usable from every thread a handle reaches
valueTthe value to share

Returns: the handle, or throws why there was no storage; the value is destroyed then

Source

Methods

Get

pub func Get(&self) -> &T

The shared value, borrowed for reading. self stays borrowed while the reference is in use, so this handle, and with it the value, outlives it.

Returns: a reference to the value

Source

GetMutable

pub func GetMutable(&var self) -> &var T?

The value borrowed for writing, while this is the only handle to it. self stays borrowed exclusively while the reference is in use, so no other handle can be made from it.

Returns: a writable reference to the value, or none while other handles share it

Source

Count

pub func Count(&self) -> uint

How many handles share the value, at some moment while this one existed; another thread may change it.

Returns: the count

Source

SameAs

pub func SameAs(&self, other: &Arc<T>) -> bool

Whether other shares the same value, rather than an equal one.

NameTypeDescription
other&Arc<T>the other handle

Returns: whether both point at one value

Source

Implements Cloneable

Arc<T> conforms to Cloneable.

Clone

pub func Clone(&self) -> Arc<T>

Another handle to the same value, adding one to the count atomically. Borrows this handle and allocates nothing; the new handle may move to another thread, and the value lives until the last handle on any thread is destroyed.

Returns: a new handle sharing the value

Source

Destructor

~Arc

func ~Arc(&var self)

Source