Skip to content

Commit 461cbee

Browse files
committed
add kanal channel
1 parent 6422297 commit 461cbee

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

channel/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ flume = "0.10.14"
1515
futures = "0.3.24"
1616
futures-channel = "0.3.24"
1717
futures-util = "0.3.24"
18+
kanal = "0.1.0-pre1"
1819
tokio = { version = "1.21.2", features = ["full"] }

channel/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ fn main() {
1414
async_priority_channel_example();
1515
futures_channel_mpsc_example();
1616
futures_channel_oneshot_example();
17+
kanal_example();
18+
kanal_async_example();
1719
}

channel/src/others.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,33 @@ pub fn async_priority_channel_example() {
223223
assert_eq!(r.recv().await, Ok(("Bar", 2)));
224224
});
225225
}
226+
227+
pub fn kanal_example() {
228+
let (tx, rx) = kanal::unbounded();
229+
230+
thread::spawn(move || {
231+
(0..10).for_each(|i| {
232+
tx.send(i).unwrap();
233+
});
234+
235+
drop(tx)
236+
});
237+
238+
let received: u32 = rx.sum();
239+
240+
println!("received sum: {}", received);
241+
}
242+
243+
pub fn kanal_async_example() {
244+
let rt = tokio::runtime::Runtime::new().unwrap();
245+
246+
let (tx, rx) = kanal::unbounded_async();
247+
248+
rt.block_on(async move {
249+
tokio::spawn(async move {
250+
tx.send(5).await.unwrap();
251+
});
252+
253+
println!("rx: {}", rx.recv().await.unwrap());
254+
});
255+
}

0 commit comments

Comments
 (0)