diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-05-29 21:15:05 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-05-29 21:15:07 -0400 |
commit | 37fb8945fad0a034d1565bc4f79f9ab524587fc0 (patch) | |
tree | e7dd480223dc1adf093e604839d8bbe943faa0ab | |
parent | f9b27f5424cb857406a60a2259052dfff1d76439 (diff) | |
download | ucp-37fb8945fad0a034d1565bc4f79f9ab524587fc0.tar.gz ucp-37fb8945fad0a034d1565bc4f79f9ab524587fc0.zip |
genericize common functions over streams
This makes source_files and sink_files generic over "Streams", defined as types
that implement both Read and Write.
-rw-r--r-- | src/client.rs | 6 | ||||
-rw-r--r-- | src/common.rs | 10 | ||||
-rw-r--r-- | 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::<u16>().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<S: Read + Write>(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<String> { +fn raw_read_line<S: Read + Write>(stream: &mut S) -> io::Result<String> { let mut s = String::new(); let mut byte_buf = [0]; @@ -75,7 +71,7 @@ fn raw_read_line(stream: &mut UtpStream) -> io::Result<String> { // 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<S: Read + Write>(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); |