Module mpsc

Module mpsc 

Source
Expand description

Multiple producers, single consumer.

The optimization assumes a single consumer. The waker registration of the receiver is lossless compared to mpmc.

NOTE: For the SC (single consumer) version, AsyncRx and Rx are not Clone and do not implement Sync. Although they can be moved to other threads, they are not allowed to be used with send/recv while in an Arc.

The following code is OK:

use crossfire::*;
async fn foo() {
    let (tx, rx) = mpsc::bounded_async::<usize>(100);
    tokio::spawn(async move {
        let _ = rx.recv().await;
    });
    drop(tx);
}

Because the AsyncRx does not have the Sync marker, using Arc<AsyncRx> will lose the Send marker.

For your safety, the following code should not compile:

use crossfire::*;
use std::sync::Arc;
async fn foo() {
    let (tx, rx) = mpsc::bounded_async::<usize>(100);
    let rx = Arc::new(rx);
    tokio::spawn(async move {
        let _ = rx.recv().await;
    });
    drop(tx);
}

Enums§

Array
Flavor Type alias for bounded MPSC channel wrapped with specified One impl

Functions§

bounded_async
Creates a bounded channel with a pair of async sender and receiver.
bounded_async_blocking
Creates a bounded channel with a pair of async sender and blocking receiver.
bounded_blocking
Creates a bounded channel with a pair of blocking sender and receiver.
bounded_blocking_async
Creates a bounded channel with a pair of blocking sender and async receiver.
build
The generic builder for all mpsc channel types
new
The generic builder for all mpsc channel types with a new method (except Array).
unbounded_async
unbounded_blocking

Type Aliases§

List
Flavor Type alias for unbounded MPSC channel
Null
Flavor type for close notification, refer to crate::null for usage
One
Flavor type for one-sized MPSC channel