generics!!

This commit is contained in:
Milo Turner 2020-03-09 00:18:50 -04:00
parent d35d4ee0c1
commit 31ec42bf3a
2 changed files with 15 additions and 9 deletions

View File

@ -5,7 +5,7 @@ extern crate tokio;
extern crate thiserror;
use hptp::log::Logger;
use tokio::io::{AsyncWriteExt, Stdout};
use tokio::io::AsyncWrite;
use tokio::net::UdpSocket;
#[derive(Error, Debug)]
@ -44,7 +44,11 @@ async fn start(log: &mut Logger) -> Result<(), Error> {
download(log, sock, tokio::io::stdout()).await
}
async fn download(log: &mut Logger, mut sock: UdpSocket, mut out: Stdout) -> Result<(), Error> {
async fn download<OUT>(log: &mut Logger, mut sock: UdpSocket, mut out: OUT) -> Result<(), Error>
where
OUT: AsyncWrite + Unpin,
{
use tokio::io::AsyncWriteExt;
let mut buf = [0u8; 2000];
let mut pos = 0;
loop {

View File

@ -6,7 +6,7 @@ extern crate thiserror;
use hptp::log::Logger;
use std::net::SocketAddrV4;
use tokio::io::{AsyncReadExt, Stdin};
use tokio::io::AsyncRead;
use tokio::net::UdpSocket;
#[derive(Error, Debug)]
@ -54,9 +54,7 @@ async fn start(log: &mut Logger) -> Result<(), Error> {
.map_err(|_| Error::NoAvailPort)?;
log.debug_msg(format!("bound on {}", sock.local_addr()?))
.await;
upload(log, sock, tokio::io::stdin(), targ_addr).await?;
log.debug_msg("bye!").await;
Ok(())
upload(log, sock, tokio::io::stdin(), targ_addr).await
}
fn parse_args(mut args: impl Iterator<Item = String>) -> Result<SocketAddrV4, Error> {
@ -66,12 +64,16 @@ fn parse_args(mut args: impl Iterator<Item = String>) -> Result<SocketAddrV4, Er
.map_err(|_| Error::InvalidArgs)
}
async fn upload(
async fn upload<IN>(
log: &mut Logger,
mut sock: UdpSocket,
mut inp: Stdin,
mut inp: IN,
targ_addr: SocketAddrV4,
) -> Result<(), Error> {
) -> Result<(), Error>
where
IN: AsyncRead + Unpin,
{
use tokio::io::AsyncReadExt;
let mut buf = [0u8; 500];
let mut pos = 0;
loop {