Crates.io | bi-directional-pipe |
lib.rs | bi-directional-pipe |
version | 0.1.4 |
source | src |
created_at | 2021-06-06 13:07:21.299437 |
updated_at | 2023-02-25 13:27:29.381518 |
description | async bi-directional pipe. |
homepage | |
repository | https://github.com/luyikk/Bi-directional-Pipe |
max_upload_size | |
id | 406864 |
size | 26,635 |
Async Bi-directional Pipe
sync example:
use anyhow::{Result,ensure};
use bi_directional_pipe::sync::pipe;
use tokio::task::JoinHandle;
#[tokio::main]
async fn main()->Result<()> {
let (left,right)=pipe();
let join:JoinHandle<Result<()>>= tokio::spawn(async move{
for i in 0..1000000 {
right.send(i);
let ok=right.recv().await?;
ensure!(ok==i+1,"left return v error!!")
}
Ok(())
});
loop {
if let Ok(v)=left.recv().await{
left.send(v+1);
}else {
break;
}
}
join.await??;
Ok(())
}
unsync example:
use anyhow::{Result,ensure};
use std::time::Instant;
use bi_directional_pipe::unsync::pipe;
use tokio::task::JoinHandle;
#[tokio::main]
async fn main()->Result<()> {
let single_runtime=tokio::task::LocalSet::new();
let (left,right)=pipe();
let res:Result<()>= single_runtime.run_until(async move{
let join:JoinHandle<Result<()>>= tokio::task::spawn_local(async move{
for i in 0..1000000 {
right.send(i);
let ok=right.recv().await?;
ensure!(ok==i+1,"left return v error!!")
}
Ok(())
});
let start=Instant::now();
loop {
if let Ok(v)=left.recv().await{
left.send(v+1);
}else {
break;
}
}
println!("time {}ms",start.elapsed().as_millis());
join.await??;
Ok(())
}).await;
res?;
Ok(())
}