From d593c5b051f07bde5117122216a356632986b6dd Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Sat, 14 Dec 2019 09:01:47 +0300 Subject: chore: remove benches and fix/work around clippy lints (#1952) --- examples/chat.rs | 4 +++- examples/connect.rs | 9 ++++----- examples/echo-udp.rs | 4 +++- examples/echo.rs | 4 +++- examples/print_each_packet.rs | 4 +++- examples/proxy.rs | 8 ++++++-- examples/tinydb.rs | 19 +++++++++---------- examples/tinyhttp.rs | 4 +++- examples/udp-client.rs | 2 +- examples/udp-codec.rs | 4 +++- 10 files changed, 38 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/chat.rs b/examples/chat.rs index 2553cc5e..91589072 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -49,7 +49,9 @@ async fn main() -> Result<(), Box> { // client connection. let state = Arc::new(Mutex::new(Shared::new())); - let addr = env::args().nth(1).unwrap_or("127.0.0.1:6142".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:6142".to_string()); // Bind a TCP listener to the socket address. // diff --git a/examples/connect.rs b/examples/connect.rs index cdd18e19..d51af88c 100644 --- a/examples/connect.rs +++ b/examples/connect.rs @@ -36,10 +36,9 @@ async fn main() -> Result<(), Box> { }; // Parse what address we're going to connect to - let addr = match args.first() { - Some(addr) => addr, - None => Err("this program requires at least one argument")?, - }; + let addr = args + .first() + .ok_or("this program requires at least one argument")?; let addr = addr.parse::()?; let stdin = FramedRead::new(io::stdin(), codec::Bytes); @@ -163,7 +162,7 @@ mod codec { type Error = io::Error; fn decode(&mut self, buf: &mut BytesMut) -> io::Result>> { - if buf.len() > 0 { + if !buf.is_empty() { let len = buf.len(); Ok(Some(buf.split_to(len).into_iter().collect())) } else { diff --git a/examples/echo-udp.rs b/examples/echo-udp.rs index f1e8134d..d8b2af9c 100644 --- a/examples/echo-udp.rs +++ b/examples/echo-udp.rs @@ -51,7 +51,9 @@ impl Server { #[tokio::main] async fn main() -> Result<(), Box> { - let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:8080".to_string()); let socket = UdpSocket::bind(&addr).await?; println!("Listening on: {}", socket.local_addr()?); diff --git a/examples/echo.rs b/examples/echo.rs index 455aebde..35b12279 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -33,7 +33,9 @@ async fn main() -> Result<(), Box> { // Allow passing an address to listen on as the first argument of this // program, but otherwise we'll just set up our TCP listener on // 127.0.0.1:8080 for connections. - let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:8080".to_string()); // Next up we create a TCP listener which will listen for incoming // connections. This TCP listener is bound to the address we determined diff --git a/examples/print_each_packet.rs b/examples/print_each_packet.rs index f056db4a..4604139b 100644 --- a/examples/print_each_packet.rs +++ b/examples/print_each_packet.rs @@ -65,7 +65,9 @@ async fn main() -> Result<(), Box> { // Allow passing an address to listen on as the first argument of this // program, but otherwise we'll just set up our TCP listener on // 127.0.0.1:8080 for connections. - let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:8080".to_string()); // Next up we create a TCP listener which will listen for incoming // connections. This TCP listener is bound to the address we determined diff --git a/examples/proxy.rs b/examples/proxy.rs index 48f8f057..f7a9111f 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -32,8 +32,12 @@ use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { - let listen_addr = env::args().nth(1).unwrap_or("127.0.0.1:8081".to_string()); - let server_addr = env::args().nth(2).unwrap_or("127.0.0.1:8080".to_string()); + let listen_addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:8081".to_string()); + let server_addr = env::args() + .nth(2) + .unwrap_or_else(|| "127.0.0.1:8080".to_string()); println!("Listening on: {}", listen_addr); println!("Proxying to: {}", server_addr); diff --git a/examples/tinydb.rs b/examples/tinydb.rs index 3fc88f6b..cf867a0a 100644 --- a/examples/tinydb.rs +++ b/examples/tinydb.rs @@ -84,7 +84,9 @@ enum Response { async fn main() -> Result<(), Box> { // Parse the address we're going to run this server on // and set up our TCP listener to accept connections. - let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:8080".to_string()); let mut listener = TcpListener::bind(&addr).await?; println!("Listening on: {}", addr); @@ -175,15 +177,12 @@ fn handle_request(line: &str, db: &Arc) -> Response { impl Request { fn parse(input: &str) -> Result { - let mut parts = input.splitn(3, " "); + let mut parts = input.splitn(3, ' '); match parts.next() { Some("GET") => { - let key = match parts.next() { - Some(key) => key, - None => return Err(format!("GET must be followed by a key")), - }; + let key = parts.next().ok_or("GET must be followed by a key")?; if parts.next().is_some() { - return Err(format!("GET's key must not be followed by anything")); + return Err("GET's key must not be followed by anything".into()); } Ok(Request::Get { key: key.to_string(), @@ -192,11 +191,11 @@ impl Request { Some("SET") => { let key = match parts.next() { Some(key) => key, - None => return Err(format!("SET must be followed by a key")), + None => return Err("SET must be followed by a key".into()), }; let value = match parts.next() { Some(value) => value, - None => return Err(format!("SET needs a value")), + None => return Err("SET needs a value".into()), }; Ok(Request::Set { key: key.to_string(), @@ -204,7 +203,7 @@ impl Request { }) } Some(cmd) => Err(format!("unknown command: {}", cmd)), - None => Err(format!("empty input")), + None => Err("empty input".into()), } } } diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs index f8731b9f..5ddf0d48 100644 --- a/examples/tinyhttp.rs +++ b/examples/tinyhttp.rs @@ -27,7 +27,9 @@ use tokio_util::codec::{Decoder, Encoder, Framed}; async fn main() -> Result<(), Box> { // Parse the arguments, bind the TCP socket we'll be listening to, spin up // our worker threads, and start shipping sockets to those worker threads. - let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:8080".to_string()); let mut server = TcpListener::bind(&addr).await?; let mut incoming = server.incoming(); println!("Listening on: {}", addr); diff --git a/examples/udp-client.rs b/examples/udp-client.rs index 5437daf6..a191033d 100644 --- a/examples/udp-client.rs +++ b/examples/udp-client.rs @@ -44,7 +44,7 @@ fn get_stdin_data() -> Result, Box> { async fn main() -> Result<(), Box> { let remote_addr: SocketAddr = env::args() .nth(1) - .unwrap_or("127.0.0.1:8080".into()) + .unwrap_or_else(|| "127.0.0.1:8080".into()) .parse()?; // We use port 0 to let the operating system allocate an available port for us. diff --git a/examples/udp-codec.rs b/examples/udp-codec.rs index 0c9dbf76..6b3f84a0 100644 --- a/examples/udp-codec.rs +++ b/examples/udp-codec.rs @@ -22,7 +22,9 @@ use std::time::Duration; #[tokio::main] async fn main() -> Result<(), Box> { - let addr = env::args().nth(1).unwrap_or("127.0.0.1:0".to_string()); + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:0".to_string()); // Bind both our sockets and then figure out what ports we got. let a = UdpSocket::bind(&addr).await?; -- cgit v1.2.3