1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Wrapper around [crossbeam::channel] and [flume] to provide common
//! interfaces for sync/async (un)bounded and non-tokio oneshot channels.
use crossbeam::channel;
/// Sender for a bounded [crossbeam::channel].
pub type BoundedChannelSender<T> = channel::Sender<T>;
/// Receiver for a bounded [crossbeam::channel].
pub type BoundedChannelReceiver<T> = channel::Receiver<T>;
/// A bounded [crossbeam::channel] with a sender and receiver.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Channel<T> {
/// Sender for the channel.
tx: channel::Sender<T>,
/// REceiver for the channel.
rx: channel::Receiver<T>,
}
impl<T> Channel<T> {
/// Create a new [Channel] with a given capacity.
pub fn with(capacity: usize) -> (BoundedChannelSender<T>, BoundedChannelReceiver<T>) {
let (tx, rx) = channel::bounded(capacity);
(tx, rx)
}
/// Create a oneshot (1) [Channel].
pub fn oneshot() -> (BoundedChannelSender<T>, BoundedChannelReceiver<T>) {
let (tx, rx) = channel::bounded(1);
(tx, rx)
}
}
/// [flume::Sender] for a bounded [flume::bounded] channel.
pub type AsyncChannelSender<T> = flume::Sender<T>;
/// [flume::Receiver] for a bounded [flume::bounded] channel.
pub type AsyncChannelReceiver<T> = flume::Receiver<T>;
/// A bounded [flume] channel with sender and receiver.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct AsyncChannel<T> {
/// Sender for the channel.
tx: flume::Sender<T>,
/// REceiver for the channel.
rx: flume::Receiver<T>,
}
impl<T> AsyncChannel<T> {
/// Create a new [AsyncChannel] with a given capacity.
pub fn with(capacity: usize) -> (AsyncChannelSender<T>, AsyncChannelReceiver<T>) {
let (tx, rx) = flume::bounded(capacity);
(tx, rx)
}
/// Create an unbounded [AsyncChannel].
pub fn unbounded() -> (AsyncChannelSender<T>, AsyncChannelReceiver<T>) {
let (tx, rx) = flume::unbounded();
(tx, rx)
}
/// Create a oneshot (1) [Channel].
pub fn oneshot() -> (AsyncChannelSender<T>, AsyncChannelReceiver<T>) {
let (tx, rx) = flume::bounded(1);
(tx, rx)
}
}