diff options
author | Carl Lerche <me@carllerche.com> | 2019-02-21 11:56:15 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-21 11:56:15 -0800 |
commit | 80162306e71c8561873a9c9496d65f2c1387d119 (patch) | |
tree | 83327ca8d9d1326d54e3c679e1fb4eb16775d4be /examples/tinyhttp.rs | |
parent | ab595d08253dd7ee0422144f8dafffa382700976 (diff) |
chore: apply rustfmt to all crates (#917)
Diffstat (limited to 'examples/tinyhttp.rs')
-rw-r--r-- | examples/tinyhttp.rs | 78 |
1 files changed, 45 insertions, 33 deletions
diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs index 4cbefcc9..cde1b79a 100644 --- a/examples/tinyhttp.rs +++ b/examples/tinyhttp.rs @@ -23,12 +23,12 @@ extern crate time; extern crate tokio; extern crate tokio_io; -use std::{env, fmt, io}; use std::net::SocketAddr; +use std::{env, fmt, io}; -use tokio::net::{TcpStream, TcpListener}; +use tokio::codec::{Decoder, Encoder}; +use tokio::net::{TcpListener, TcpStream}; use tokio::prelude::*; -use tokio::codec::{Encoder, Decoder}; use bytes::BytesMut; use http::header::HeaderValue; @@ -44,7 +44,8 @@ fn main() -> Result<(), Box<std::error::Error>> { println!("Listening on: {}", addr); tokio::run({ - listener.incoming() + listener + .incoming() .map_err(|e| println!("failed to accept socket; error = {:?}", e)) .for_each(|socket| { process(socket); @@ -64,14 +65,13 @@ fn process(socket: TcpStream) { .split(); // Map all requests into responses and send them back to the client. - let task = tx.send_all(rx.and_then(respond)) - .then(|res| { - if let Err(e) = res { - println!("failed to process connection; error = {:?}", e); - } + let task = tx.send_all(rx.and_then(respond)).then(|res| { + if let Err(e) = res { + println!("failed to process connection; error = {:?}", e); + } - Ok(()) - }); + Ok(()) + }); // Spawn the task that handles the connection. tokio::spawn(task); @@ -82,9 +82,7 @@ fn process(socket: TcpStream) { /// This function is a map from and HTTP request to a future of a response and /// represents the various handling a server might do. Currently the contents /// here are pretty uninteresting. -fn respond(req: Request<()>) - -> Box<Future<Item = Response<String>, Error = io::Error> + Send> -{ +fn respond(req: Request<()>) -> Box<Future<Item = Response<String>, Error = io::Error> + Send> { let f = future::lazy(move || { let mut response = Response::builder(); let body = match req.uri().path() { @@ -99,14 +97,18 @@ fn respond(req: Request<()>) struct Message { message: &'static str, } - serde_json::to_string(&Message { message: "Hello, World!" })? + serde_json::to_string(&Message { + message: "Hello, World!", + })? } _ => { response.status(StatusCode::NOT_FOUND); String::new() } }; - let response = response.body(body).map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; + let response = response + .body(body) + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; Ok(response) }); @@ -124,12 +126,19 @@ impl Encoder for Http { fn encode(&mut self, item: Response<String>, dst: &mut BytesMut) -> io::Result<()> { use std::fmt::Write; - write!(BytesWrite(dst), "\ - HTTP/1.1 {}\r\n\ - Server: Example\r\n\ - Content-Length: {}\r\n\ - Date: {}\r\n\ - ", item.status(), item.body().len(), date::now()).unwrap(); + write!( + BytesWrite(dst), + "\ + HTTP/1.1 {}\r\n\ + Server: Example\r\n\ + Content-Length: {}\r\n\ + Date: {}\r\n\ + ", + item.status(), + item.body().len(), + date::now() + ) + .unwrap(); for (k, v) in item.headers() { dst.extend_from_slice(k.as_str().as_bytes()); @@ -198,13 +207,18 @@ impl Decoder for Http { headers[i] = Some((k, v)); } - (toslice(r.method.unwrap().as_bytes()), - toslice(r.path.unwrap().as_bytes()), - r.version.unwrap(), - amt) + ( + toslice(r.method.unwrap().as_bytes()), + toslice(r.path.unwrap().as_bytes()), + r.version.unwrap(), + amt, + ) }; if version != 1 { - return Err(io::Error::new(io::ErrorKind::Other, "only HTTP/1.1 accepted")) + return Err(io::Error::new( + io::ErrorKind::Other, + "only HTTP/1.1 accepted", + )); } let data = src.split_to(amt).freeze(); let mut ret = Request::builder(); @@ -216,15 +230,13 @@ impl Decoder for Http { Some((ref k, ref v)) => (k, v), None => break, }; - let value = unsafe { - HeaderValue::from_shared_unchecked(data.slice(v.0, v.1)) - }; + let value = unsafe { HeaderValue::from_shared_unchecked(data.slice(v.0, v.1)) }; ret.header(&data[k.0..k.1], value); } - let req = ret.body(()).map_err(|e| { - io::Error::new(io::ErrorKind::Other, e) - })?; + let req = ret + .body(()) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; Ok(Some(req)) } } |