std::concurrent::channel

std/src/concurrent/channel.mo

typeChannel#
pub type Channel (A : Type) {
    channel
}

Channel handles are opaque runtime objects; do not construct or pattern match on them (the channel constructor is never built at run time).

defChannel.new#
pub def Channel.new : IO (Channel A)

A fresh, empty channel.

defChannel.put#
pub def Channel.put (c : Channel A) (v : A) : IO Unit

Put v, blocking while the channel is full. The Rust host cannot block (its fibers are lazy thunks), so there a put into a full channel replaces the value that was never taken.

defChannel.take#
pub def Channel.take (c : Channel A) : IO A

Take a value, blocking while the channel is empty. On the Rust host an empty channel is a loud eval error instead of a block -- there is no other thread that could ever fill it.

defChannel.tryTake#
pub def Channel.tryTake (c : Channel A) : IO (Option A)

Take a value if one is waiting, without blocking.

defselect#
pub def select {A : Type} (chs : List (Channel A)) : IO (Option A)

Take from whichever of chs has a value first, blocking until one does; none when the waiting fiber is cancelled first.

This used to probe every channel with tryTake and sleepIO 1 between passes, which cost a timer wakeup per millisecond and delayed a value by up to a millisecond. It now parks once on the channels themselves (channel_take_any), so a value is seen as soon as it arrives and an idle select costs nothing.

The result is an Option rather than a bare A because cancellation is a real outcome: a cancelled waiter has no value to hand back, and inventing one would be exactly the silent-IO-failure the project forbids. Callers that know their select cannot be cancelled unwrap it.

On the Rust host there is no parking (its fibers are deferred thunks), so the host answers none immediately -- the same lazy degradation race has, for the same reason. Note this is a BEHAVIOUR CHANGE on the host as well as a type change: the old polling select blocked until a value arrived, where the host's now scans once and gives up. Nothing in the tree calls select outside channel_test.mo, so no caller is affected today.

A NOTE ON THE TYPE: IO A became IO (Option A). This is a breaking change to a pub API, made because cancellation is a real outcome with no value of A to report; the polling version had no way to say it.