@@ -163,6 +163,51 @@ pub fn async_channel_example() {
163
163
} ) ;
164
164
}
165
165
166
+ pub fn futures_channel_mpsc_example ( ) {
167
+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
168
+
169
+ let ( tx, mut rx) = futures_channel:: mpsc:: channel ( 3 ) ;
170
+
171
+ rt. block_on ( async move {
172
+ tokio:: spawn ( async move {
173
+ for _ in 0 ..3 {
174
+ let mut tx = tx. clone ( ) ;
175
+ thread:: spawn ( move || tx. start_send ( "ok" ) ) ;
176
+ }
177
+
178
+ drop ( tx) ;
179
+ } ) ;
180
+
181
+ // Unbounded receiver waiting for all senders to complete.
182
+ while let Ok ( msg) = rx. try_next ( ) {
183
+ println ! ( "{:?}" , msg) ;
184
+ }
185
+
186
+ println ! ( "futures_channel_mpsc_example completed" ) ;
187
+ } ) ;
188
+ }
189
+
190
+ pub fn futures_channel_oneshot_example ( ) {
191
+ use futures:: channel:: oneshot;
192
+ use std:: time:: Duration ;
193
+
194
+ let ( sender, receiver) = oneshot:: channel :: < i32 > ( ) ;
195
+
196
+ thread:: spawn ( || {
197
+ println ! ( "THREAD: sleeping zzz..." ) ;
198
+ thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
199
+ println ! ( "THREAD: i'm awake! sending." ) ;
200
+ sender. send ( 3 ) . unwrap ( ) ;
201
+ } ) ;
202
+
203
+ println ! ( "MAIN: doing some useful stuff" ) ;
204
+
205
+ futures:: executor:: block_on ( async {
206
+ println ! ( "MAIN: waiting for msg..." ) ;
207
+ println ! ( "MAIN: got: {:?}" , receiver. await )
208
+ } ) ;
209
+ }
210
+
166
211
pub fn async_priority_channel_example ( ) {
167
212
let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
168
213
0 commit comments