std.channel

Bounded channels between async tasks.

Importuse std.channel

Types

struct Channel#

A typed std.channel handle (Channel<T>); use send/recv and related methods.

An opaque handle: only std.channel can produce one, and its contents are not reachable from moon source.

Functions

extern fn new<T>(capacity: int) -> Channel<T>#

Creates a new value with the supplied configuration.

  • capacity: Buffered slots; zero makes every send wait for a receiver.
async fn main() {
  let messages = std.channel.new<string>(1)
  await messages.send("moon")
  println(messages.len())
  match await messages.recv() {
    some(message) => println(message),
    none => println("closed"),
  }
  messages.close()
  println(messages.is_closed())
}
extern fn send<T>(channel: Channel<T>, value: T) -> Future<void>#

Sends a value through the channel.

  • channel: Channel to send on.
  • value: Value to hand to the next receiver.
extern fn recv<T>(channel: Channel<T>) -> Future<Optional<T>>#

Receives the next channel value, or none after closure.

  • channel: Channel to receive from.
extern fn close<T>(channel: Channel<T>)#

Closes the resource.

  • channel: Channel to close; pending receivers then see none.
extern fn len<T>(channel: Channel<T>) -> int#

Returns the number of elements.

  • channel: Channel whose buffered count is returned.
extern fn is_closed<T>(channel: Channel<T>) -> bool#

Reports whether the channel has been closed.

  • channel: Channel to test.