Channels v0.1.0

Source
Bounded queues that carry values from thread to thread.

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.

Source

Type parameters

NameDescription
Tthe 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.

NameTypeDescription
capacityuinthow many values may wait in the channel at once

Returns: the first handle to it

Source

Methods

Clone

pub func Clone(self: &Channel<T>) -> Channel<T>

Another handle to the same channel.

Returns: the handle

Source

Send

pub func Send(self: &Channel<T>, value: T) -> ! T

Sends value, waiting while the channel is full.

NameTypeDescription
valueTthe value to send

Returns: nothing, or fails with the value when the channel is closed

Source

TrySend

pub func TrySend(self: &Channel<T>, value: T) -> ! T

Sends value if there is room, without waiting.

NameTypeDescription
valueTthe value to send

Returns: nothing, or fails with the value when the channel is full or closed

Source

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

Source

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

Source

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.

Source

IsClosed

pub func IsClosed(self: &Channel<T>) -> bool

Whether the channel was closed.

Returns: true once Close was called on any handle

Source

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

Source

Destructor

~Channel

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

Source