moonbitlang/async/aqueue does not have a README file

    QueueAlreadyClosed

    pub suberror QueueAlreadyClosed

    Kind

    pub(all) enum Kind {
    Unbounded
    Blocking(Int)
    DiscardOldest(Int)
    DiscardLatest(Int)
    }

    Options for different behavior of put in async queue.
    • Unbounded: the queue has unbounded size, so put always succeed and never blocks. However, if the reader of the queue hangs silently, elements will accumulate infinitely in the queue, causing memory leak
    • Blocking(n): the queue can contain at most n elements, if the queue is full, put will block until some value is consumed. If multiple blocked writers coexist, they will be accepted in a FIFO manner
    • DiscardOldest(n): the queue can contain at most n elements, if the queue is full, the oldest elements will be discarded, so put always succeed and never block
    • DiscardLatest(n): the queue can contain at most n elements, if the queue is full, the newest elements, i.e. the argument of put, will be silently discarded.

    Queue

    type Queue[X]

    An asynchronous queue, where reader can wait for data to arrive in a non-blocking manner.

    Queue::close

    fn[X] Queue::close(self : Queue[X], error? : Error, clear? : Bool) -> Unit

    Close an async queue. After the queue is closed:

    • blocking put and get operations will fail immediately.
    • subsequent put and try_put operations also fail immediately
    • if clear = false (false by default), sbusequent get and try_get operations can still retrieve buffered elements in the queue before close. After the queue becomes empty, subsequent get and try_get operations also fail immediately
    • if clear = true, buffered elements in the queue will be cleared

    By default, writing to or reading from a closed queue will receive the QueueAlreadyClosed error. But this error can be customized via the error parameter.

    Queue::get

    async fn[X] Queue::get(self : Queue[X]) -> X

    Fetch an element from the queue. If the queue is currently empty, get will block and wait until data arrive. If there are multiple readers blocked on get, new data will be delivered in a first-come-first-serve manner.

    get itself never fail, and will wait indefinitely. However, since get is a blocking point, the task running get may be cancelled, in this case, an cancellation will be raised from get.

    If the queue is already closed and there is no buffered elements, get will fail immediately. If the queue is closed while get is waiting, get will also fail immediately.

    Queue::new

    #as_free_fn
    fn[X] Queue::new(kind? : Kind) -> Queue[X]

    Create an empty queue. The behavior of put is determined by the kind argument, see the type @aqueue.Kind for more details.

    Queue::put

    async fn[X] Queue::put(self : Queue[X], data : X) -> Unit

    Put a new element into a queue.

    • if there are readers blocked on this thread, the first reader will be woken to process the data
    • if there are no readers currently blocking, but the queue still have enough spare space, the new element will be placed in the queue, and put will return immediately without suspension
    • if the queue is already full, the behavior is determined by the kind argument provided on queue creation:
      • Unbounded: the queue has unbounded size, so can never be full
      • DiscardOldest: the oldest element in the queue is discarded
      • DiscardLatest: the argument of put is silently discarded, and put will have not effect

    Although put is an async function, if will not suspend unless the queue is Blocking and already full. So for Unbounded/DiscardOldest/DiscardLatest, it is safe to assume that put is actually synchronous. For context where async function is not allowed, use try_put instead.

    put itself never fails. But for Blocking queues, put may be cancelled before it succeed, in this case put will fail with a cancellation error. It is guaranteed that the element will be added to the queue if and only if put return normally.

    If the queue is already closed, put will fail immediately. If the queue is a blocking queue, and is closed while put is blocking, put will also fail immediately.

    Queue::try_get

    fn[X] Queue::try_get(self : Queue[X]) -> X? raise

    Try to fetch an element from the queue without blocking. If no element is in the queue at the moment, None is returned.

    If the queue is already closed and there is no buffered elements, try_get will fail immediately.

    Queue::try_put

    fn[X] Queue::try_put(self : Queue[X], data : X) -> Bool raise

    Try to put a new element into an async queue. If the queue still have enough room, the element will be added to the queue, and true will be returned. If the queue is already full, the element will not be added and false will be returned. Note that even for DiscardOldest or DiscardLatest queues, where put never blocks, try_put will still return false if the queue is full, instead of discarding the oldest/newest element.

    If the queue is already closed, try_put will fail with error immediately.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io