Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 19, 2020 15:23
Show Gist options
  • Save rust-play/20bc6b3c2490cfc721012d3ffb279d81 to your computer and use it in GitHub Desktop.
Save rust-play/20bc6b3c2490cfc721012d3ffb279d81 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use tokio::sync::mpsc;
use tokio::sync::oneshot;
extern crate rand;
use futures::future::FutureExt;
use rand::Rng;
async fn lmao_test() {
let (gtx, mut grx): (
mpsc::UnboundedSender<oneshot::Sender<bool>>,
mpsc::UnboundedReceiver<oneshot::Sender<bool>>,
) = mpsc::unbounded_channel();
tokio::spawn(async move {
let mut ctr = 0;
loop {
println!("WAITING");
if let Some(i) = grx.recv().await {
i.send(false).unwrap();
ctr += 1;
println!("SENT {}", ctr);
} else {
println!("FINISHED");
break;
}
}
});
let mut rng = rand::thread_rng();
let t = rng.gen_range(200, 500);
tokio::spawn(async move {
let mut futs = vec![];
println!("{} times", t);
for _ in 0..t {
let (tx, rx) = oneshot::channel::<bool>();
gtx.send(tx).unwrap();
// If you remove shared(), it works
futs.push(Box::pin(async move { rx.await.unwrap() }).shared());
}
let mut ctr = 0;
for f in futs {
println!("start {}", ctr);
if f.await {
panic!("iwkms");
}
println!("end {}", ctr);
ctr = ctr + 1;
}
})
.await;
}
fn main() {
let mut rt = tokio::runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap();
rt.block_on(async {lmao_test().await;});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment