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:

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§

Drain
An fixed-sized iterator over the msgs drained from a channel.
IntoIter
An owned iterator over the msgs received synchronously from a channel.
Iter
An iterator over the msgs received synchronously from a channel.
Receiver
The receiving end of a channel.
RecvFuture
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.
RecvStream
A stream which allows asynchronously receiving messages.
SendError
An error that occurs when trying to send a value on a channel after all receivers have been dropped.
SendFuture
A future that sends a value into a channel.
SendSink
A sink that allows sending values into a channel.
Sender
The sending half of a channel.
TryIter
An non-blocking iterator over the msgs received synchronously from a channel.

Enums§

RecvError
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.
RecvTimeoutError
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.
SendTimeoutError
An error that may be emitted when sending a value into a channel on a sender with a timeout.
TryRecvError
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.
TrySendError
An error that occurs when trying to send a value to a channel:

Functions§

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