A simple bounded synchronous queue.
Uses a ring buffer and Mutex and CondVar to provide mutual exclusion and signalling.
Example
use std::sync::channel::Channel;
use std::thread::spawn;
// Space for 10 elements
let chan: Channel<i32> = Channel::new(5);
let t = spawn(|&chan| {
for i in 0..10 {
chan.send(i).unwrap();
}
chan.close();
});
// Prints all the values.
for i in chan {
println!("{}", i);
}
t.join().unwrap();
Run this example
Fields
Methods
impl Channel<T> { ... }
-
Creates a new channel with a given capacity
-
fn send(self: &mut Channel<T>, value: T) -> Result<(), ChannelError>
Send a value to the channel.
This function will block if the channel is full.
-
fn try_send(self: &mut Channel<T>, value: T) -> Result<(), ChannelError>
-
fn send_timeout(self: &mut Channel<T>, value: T, timeout: Duration) -> Result<(), ChannelError>
Try to send a value to the channel with a timeout.
If the channel is full, this function will return WouldBlock.
-
fn recv(self: &mut Channel<T>) -> Result<T, ChannelError>
Receive a value from the channel.
This function will block if the channel is empty.
-
fn try_recv(self: &mut Channel<T>) -> Result<T, ChannelError>
Try to receive a value from the channel.
If the channel is empty, this function will return WouldBlock.
-
fn recv_timeout(self: &mut Channel<T>, timeout: Duration) -> Result<T, ChannelError>
Try to receive a value from the channel with a timeout.
If the channel is empty, this function will return WouldBlock.
-
fn close(self: &mut Channel<T>)
-
fn iter(self: &mut Channel<T>) -> ChannelIterator<Channel<T>, T>
-
fn free(self: &mut Channel<T>)
Frees the memory backing the object.
Mixins
impl Channel<T> { ... }
-
mixin ConditionExt<Channel<T>>