Channels v0.1.0
Channel<T>
pub struct Channel<T> {
// private fields
}
A bounded queue of values passed between threads, any number of which may send and receive.
Values arrive in the order they were sent. Clone gives another handle to the same channel; the values still in
it are destroyed when the last handle is gone. A handle is not copied.
Type parameters
| Name | Description |
|---|---|
T | the type of the values |
Constructors
Channel
pub func Channel(capacity: uint) -> Channel<T>
A channel with room for capacity values. Zero capacity is a program error, as is running out of memory.
| Name | Type | Description |
|---|---|---|
capacity | uint | how many values may wait in the channel at once |
Returns: the first handle to it
Methods
Clone
pub func Clone(self: &Channel<T>) -> Channel<T>
Another handle to the same channel.
Returns: the handle
Send
pub func Send(self: &Channel<T>, value: T) -> ! T
Sends value, waiting while the channel is full.
| Name | Type | Description |
|---|---|---|
value | T | the value to send |
Returns: nothing, or fails with the value when the channel is closed
TrySend
pub func TrySend(self: &Channel<T>, value: T) -> ! T
Sends value if there is room, without waiting.
| Name | Type | Description |
|---|---|---|
value | T | the value to send |
Returns: nothing, or fails with the value when the channel is full or closed
Receive
pub func Receive(self: &Channel<T>) -> T?
Receives the oldest value, waiting while the channel is empty.
Returns: the value, or none once the channel is closed and empty
TryReceive
pub func TryReceive(self: &Channel<T>) -> T?
Receives the oldest value if there is one, without waiting.
Returns: the value, or none when the channel is empty
Close
pub func Close(self: &Channel<T>)
Closes the channel: sends fail from now on, and receivers take what is left and then get none. Closing a closed channel does nothing.
IsClosed
pub func IsClosed(self: &Channel<T>) -> bool
Whether the channel was closed.
Returns: true once Close was called on any handle
Length
pub func Length(self: &Channel<T>) -> uint
How many values wait in the channel right now, which other threads may change at any moment.
Returns: the number of values
Destructor
~Channel
func ~Channel(self: &var Channel<T>)