pub struct Receiver<T> { /* private fields */ }Expand description
The receiving half of an schannel channel.
This struct allows receiving values both synchronously and asynchronously using an optimized implementation designed for high throughput scenarios.
Receivers can be cloned to create multiple receiving endpoints for the same channel.
All synchronous crossbeam channel operations are available through [Deref].
The receiver can also be used as a Stream for async iteration.
§Examples
use linch::schannel;
let (sender, receiver) = schannel::bounded(5);
sender.send(42).unwrap();
// Receive synchronously
let value = receiver.recv().unwrap();
assert_eq!(value, 42);
// Clone the receiver
let receiver2 = receiver.clone();Implementations§
Source§impl<T> Receiver<T>
impl<T> Receiver<T>
Sourcepub fn new(rx: CrossbeamReceiver<T>) -> Self
pub fn new(rx: CrossbeamReceiver<T>) -> Self
Creates a new receiver from the underlying crossbeam receiver.
This is typically not called directly by users, but rather through
bounded.
Sourcepub fn recv(&self) -> Result<T, RecvError>
pub fn recv(&self) -> Result<T, RecvError>
Receives a value synchronously.
This method blocks until a value is available in the channel or all senders have been dropped.
§Returns
Ok(value)if a value was received successfullyErr(RecvError)if all senders have been dropped and the channel is empty
§Examples
use linch::schannel;
let (sender, receiver) = schannel::bounded(1);
sender.send(42).unwrap();
assert_eq!(receiver.recv().unwrap(), 42);Sourcepub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
Receives a value synchronously with a timeout.
This method blocks until a value is available in the channel, the timeout expires, or all senders have been dropped.
§Arguments
timeout- The maximum duration to wait
§Returns
Ok(value)if a value was received successfullyErr(RecvTimeoutError::Timeout)if the timeout expiredErr(RecvTimeoutError::Disconnected)if all senders have been dropped
§Examples
use linch::schannel;
use std::time::Duration;
let (_sender, receiver) = schannel::bounded::<i32>(1);
// This will timeout since no values are sent
let result = receiver.recv_timeout(Duration::from_millis(10));
assert!(result.is_err());Sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Attempts to receive a value without blocking.
This method returns immediately without blocking.
§Returns
Ok(value)if a value was received successfullyErr(TryRecvError::Empty)if the channel is empty but not closedErr(TryRecvError::Disconnected)if all senders have been dropped and the channel is empty
§Examples
use linch::schannel;
let (sender, receiver) = schannel::bounded(1);
// This will fail since no values are available
let result = receiver.try_recv();
assert!(result.is_err());
sender.send(42).unwrap();
assert_eq!(receiver.try_recv().unwrap(), 42);Sourcepub fn recv_timeout_async(&self, timeout: Duration) -> RecvTimeoutFut<'_, T> ⓘ
pub fn recv_timeout_async(&self, timeout: Duration) -> RecvTimeoutFut<'_, T> ⓘ
Receives a value asynchronously with a timeout.
The timeout is calculated from the time this method is called, not when the future is first polled.
§Arguments
timeout- The maximum duration to wait
§Returns
A RecvTimeoutFut that resolves to:
Ok(value)if a value was received successfullyErr(RecvTimeoutError::Timeout)if the timeout expiredErr(RecvTimeoutError::Disconnected)if all senders have been dropped
§Examples
use linch::schannel;
use std::time::Duration;
let (sender, receiver) = schannel::bounded(1);
sender.send(42).unwrap();
let timeout_fut = receiver.recv_timeout_async(Duration::from_secs(1));
assert_eq!(timeout_fut.await.unwrap(), 42);Sourcepub fn recv_async(&self) -> RecvFut<'_, T> ⓘ
pub fn recv_async(&self) -> RecvFut<'_, T> ⓘ
Receives a value asynchronously.
This method returns a future that will complete when a value is available in the channel or when all senders have been dropped.
§Returns
A RecvFut that resolves to:
Ok(value)if a value was received successfullyErr(RecvError)if all senders have been dropped and the channel is empty
§Examples
use linch::schannel;
let (sender, receiver) = schannel::bounded(1);
sender.send(42).unwrap();
assert_eq!(receiver.recv_async().await.unwrap(), 42);Sourcepub fn into_stream(self) -> RecvStream<T>
pub fn into_stream(self) -> RecvStream<T>
Converts the receiver into a stream.
This allows using the receiver with async stream utilities and for iteration.
§Returns
A RecvStream that yields values from the channel
§Examples
use linch::schannel;
use futures::StreamExt;
let (sender, receiver) = schannel::bounded(3);
sender.send(1).unwrap();
sender.send(2).unwrap();
sender.send(3).unwrap();
drop(sender); // Close the channel
let mut stream = receiver.into_stream();
let values: Vec<_> = stream.collect().await;
assert_eq!(values, vec![1, 2, 3]);