Skip to main content

Receiver

Struct Receiver 

Source
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>

Source

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.

Source

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 successfully
  • Err(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);
Source

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 successfully
  • Err(RecvTimeoutError::Timeout) if the timeout expired
  • Err(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());
Source

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 successfully
  • Err(TryRecvError::Empty) if the channel is empty but not closed
  • Err(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);
Source

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 successfully
  • Err(RecvTimeoutError::Timeout) if the timeout expired
  • Err(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);
Source

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 successfully
  • Err(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);
Source

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]);

Trait Implementations§

Source§

impl<T> Clone for Receiver<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Receiver<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<Receiver<T>> for Receiver<T>

Source§

fn from(rx: CrossbeamReceiver<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for Receiver<T>
where Receiver<T>: Freeze,

§

impl<T> RefUnwindSafe for Receiver<T>

§

impl<T> Send for Receiver<T>
where Receiver<T>: Send,

§

impl<T> Sync for Receiver<T>
where Receiver<T>: Sync,

§

impl<T> Unpin for Receiver<T>
where Receiver<T>: Unpin,

§

impl<T> UnsafeUnpin for Receiver<T>

§

impl<T> UnwindSafe for Receiver<T>
where Receiver<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.