From 114a733d7084d65148f5134154493b89083dce15 Mon Sep 17 00:00:00 2001 From: Jeb Rosen Date: Sat, 11 Jul 2020 09:54:05 -0700 Subject: [PATCH] Use 'tokio::fs::File' in the 'pastebin' example. --- examples/pastebin/src/main.rs | 11 +++++------ site/guide/10-pastebin.md | 19 +++++++------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/examples/pastebin/src/main.rs b/examples/pastebin/src/main.rs index 0251bf11..e3179a05 100644 --- a/examples/pastebin/src/main.rs +++ b/examples/pastebin/src/main.rs @@ -6,11 +6,10 @@ mod paste_id; #[cfg(test)] mod tests; use std::io; -use std::fs::File; -use std::path::PathBuf; use rocket::Data; -use rocket::response::{content, Debug}; +use rocket::response::{content::Plain, Debug}; +use rocket::tokio::fs::File; use crate::paste_id::PasteID; @@ -23,14 +22,14 @@ async fn upload(paste: Data) -> Result> { let filename = format!("upload/{id}", id = id); let url = format!("{host}/{id}\n", host = HOST, id = id); - paste.stream_to_file(PathBuf::from(filename)).await?; + paste.stream_to_file(filename).await?; Ok(url) } #[get("/")] -fn retrieve(id: PasteID<'_>) -> Option> { +async fn retrieve(id: PasteID<'_>) -> Option> { let filename = format!("upload/{id}", id = id); - File::open(&filename).map(|f| content::Plain(f)).ok() + File::open(&filename).await.map(Plain).ok() } #[get("/")] diff --git a/site/guide/10-pastebin.md b/site/guide/10-pastebin.md index e7c394ac..e6b906fb 100644 --- a/site/guide/10-pastebin.md +++ b/site/guide/10-pastebin.md @@ -213,9 +213,6 @@ mkdir upload For the `upload` route, we'll need to `use` a few items: ```rust -use std::io; -use std::path::PathBuf; - use rocket::Data; use rocket::http::RawStr; use rocket::response::Debug; @@ -266,8 +263,6 @@ Here's our version (in `src/main.rs`): # fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Ok(()) } # } -use std::path::PathBuf; - use rocket::Data; use rocket::response::Debug; @@ -278,7 +273,7 @@ async fn upload(paste: Data) -> Result> { let url = format!("{host}/{id}\n", host = "http://localhost:8000", id = id); // Write the paste out to the file and return the URL. - paste.stream_to_file(PathBuf::from(filename)).await?; + paste.stream_to_file(filename).await?; Ok(url) } ``` @@ -335,13 +330,13 @@ paste doesn't exist. ```rust # #[macro_use] extern crate rocket; -use std::fs::File; use rocket::http::RawStr; +use rocket::tokio::fs::File; #[get("/")] -fn retrieve(id: &RawStr) -> Option { +async fn retrieve(id: &RawStr) -> Option { let filename = format!("upload/{id}", id = id); - File::open(&filename).ok() + File::open(&filename).await.ok() } ``` @@ -420,14 +415,14 @@ the `retrieve` route, preventing attacks on the `retrieve` route: ```rust # #[macro_use] extern crate rocket; -# use std::fs::File; +# use rocket::tokio::fs::File; # type PasteId = usize; #[get("/")] -fn retrieve(id: PasteId) -> Option { +async fn retrieve(id: PasteId) -> Option { let filename = format!("upload/{id}", id = id); - File::open(&filename).ok() + File::open(&filename).await.ok() } ```