channels/
lib.rs

1//! <style>
2//! .rustdoc-hidden { display: none; }
3//! </style>
4#![doc = include_str!("../README.md")]
5#![cfg_attr(channels_nightly, feature(doc_auto_cfg))]
6#![cfg_attr(not(feature = "std"), no_std)]
7#![warn(clippy::print_stdout, clippy::print_stderr)]
8
9extern crate alloc;
10
11mod statistics;
12mod util;
13
14pub mod error;
15
16pub mod receiver;
17pub mod sender;
18
19#[cfg(feature = "statistics")]
20pub use self::statistics::Statistics;
21
22pub use self::receiver::Receiver;
23pub use self::sender::Sender;
24
25#[doc(inline)]
26pub use {channels_io as io, channels_serdes as serdes};
27
28/// A tuple containing a [`Sender`] and a [`Receiver`].
29pub type Pair<T, R, W, Sd> = (Sender<T, W, Sd>, Receiver<T, R, Sd>);
30
31#[cfg(feature = "bincode")]
32/// Create a new channel.
33///
34/// This function is just a shorthand for [`Sender::new`] and [`Receiver::new`].
35///
36/// # Examples
37///
38/// Synchronous version:
39/// ```no_run
40/// use std::net::TcpStream;
41///
42/// let conn = TcpStream::connect("127.0.0.1:1234").unwrap();
43///
44/// let (mut tx, mut rx) = channels::channel(conn.try_clone().unwrap(), conn);
45///
46/// tx.send_blocking(42_i32).unwrap();
47/// let received: i32 = rx.recv_blocking().unwrap();
48/// ```
49///
50/// Asynchronous version:
51/// ```no_run
52/// use tokio::net::TcpStream;
53///
54/// # #[tokio::main]
55/// # async fn main() {
56/// let conn = TcpStream::connect("127.0.0.1:1234").await.unwrap();
57/// let (r, w) = conn.into_split();
58/// let (mut tx, mut rx) = channels::channel(r, w);
59///
60/// tx.send(42_i32).await.unwrap();
61/// let received: i32 = rx.recv().await.unwrap();
62/// # }
63/// ```
64#[inline]
65pub fn channel<T, R, W>(
66	r: impl io::IntoRead<R>,
67	w: impl io::IntoWrite<W>,
68) -> Pair<T, R, W, channels_serdes::Bincode>
69where
70	for<'de> T: serde::Serialize + serde::Deserialize<'de>,
71{
72	(Sender::new(w), Receiver::new(r))
73}