From e445206823faa7499c92ec40c89ea776c195abc7 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sat, 25 Apr 2020 16:01:20 +0800 Subject: [PATCH] Remove Ok(()) with .await as end statement Make app.listen more ergonomic. Before #[async_std::main] async fn main() -> Result<(), std::io::Error> { let mut app = tide::new(); app.at("/").get(|_| async { Ok("Hello, world!") }); app.listen("127.0.0.1:8080").await?; Ok(()) } After #[async_std::main] async fn main() -> Result<(), std::io::Error> { let mut app = tide::new(); app.at("/").get(|_| async { Ok("Hello, world!") }); app.listen("127.0.0.1:8080").await } --- README.md | 3 +-- examples/chunked.rs | 3 +-- examples/cookies.rs | 4 +--- examples/fib.rs | 4 ++-- examples/graphql.rs | 3 +-- examples/hello.rs | 3 +-- examples/json.rs | 3 +-- examples/nested.rs | 3 +-- examples/sse.rs | 3 +-- examples/static_file.rs | 3 +-- src/lib.rs | 20 ++++++++++---------- src/redirect/mod.rs | 4 ++-- src/redirect/permanent.rs | 4 ++-- src/redirect/temporary.rs | 4 ++-- src/request.rs | 24 ++++++++++++------------ src/server/mod.rs | 8 ++++---- src/server/route.rs | 3 +-- src/sse/mod.rs | 4 ++-- 18 files changed, 46 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 6b44381f7..0100f959a 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,7 @@ async-std = { version = "1.5.0", features = ["attributes"] } async fn main() -> Result<(), std::io::Error> { let mut app = tide::new(); app.at("/").get(|_| async { Ok("Hello, world!") }); - app.listen("127.0.0.1:8080").await?; - Ok(()) + app.listen("127.0.0.1:8080").await } ``` diff --git a/examples/chunked.rs b/examples/chunked.rs index da6cfae6b..e3869d7fc 100644 --- a/examples/chunked.rs +++ b/examples/chunked.rs @@ -11,7 +11,6 @@ fn main() -> Result<(), std::io::Error> { let res = Response::new(StatusCode::Ok).body(BufReader::new(file)); Ok(res) }); - app.listen("127.0.0.1:8080").await?; - Ok(()) + app.listen("127.0.0.1:8080").await }) } diff --git a/examples/cookies.rs b/examples/cookies.rs index 1391aa2d7..effd6f391 100644 --- a/examples/cookies.rs +++ b/examples/cookies.rs @@ -27,8 +27,6 @@ fn main() -> Result<(), std::io::Error> { app.at("/").get(retrieve_cookie); app.at("/set").get(set_cookie); app.at("/remove").get(remove_cookie); - app.listen("127.0.0.1:8080").await?; - - Ok(()) + app.listen("127.0.0.1:8080").await }) } diff --git a/examples/fib.rs b/examples/fib.rs index 7e4a8ffae..3aca54d14 100644 --- a/examples/fib.rs +++ b/examples/fib.rs @@ -25,6 +25,7 @@ async fn fibsum(req: Request<()>) -> tide::Result { ); Ok(res) } + // Example: HTTP GET to http://localhost:8080/fib/42 // $ curl "http://localhost:8080/fib/42" // The fib of 42 is 267914296. @@ -33,7 +34,6 @@ fn main() -> Result<(), std::io::Error> { task::block_on(async { let mut app = tide::new(); app.at("/fib/:n").get(fibsum); - app.listen("0.0.0.0:8080").await?; - Ok(()) + app.listen("0.0.0.0:8080").await }) } diff --git a/examples/graphql.rs b/examples/graphql.rs index 0c787a161..315255ea4 100644 --- a/examples/graphql.rs +++ b/examples/graphql.rs @@ -107,7 +107,6 @@ fn main() -> std::io::Result<()> { app.at("/").get(redirect::permanent("/graphiql")); app.at("/graphql").post(handle_graphql); app.at("/graphiql").get(handle_graphiql); - app.listen("0.0.0.0:8080").await?; - Ok(()) + app.listen("0.0.0.0:8080").await }) } diff --git a/examples/hello.rs b/examples/hello.rs index 9dc8f6bd2..0e7910562 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -4,7 +4,6 @@ fn main() -> Result<(), std::io::Error> { task::block_on(async { let mut app = tide::new(); app.at("/").get(|_| async move { Ok("Hello, world!") }); - app.listen("127.0.0.1:8080").await?; - Ok(()) + app.listen("127.0.0.1:8080").await }) } diff --git a/examples/json.rs b/examples/json.rs index 2e8ea1891..bd245d679 100644 --- a/examples/json.rs +++ b/examples/json.rs @@ -23,7 +23,6 @@ fn main() -> io::Result<()> { Ok(Response::new(StatusCode::Ok).body_json(&cat)?) }); - app.listen("127.0.0.1:8080").await?; - Ok(()) + app.listen("127.0.0.1:8080").await }) } diff --git a/examples/nested.rs b/examples/nested.rs index 459e06fef..1383983b4 100644 --- a/examples/nested.rs +++ b/examples/nested.rs @@ -9,6 +9,5 @@ async fn main() -> Result<(), std::io::Error> { .get(|_| async move { Ok("Goodbye, world") }); api }); - app.listen("127.0.0.1:8080").await?; - Ok(()) + app.listen("127.0.0.1:8080").await } diff --git a/examples/sse.rs b/examples/sse.rs index 9e2dcdb23..51f8b2458 100644 --- a/examples/sse.rs +++ b/examples/sse.rs @@ -8,6 +8,5 @@ async fn main() -> Result<(), std::io::Error> { sender.send("fruit", "apple", None).await; Ok(()) })); - app.listen("localhost:8080").await?; - Ok(()) + app.listen("localhost:8080").await } diff --git a/examples/static_file.rs b/examples/static_file.rs index 6c15ca251..e6564cc4f 100644 --- a/examples/static_file.rs +++ b/examples/static_file.rs @@ -7,7 +7,6 @@ fn main() -> Result<(), std::io::Error> { let mut app = tide::new(); app.at("/").get(|_| async move { Ok("visit /src/*") }); app.at("/src").serve_dir("src/")?; - app.listen("127.0.0.1:8080").await?; - Ok(()) + app.listen("127.0.0.1:8080").await }) } diff --git a/src/lib.rs b/src/lib.rs index 169e6de23..ab6a9d7f3 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,9 +30,9 @@ //! # //! let mut app = tide::new(); //! app.at("/").get(|_| async move { Ok("Hello, world!") }); -//! app.listen("127.0.0.1:8080").await?; +//! app.listen("127.0.0.1:8080").await //! # -//! # Ok(()) }) } +//! # }) } //! ``` //! //! __echo server__ @@ -42,9 +42,9 @@ //! # //! let mut app = tide::new(); //! app.at("/").get(|req| async move { Ok(req) }); -//! app.listen("127.0.0.1:8080").await?; +//! app.listen("127.0.0.1:8080").await //! # -//! # Ok(()) }) } +//! # }) } //! ```` //! //! __send and receive json__ @@ -63,9 +63,9 @@ //! counter.count += 1; //! Ok(Response::new(tide::http::StatusCode::Ok).body_json(&counter)?) //! }); -//! app.listen("127.0.0.1:8080").await?; +//! app.listen("127.0.0.1:8080").await //! # -//! # Ok(()) }) } +//! # }) } //! ``` //! //! # Concepts @@ -226,9 +226,9 @@ pub use http_types as http; /// # /// let mut app = tide::new(); /// app.at("/").get(|_| async move { Ok("Hello, world!") }); -/// app.listen("127.0.0.1:8080").await?; +/// app.listen("127.0.0.1:8080").await /// # -/// # Ok(()) }) } +/// # }) } /// ``` pub fn new() -> server::Server<()> { Server::new() @@ -261,9 +261,9 @@ pub fn new() -> server::Server<()> { /// app.at("/").get(|req: Request| async move { /// Ok(format!("Hello, {}!", &req.state().name)) /// }); -/// app.listen("127.0.0.1:8080").await?; +/// app.listen("127.0.0.1:8080").await /// # -/// # Ok(()) }) } +/// # }) } /// ``` pub fn with_state(state: State) -> server::Server where diff --git a/src/redirect/mod.rs b/src/redirect/mod.rs index f1d63185d..e0c20115d 100644 --- a/src/redirect/mod.rs +++ b/src/redirect/mod.rs @@ -11,9 +11,9 @@ //! let mut app = tide::new(); //! app.at("/").get(|_| async move { Ok("meow") }); //! app.at("/nori").get(redirect::temporary("/")); -//! app.listen("127.0.0.1:8080").await?; +//! app.listen("127.0.0.1:8080").await //! # -//! # Ok(()) }) } +//! # }) } //! ``` mod permanent; mod temporary; diff --git a/src/redirect/permanent.rs b/src/redirect/permanent.rs index 03ca18155..d3c3c3aaf 100644 --- a/src/redirect/permanent.rs +++ b/src/redirect/permanent.rs @@ -16,9 +16,9 @@ use crate::{Endpoint, Request, Response}; /// let mut app = tide::new(); /// app.at("/").get(|_| async move { Ok("meow") }); /// app.at("/nori").get(redirect::permanent("/")); -/// app.listen("127.0.0.1:8080").await?; +/// app.listen("127.0.0.1:8080").await /// # -/// # Ok(()) }) } +/// # }) } /// ``` pub fn permanent(location: impl AsRef) -> PermanentRedirect { let location = location.as_ref().to_owned(); diff --git a/src/redirect/temporary.rs b/src/redirect/temporary.rs index 9d3dc146c..f59d20a07 100644 --- a/src/redirect/temporary.rs +++ b/src/redirect/temporary.rs @@ -16,9 +16,9 @@ use crate::{Endpoint, Request, Response}; /// let mut app = tide::new(); /// app.at("/").get(|_| async move { Ok("meow") }); /// app.at("/nori").get(redirect::temporary("/")); -/// app.listen("127.0.0.1:8080").await?; +/// app.listen("127.0.0.1:8080").await /// # -/// # Ok(()) }) } +/// # }) } /// ``` pub fn temporary(location: impl AsRef) -> TemporaryRedirect { let location = location.as_ref().to_owned(); diff --git a/src/request.rs b/src/request.rs index 9afe277fc..d3bf990f9 100644 --- a/src/request.rs +++ b/src/request.rs @@ -57,9 +57,9 @@ impl Request { /// assert_eq!(req.method(), http_types::Method::Get); /// Ok("") /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) })} + /// # })} /// ``` pub fn method(&self) -> Method { self.request.method() @@ -80,9 +80,9 @@ impl Request { /// assert_eq!(req.uri(), &"/".parse::().unwrap()); /// Ok("") /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) })} + /// # })} /// ``` pub fn uri(&self) -> &Url { self.request.url() @@ -103,9 +103,9 @@ impl Request { /// assert_eq!(req.version(), Some(http_types::Version::Http1_1)); /// Ok("") /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) })} + /// # })} /// ``` pub fn version(&self) -> Option { self.request.version() @@ -126,9 +126,9 @@ impl Request { /// assert_eq!(req.header(&"X-Forwarded-For".parse().unwrap()), Some(&vec!["127.0.0.1".parse().unwrap()])); /// Ok("") /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) })} + /// # })} /// ``` pub fn header( &self, @@ -208,9 +208,9 @@ impl Request { /// let _body: Vec = req.body_bytes().await.unwrap(); /// Ok("") /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) })} + /// # })} /// ``` pub async fn body_bytes(&mut self) -> std::io::Result> { let mut buf = Vec::with_capacity(1024); @@ -243,9 +243,9 @@ impl Request { /// let _body: String = req.body_string().await.unwrap(); /// Ok("") /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) })} + /// # })} /// ``` pub async fn body_string(&mut self) -> std::io::Result { let body_bytes = self.body_bytes().await?; diff --git a/src/server/mod.rs b/src/server/mod.rs index c0f109982..b85ab0dad 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -153,9 +153,9 @@ impl Server<()> { /// # /// let mut app = tide::new(); /// app.at("/").get(|_| async move { Ok("Hello, world!") }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) }) } + /// # }) } /// ``` pub fn new() -> Server<()> { Self::with_state(()) @@ -196,9 +196,9 @@ impl Server { /// app.at("/").get(|req: Request| async move { /// Ok(format!("Hello, {}!", &req.state().name)) /// }); - /// app.listen("127.0.0.1:8080").await?; + /// app.listen("127.0.0.1:8080").await /// # - /// # Ok(()) }) } + /// # }) } /// ``` pub fn with_state(state: State) -> Server { let mut server = Server { diff --git a/src/server/route.rs b/src/server/route.rs index 102cc41b1..97b3af339 100644 --- a/src/server/route.rs +++ b/src/server/route.rs @@ -125,8 +125,7 @@ impl<'a, State: 'static> Route<'a, State> { /// async fn main() -> Result<(), std::io::Error> { /// let mut app = tide::new(); /// app.at("/public/images").serve_dir("images/")?; - /// app.listen("127.0.0.1:8080").await?; - /// Ok(()) + /// app.listen("127.0.0.1:8080").await /// } /// ``` pub fn serve_dir(&mut self, dir: impl AsRef) -> io::Result<()> { diff --git a/src/sse/mod.rs b/src/sse/mod.rs index 365b4ca45..93d6ec826 100644 --- a/src/sse/mod.rs +++ b/src/sse/mod.rs @@ -22,8 +22,8 @@ //! sender.send("fruit", "apple", None).await; //! Ok(()) //! })); -//! app.listen("localhost:8080").await?; -//! # Ok(()) }) } +//! app.listen("localhost:8080").await +//! # }) } //! ``` mod endpoint;