Shared ownership v0.1.0
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | T | the value to share |
Returns: the handle
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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | T | the value to share |
Returns: the handle, or throws why there was no storage; the value is destroyed then
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
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
Count
pub func Count(&self) -> uint
How many handles share the value.
Returns: the count
SameAs
pub func SameAs(&self, other: &Rc<T>) -> bool
Whether other shares the same value, rather than an equal one.
| Name | Type | Description |
|---|---|---|
other | &Rc<T> | the other handle |
Returns: whether both point at one value
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
Destructor
~Rc
func ~Rc(&var self)
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from, which must be usable from every thread a handle reaches |
value | T | the value to share |
Returns: the handle
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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from, which must be usable from every thread a handle reaches |
value | T | the value to share |
Returns: the handle, or throws why there was no storage; the value is destroyed then
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
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
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
SameAs
pub func SameAs(&self, other: &Arc<T>) -> bool
Whether other shares the same value, rather than an equal one.
| Name | Type | Description |
|---|---|---|
other | &Arc<T> | the other handle |
Returns: whether both point at one value
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
Destructor
~Arc
func ~Arc(&var self)