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. Copying a handle shares the 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 |
Associated functions
Create
pub func Create(
allocator: Allocator,
value: T
) -> Rc<T> ! AllocError
The first handle to value, moved onto the heap.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | T | the value to share |
Returns: the handle, or why there was no storage; the value is destroyed then
Methods
Get
pub func Get(self: &Rc<T>) -> *T
The shared value, for reading.
Returns: a pointer to it, valid while any handle exists
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
Count
pub func Count(self: &Rc<T>) -> uint
How many handles share the value.
Returns: the count
SameAs
pub func SameAs(self: &Rc<T>, 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
Operators
=
pub func =(self: &var Rc<T>, other: &Rc<T>)
Another handle to the same value, adding one to the count.
| Name | Type | Description |
|---|---|---|
other | &Rc<T> | the handle to copy |
Destructor
~Rc
func ~Rc(self: &var Rc<T>)
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from, which must be usable from every thread a copy reaches |
value | T | the value to share |
Returns: the handle, or why there was no storage; the value is destroyed then
Methods
Get
pub func Get(self: &Arc<T>) -> *T
The shared value, for reading.
Returns: a pointer to it, valid while any handle exists
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
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
SameAs
pub func SameAs(self: &Arc<T>, 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
Operators
=
pub func =(self: &var Arc<T>, other: &Arc<T>)
Another handle to the same value, adding one to the count atomically.
| Name | Type | Description |
|---|---|---|
other | &Arc<T> | the handle to copy |
Destructor
~Arc
func ~Arc(self: &var Arc<T>)