Crate smart_channel

Crate smart_channel 

Source
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 Sender and Receiver with 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 Sender and Receiver using an ID for stronger logical coupling.
  • Provides methods like is_binded_with to 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 channel or bind with the same ID.

Functions§

bind
Wraps the given sender and receiver with the provided ID. This function ensures that calling is_bound_to on the returned sender with the returned receiver will return true and 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.