Expand description
§Smart Channel
This crate provides enhanced functionalities for working with Tokio’s channels in mpsc
§Overview
In its current version, this crate introduces:
- Identifiable Channels: A way to associate
SenderandReceiverwith unique IDs to track and bind channels logically.
§Usage Example
#[tokio::main]
async fn main() {
use smart_channel::channel;
let id = 1;
let (sender, mut receiver) = channel::<String, _>(100, id);
tokio::spawn(async move {
sender.send("Hello from sender!".to_string()).await.unwrap();
});
let message = receiver.recv().await.unwrap();
assert_eq!(message, "Hello from sender!");
}§Features
- Bind
SenderandReceiverusing an ID for stronger logical coupling. - Provides methods like
is_binded_withto check relationships between channels.
Structs§
- Receiver
- A wrapper around Tokio’s receiver, with an associated ID for identification. Since receivers are not clonable, each receiver must have its own unique ID unless you manually create another receiver with the same ID.
- Sender
- A wrapper around Tokio’s sender, with an associated ID for identification.
Multiple senders can share the same ID if they are cloned or created using
channelorbindwith the same ID.
Functions§
- bind
- Wraps the given sender and receiver with the provided ID.
This function ensures that calling
is_bound_toon the returned sender with the returned receiver will returntrueand vice-versa. However, it does not guarantee that the Tokio channels are correctly connected. - channel
- Creates a Tokio channel (
tokio::sync::mpsc::channel) and associates it with the provided ID.