From 37fb8945fad0a034d1565bc4f79f9ab524587fc0 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 29 May 2016 21:15:05 -0400 Subject: genericize common functions over streams This makes source_files and sink_files generic over "Streams", defined as types that implement both Read and Write. --- src/client.rs | 6 +++--- src/common.rs | 10 +++------- src/server.rs | 4 ++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/client.rs b/src/client.rs index ccdf07d..8827a11 100644 --- a/src/client.rs +++ b/src/client.rs @@ -10,7 +10,7 @@ use std::process; use std::process::exit; use std::process::Command; use getopts::Options; -use utp::{UtpSocket}; +use utp::{UtpSocket, UtpStream}; pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir: bool, is_recv: bool) { println!("\thost: {}", host); @@ -52,7 +52,7 @@ pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir println!("\tsecret: {}", remote_secret); let mut socket = UtpSocket::connect((remote_host, remote_port)).unwrap();; - let mut stream = socket.into(); + let mut stream: UtpStream = socket.into(); if is_recv { common::sink_files(&mut stream, local_file, remote_is_dir); } else { @@ -105,7 +105,7 @@ pub fn main_client() { let port = matches.opt_str("port").unwrap().parse::().unwrap(); let mut socket = UtpSocket::connect((matches.opt_str("host").unwrap().as_str(), port)).unwrap(); - let mut stream = socket.into(); + let mut stream: UtpStream = socket.into(); println!("opened socket"); if matches.opt_present("f") { common::source_files(&mut stream, &matches.opt_str("f").unwrap(), dir_mode); diff --git a/src/common.rs b/src/common.rs index e68bae6..09f2b5b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,4 @@ -extern crate utp; - use std::str; use std::env; use std::path::Path; @@ -9,10 +7,8 @@ use std::os::unix::fs::PermissionsExt; use std::io; use std::io::{Read, Write, BufRead, BufReader}; use std::process::exit; -use utp::{UtpStream}; - -pub fn source_files(stream: &mut UtpStream, file_path: &str, recursive: bool) { +pub fn source_files(stream: &mut S, file_path: &str, recursive: bool) { println!("SOURCE FILE: {}", file_path); if recursive { unimplemented!(); } let mut f = File::open(file_path).unwrap(); @@ -59,7 +55,7 @@ pub fn source_files(stream: &mut UtpStream, file_path: &str, recursive: bool) { }; } -fn raw_read_line(stream: &mut UtpStream) -> io::Result { +fn raw_read_line(stream: &mut S) -> io::Result { let mut s = String::new(); let mut byte_buf = [0]; @@ -75,7 +71,7 @@ fn raw_read_line(stream: &mut UtpStream) -> io::Result { // TODO: it would be nice to be able to do BufReader/BufWriter on UtpStream. This would require // implementations of Read and Write on immutable references to UtpStream (a la TcpStream, File, et // al) -pub fn sink_files(stream: &mut UtpStream, file_path: &str, recursive: bool) { +pub fn sink_files(stream: &mut S, file_path: &str, recursive: bool) { println!("SINK FILE: {}", file_path); if recursive { unimplemented!(); } let mut f = File::create(file_path).unwrap(); diff --git a/src/server.rs b/src/server.rs index 390093e..3c79984 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,7 +10,7 @@ use std::env; use std::env::home_dir; use std::process::exit; use getopts::Options; -use utp::{UtpSocket, UtpListener}; +use utp::{UtpSocket, UtpStream, UtpListener}; fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool) { @@ -51,7 +51,7 @@ fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool) { let (mut socket, _src) = listener.accept().unwrap(); println!("Got connection from {}", socket.peer_addr().unwrap()); - let mut stream = socket.into(); + let mut stream: UtpStream = socket.into(); if is_recv { common::sink_files(&mut stream, path, recursive); -- cgit v1.2.3