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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Copyright (C) 2024 Automated Design Corp.. All Rights Reserved.
// Created Date: 2024-10-03 08:36:27
// -----
// Last Modified: 2024-10-19 16:03:25
// -----
//
//
//! An asynchronous, bi-directional channel meant for communicating via an internal bus.
//! We use tokio mpsc (multi-producer, single consumer) channels because tokio typically
//! runs through all or projects.
//!
//! The AsyncBusChannel uses generics to allow any type of message.
//!
use ;
use ;
/// An asynchronous, bi-directional channel.
/// The AsyncBusChannel uses generics to allow any type of message payload.
///
/// # Examples
///
/// ```ignore
/// let (mut l, mut r) = AsyncBusChannel::new::<String, String>(32);
///
/// l.send("Hello from chan1".to_string()).await.unwrap();
/// r.send("Hello from chan2".to_string()).await.unwrap();
///
/// assert_eq!(r.recv().await.unwrap(), "Hello from chan1");
/// assert_eq!(l.recv().await.unwrap(), "Hello from chan2");
/// ```
/// Creates a complete asynchronous, bidirectional pipeline with the specified buffer size for each channel.
/// A pipeline is the combination of two channels, allowing two separate instances to communicate bi-directionally.
///
/// # Arguments
///
/// * `buffer_size` - The number of messages than can be buffered in the channel. A minimum of 32 is recommended.
///
/// # Returns
///
/// * `(AsyncBusChannel<T, U>, AsyncBusChannel<U, T>)` - Returns a tuple of two `AsyncBusChannel` instances.
///
/// # Examples
///
/// ```
/// use mechutil::async_channel::{AsyncBusChannel, async_pipeline};
///
/// #[tokio::main]
/// async fn main() {
/// let (mut chan1, mut chan2) = async_pipeline::<String, String>(32);
///
/// chan1.send("Message from channel 1".to_string()).await.unwrap();
/// chan2.send("Message from channel 2".to_string()).await.unwrap();
///
/// let msg1 = chan2.recv().await.unwrap();
/// let msg2 = chan1.recv().await.unwrap();
///
/// assert_eq!(msg1, "Message from channel 1");
/// assert_eq!(msg2, "Message from channel 2");
/// }
/// ```