Deduplicate copy method

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
2023-10-04 20:50:40 +02:00
parent da46c5873f
commit 8dae1126d5
3 changed files with 19 additions and 31 deletions

View File

@@ -2,6 +2,8 @@ use log::{error, info};
use std::collections::{HashMap, HashSet};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::task::JoinHandle;
mod protocol;
@@ -210,3 +212,17 @@ mod tests {
// conn.shutdown().await.unwrap();
}
}
async fn copy<'a, R, W>(reader: &'a mut R, writer: &'a mut W) -> io::Result<u64>
where
R: AsyncRead + Unpin + ?Sized,
W: AsyncWrite + Unpin + ?Sized,
{
match io::copy(reader, writer).await {
Ok(u64) => {
let _ = writer.shutdown().await;
Ok(u64)
}
Err(_) => Ok(0),
}
}