Crate loole

source ·
Expand description

An async/sync multi-producer multi-consumer channel. Multiple threads can send and receive messages on the channel at the same time and each message will be received by only one thread.

Producers can send and consumers can receive messages asynchronously or synchronously:

  • async send -> async receive
  • sync send -> sync receive
  • async send -> sync receive
  • sync send -> async receive

There are two types of channels: bounded and unbounded.

  1. Bounded channel with limited capacity.
  2. Unbounded channel with unlimited capacity.

A channel has two sides: the Sender side and the Receiver side. Both sides are cloneable, meaning that they can be copied and shared among multiple threads. This allows you to have multiple threads sending and receiving messages on the same channel.

When all Senders or all Receivers are dropped, the channel becomes closed. This means that no more messages can be sent, but remaining messages can still be received.

The channel can also be closed manually by calling Sender::close() or Receiver::close().

Examples

let (tx, rx) = loole::unbounded();

std::thread::spawn(move || {
    for i in 0..10 {
        tx.send(i).unwrap();
    }
});

let mut sum = 0;
while let Ok(i) = rx.recv() {
    sum += i;
}

assert_eq!(sum, (0..10).sum());

Structs

  • An fixed-sized iterator over the msgs drained from a channel.
  • An owned iterator over the msgs received synchronously from a channel.
  • An iterator over the msgs received synchronously from a channel.
  • The receiving end of a channel.
  • A future that allows asynchronously receiving a message. This future will resolve to a message when a message is available on the channel, or to an error if the channel is closed.
  • An error that occurs when trying to send a value on a channel after all receivers have been dropped.
  • A future that sends a value into a channel.
  • The sending half of a channel.
  • An non-blocking iterator over the msgs received synchronously from a channel.

Enums

  • An error that occurs when trying to receive a value from a channel after all senders have been dropped and there are no more messages in the channel.
  • An error that may be returned when attempting to receive a value on a channel with a timeout and no value is received within the timeout period, or when all senders have been dropped and there are no more values left in the channel.
  • An error that may be emitted when sending a value into a channel on a sender with a timeout.
  • An error that occurs when trying to receive a value from a channel when there are no messages in the channel. If there are no messages in the channel and all senders are dropped, then the Disconnected error will be returned.
  • An error that occurs when trying to send a value to a channel:

Functions

  • Create a bounded channel with a limited maximum capacity.
  • Creates an unbounded channel, which has unlimited capacity.