Channel

Struct Channel 

Source
pub struct Channel<S, R> { /* private fields */ }
Expand description

One side of a bidirectional channel. This channel can send to and receive from its counterpart.

§Examples

let (l, r) = bichannel::channel();
  
l.send(1).unwrap();
assert_eq!(Ok(1), r.recv());

r.send(1).unwrap();
assert_eq!(Ok(1), l.recv());

Implementations§

Source§

impl<S, R> Channel<S, R>

Source

pub fn send(&self, s: S) -> Result<(), SendError<S>>

See mpsc::Sender::send

Attempts to send a value to the other side of this channel, returning it back if it could not be sent.

A successful send occurs when it is determined that the other end of the channel has not hung up already. An unsuccessful send would be one where the corresponding receiver has already been deallocated. Note that a return value of Err means that the data will never be received, but a return value of Ok does not mean that the data will be received. It is possible for the corresponding receiver to hang up immediately after this function returns Ok.

This method will never block the current thread.

§Examples

let (l, r) = bichannel::channel();

// This send is always successful
l.send(1).unwrap();

// This send will fail because the receiver is gone
drop(l);
assert_eq!(r.send(1).unwrap_err().0, 1);
Source

pub fn recv(&self) -> Result<R, RecvError>

See mpsc::Receiver::recv

Attempts to wait for a value from the other side, returning an error if the other side has hung up.

This function will always block the current thread if there is no data available and it’s possible for more data to be sent. Once a message is sent from the other side then this will wake up and return that message.

If the corresponding channel has disconnected, or it disconnects while this call is blocking, this call will wake up and return Err to indicate that no more messages can ever be received on this channel. However, since channels are buffered, messages sent before the disconnect will still be properly received.

§Examples
use std::thread;

let (left, right) = bichannel::channel::<u8, u8>();

let _result = thread::spawn(move || {
    right.send(1u8).unwrap();
}).join().unwrap();

assert_eq!(Ok(1), left.recv());

Buffering behavior:

use std::sync::mpsc;
use std::thread;
use std::sync::mpsc::RecvError;

let (send, recv) = mpsc::channel();
let handle = thread::spawn(move || {
    send.send(1u8).unwrap();
    send.send(2).unwrap();
    send.send(3).unwrap();
    drop(send);
});

// wait for the thread to join so we ensure the sender is dropped
handle.join().unwrap();

assert_eq!(Ok(1), recv.recv());
assert_eq!(Ok(2), recv.recv());
assert_eq!(Ok(3), recv.recv());
assert_eq!(Err(RecvError), recv.recv());
Source

pub fn try_recv(&self) -> Result<R, TryRecvError>

See mpsc::Receiver::try_recv.

Attempts to return a pending value from the other side without blocking.

This method will never block the caller in order to wait for data to become available. Instead, this will always return immediately with a possible option of pending data on the channel.

This is useful for a flavor of “optimistic check” before deciding to block on a receiver.

Compared with recv, this function has two failure cases instead of one (one for disconnection, one for an empty buffer).

§Examples

let (_, right) = bichannel::channel::<(), ()>();

assert!(right.try_recv().is_err());

Trait Implementations§

Source§

impl<S: Debug, R: Debug> Debug for Channel<S, R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, R> Freeze for Channel<S, R>

§

impl<S, R> RefUnwindSafe for Channel<S, R>

§

impl<S, R> Send for Channel<S, R>
where S: Send, R: Send,

§

impl<S, R> !Sync for Channel<S, R>

§

impl<S, R> Unpin for Channel<S, R>

§

impl<S, R> UnwindSafe for Channel<S, R>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.