Crates.io | poll-channel |
lib.rs | poll-channel |
version | 0.1.9 |
source | src |
created_at | 2023-11-19 08:36:17.293912 |
updated_at | 2023-11-23 13:21:44.845547 |
description | Poll on channel |
homepage | |
repository | https://github.com/sweihub/poll-channel |
max_upload_size | |
id | 1041015 |
size | 8,542 |
poll-channel
provides a way to poll on channel in Rust sync programming, crossbeam channel was used in this crate.
example
use poll_channel::{channel, Poll};
#[test]
fn poll_test() -> Result<(), crossbeam::channel::RecvError> {
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
let poller = Poll::new();
poller.append([&rx1, &rx2]);
let _ = tx1.send(100);
let _ = tx2.send(200);
let mut i = 0;
while i < 3 {
let id = poller.poll(0.01);
if id == rx1.id() {
let n1 = rx1.recv()?;
assert!(n1 == 100);
i += 1;
} else if id == rx2.id() {
let n2 = rx2.recv()?;
assert!(n2 == 200);
i += 1;
} else if id == -1 {
// timeout
i += 1;
break;
}
}
Ok(())
}