This commit is contained in:
2021-02-17 22:49:01 +08:00
parent 38439e4204
commit d7967ce520
4 changed files with 202 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
use async_speed_limit::Limiter;
use futures_util::{
future::try_join,
io::{self, AsyncRead, AsyncWrite},
};
use std::marker::Unpin;
fn main() {
println!("Hello, world!");
}
#[warn(dead_code)]
async fn copy_both_slowly(
r1: impl AsyncRead,
w1: &mut (impl AsyncWrite + Unpin),
r2: impl AsyncRead,
w2: &mut (impl AsyncWrite + Unpin),
) -> io::Result<()> {
// create a speed limiter of 1.0 KiB/s.
let limiter = <Limiter>::new(1024.0);
// limit both readers by the same queue
// (so 1.0 KiB/s is the combined maximum speed)
let r1 = limiter.clone().limit(r1);
let r2 = limiter.limit(r2);
// do the copy.
try_join(io::copy(r1, w1), io::copy(r2, w2)).await?;
Ok(())
}