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. Copying a handle shares the 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

Associated functions

Create

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

The first handle to value, moved onto the heap.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
valueTthe value to share

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

Source

Methods

Get

pub func Get(self: &Rc<T>) -> *T

The shared value, for reading.

Returns: a pointer to it, valid while any handle exists

Source

GetMutable

pub func GetMutable(self: &var Rc<T>) -> (*var T)?

The value for writing, while this is the only handle to it.

Returns: a pointer to it, or none while other handles share it

Source

Count

pub func Count(self: &Rc<T>) -> uint

How many handles share the value.

Returns: the count

Source

SameAs

pub func SameAs(self: &Rc<T>, 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

Operators

=

pub func =(self: &var Rc<T>, other: &Rc<T>)

Another handle to the same value, adding one to the count.

NameTypeDescription
other&Rc<T>the handle to copy

Source

Destructor

~Rc

func ~Rc(self: &var Rc<T>)

Source

Arc<T>

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

A shared, counted handle to one value whose copies may live on different threads. Copying a handle shares the 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

Associated functions

Create

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

The first handle to value, moved onto the heap.

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

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

Source

Methods

Get

pub func Get(self: &Arc<T>) -> *T

The shared value, for reading.

Returns: a pointer to it, valid while any handle exists

Source

GetMutable

pub func GetMutable(self: &var Arc<T>) -> (*var T)?

The value for writing, while this is the only handle to it.

Returns: a pointer to it, or none while other handles share it

Source

Count

pub func Count(self: &Arc<T>) -> 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: &Arc<T>, 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

Operators

=

pub func =(self: &var Arc<T>, other: &Arc<T>)

Another handle to the same value, adding one to the count atomically.

NameTypeDescription
other&Arc<T>the handle to copy

Source

Destructor

~Arc

func ~Arc(self: &var Arc<T>)

Source