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.
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 Sender
s or all Receiver
s 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.
- Into
Iter - 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.
- Recv
Future - 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.
- Recv
Stream - A stream which allows asynchronously receiving messages.
- Send
Error - An error that occurs when trying to send a value on a channel after all receivers have been dropped.
- Send
Future - A future that sends a value into a channel.
- Send
Sink - 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§
- Recv
Error - 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.
- Recv
Timeout Error - 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.
- Send
Timeout Error - An error that may be emitted when sending a value into a channel on a sender with a timeout.
- TryRecv
Error - 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. - TrySend
Error - An error that occurs when trying to send a value to a channel: