Use 'File' from 'async_std' instead of from 'tokio'.

Additionally pin tokio to '=0.2.0-alpha.2'; before this change cargo
selects '0.2.0-alpha.3' which hyper does not compile against.
This commit is contained in:
Jacob Pratt 2019-08-28 21:32:32 -04:00 committed by Sergio Benitez
parent 1f0577bfc5
commit 05426881b0
8 changed files with 19 additions and 28 deletions

View File

@ -28,7 +28,7 @@ rocket_codegen = { version = "0.5.0-dev", path = "../codegen" }
rocket_http = { version = "0.5.0-dev", path = "../http" } rocket_http = { version = "0.5.0-dev", path = "../http" }
futures-preview = "0.3.0-alpha.18" futures-preview = "0.3.0-alpha.18"
futures-tokio-compat = { git = "https://github.com/Nemo157/futures-tokio-compat", rev = "8a93702" } futures-tokio-compat = { git = "https://github.com/Nemo157/futures-tokio-compat", rev = "8a93702" }
tokio = "0.2.0-alpha.2" tokio = "=0.2.0-alpha.2"
yansi = "0.5" yansi = "0.5"
log = { version = "0.4", features = ["std"] } log = { version = "0.4", features = ["std"] }
toml = "0.4.7" toml = "0.4.7"
@ -39,6 +39,7 @@ memchr = "2" # TODO: Use pear instead.
binascii = "0.1" binascii = "0.1"
pear = "0.1" pear = "0.1"
atty = "0.2" atty = "0.2"
async-std = "0.99.4"
[build-dependencies] [build-dependencies]
yansi = "0.5" yansi = "0.5"

View File

@ -3,7 +3,6 @@ use std::path::Path;
use futures::io::{self, AsyncRead, AsyncReadExt as _, AsyncWrite}; use futures::io::{self, AsyncRead, AsyncReadExt as _, AsyncWrite};
use futures::future::Future; use futures::future::Future;
use futures::stream::TryStreamExt; use futures::stream::TryStreamExt;
use futures_tokio_compat::Compat as TokioCompat;
use super::data_stream::DataStream; use super::data_stream::DataStream;
@ -171,7 +170,7 @@ impl Data {
#[inline(always)] #[inline(always)]
pub fn stream_to_file<P: AsRef<Path> + Send + Unpin + 'static>(self, path: P) -> impl Future<Output = io::Result<u64>> { pub fn stream_to_file<P: AsRef<Path> + Send + Unpin + 'static>(self, path: P) -> impl Future<Output = io::Result<u64>> {
Box::pin(async move { Box::pin(async move {
let mut file = TokioCompat::new(tokio::fs::File::create(path).await?); let mut file = async_std::fs::File::create(path).await?;
self.stream_to(&mut file).await self.stream_to(&mut file).await
}) })
} }

View File

@ -2,7 +2,6 @@ use std::fs::File;
use std::io::Cursor; use std::io::Cursor;
use futures::io::BufReader; use futures::io::BufReader;
use futures_tokio_compat::Compat as TokioCompat;
use crate::http::{Status, ContentType, StatusClass}; use crate::http::{Status, ContentType, StatusClass};
use crate::response::{self, Response, Body}; use crate::response::{self, Response, Body};
@ -255,8 +254,9 @@ impl Responder<'_> for Vec<u8> {
impl Responder<'_> for File { impl Responder<'_> for File {
fn respond_to(self, _: &Request<'_>) -> response::ResultFuture<'static> { fn respond_to(self, _: &Request<'_>) -> response::ResultFuture<'static> {
Box::pin(async move { Box::pin(async move {
let metadata = self.metadata(); let file = async_std::fs::File::from(self);
let stream = BufReader::new(TokioCompat::new(tokio::fs::File::from_std(self))); let metadata = file.metadata().await;
let stream = BufReader::new(file);
match metadata { match metadata {
Ok(md) => Response::build().raw_body(Body::Sized(stream, md.len())).ok(), Ok(md) => Response::build().raw_body(Body::Sized(stream, md.len())).ok(),
Err(_) => Response::build().streamed_body(stream).ok() Err(_) => Response::build().streamed_body(stream).ok()

View File

@ -346,15 +346,14 @@ impl<'r> ResponseBuilder<'r> {
/// ///
/// ```rust,ignore /// ```rust,ignore
/// use rocket::Response; /// use rocket::Response;
/// use futures_tokio_compat::Compat as TokioCompat; /// use async_std::fs::File;
/// use tokio::fs::File;
/// # use std::io; /// # use std::io;
/// ///
/// # #[allow(dead_code)] /// # #[allow(dead_code)]
/// # async fn test() -> io::Result<()> { /// # async fn test() -> io::Result<()> {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let response = Response::build() /// let response = Response::build()
/// .sized_body(TokioCompat::new(File::open("body.txt").await?)) /// .sized_body(File::open("body.txt").await?)
/// .finalize(); /// .finalize();
/// # Ok(()) /// # Ok(())
/// # } /// # }
@ -373,15 +372,14 @@ impl<'r> ResponseBuilder<'r> {
/// ///
/// ```rust /// ```rust
/// use rocket::Response; /// use rocket::Response;
/// use futures_tokio_compat::Compat as TokioCompat; /// use async_std::fs::File;
/// use tokio::fs::File;
/// # use std::io; /// # use std::io;
/// ///
/// # #[allow(dead_code)] /// # #[allow(dead_code)]
/// # async fn test() -> io::Result<()> { /// # async fn test() -> io::Result<()> {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let response = Response::build() /// let response = Response::build()
/// .streamed_body(TokioCompat::new(File::open("body.txt").await?)) /// .streamed_body(File::open("body.txt").await?)
/// .finalize(); /// .finalize();
/// # Ok(()) /// # Ok(())
/// # } /// # }
@ -401,15 +399,14 @@ impl<'r> ResponseBuilder<'r> {
/// ///
/// ```rust /// ```rust
/// use rocket::Response; /// use rocket::Response;
/// use futures_tokio_compat::Compat as TokioCompat; /// use async_std::fs::File;
/// use tokio::fs::File;
/// # use std::io; /// # use std::io;
/// ///
/// # #[allow(dead_code)] /// # #[allow(dead_code)]
/// # async fn test() -> io::Result<()> { /// # async fn test() -> io::Result<()> {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let response = Response::build() /// let response = Response::build()
/// .chunked_body(TokioCompat::new(File::open("body.txt").await?), 8096) /// .chunked_body(File::open("body.txt").await?, 8096)
/// .finalize(); /// .finalize();
/// # Ok(()) /// # Ok(())
/// # } /// # }

View File

@ -7,5 +7,4 @@ publish = false
[dependencies] [dependencies]
rocket = { path = "../../core/lib" } rocket = { path = "../../core/lib" }
tokio = "0.2.0-alpha.2" async-std = "0.99.4"
futures-tokio-compat = { git = "https://github.com/Nemo157/futures-tokio-compat" }

View File

@ -4,9 +4,7 @@ extern crate rocket;
mod tests; mod tests;
use std::env; use std::env;
use async_std::fs::File;
use futures_tokio_compat::Compat as TokioCompat;
use tokio::fs::File;
use rocket::{Request, Handler, Route, Data, Catcher, try_outcome}; use rocket::{Request, Handler, Route, Data, Catcher, try_outcome};
use rocket::http::{Status, RawStr}; use rocket::http::{Status, RawStr};
@ -52,7 +50,7 @@ fn upload<'r>(req: &'r Request, data: Data) -> HandlerFuture<'r> {
let file = File::create(env::temp_dir().join("upload.txt")).await; let file = File::create(env::temp_dir().join("upload.txt")).await;
if let Ok(file) = file { if let Ok(file) = file {
if let Ok(n) = data.stream_to(TokioCompat::new(file)).await { if let Ok(n) = data.stream_to(file).await {
return Outcome::from(req, format!("OK: {} bytes uploaded.", n)).await; return Outcome::from(req, format!("OK: {} bytes uploaded.", n)).await;
} }

View File

@ -8,5 +8,4 @@ publish = false
[dependencies] [dependencies]
rocket = { path = "../../core/lib" } rocket = { path = "../../core/lib" }
futures-preview = "0.3.0-alpha.18" futures-preview = "0.3.0-alpha.18"
tokio = "0.2.0-alpha.2" async-std = "0.99.4"
futures-tokio-compat = { git = "https://github.com/Nemo157/futures-tokio-compat" }

View File

@ -7,9 +7,7 @@
use rocket::response::{content, Stream}; use rocket::response::{content, Stream};
use std::io::repeat; use std::io::repeat;
use async_std::fs::File;
use tokio::fs::File;
use futures_tokio_compat::Compat as TokioCompat;
use rocket::AsyncReadExt as _; use rocket::AsyncReadExt as _;
@ -25,8 +23,8 @@ fn root() -> content::Plain<Stream<LimitedRepeat>> {
} }
#[get("/big_file")] #[get("/big_file")]
async fn file() -> Option<Stream<TokioCompat<File>>> { async fn file() -> Option<Stream<File>> {
File::open(FILENAME).await.map(|file| Stream::from(TokioCompat::new(file))).ok() File::open(FILENAME).await.map(Stream::from).ok()
} }
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {