diff --git a/Cargo.toml b/Cargo.toml index fea3aa1e..d5b6b4f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,10 @@ categories = ["network-programming", "asynchronous"] readme = "README.md" [features] -default = ["apollo_tracing", "bson", "chrono", "chrono-tz", "log", "multipart", "tracing", "url"] +default = ["apollo_tracing", "bson", "chrono", "chrono-tz", "log", "multipart", "tracing", "url", "unblock"] apollo_tracing = ["chrono"] multipart = ["multer", "bytes", "tempfile"] +unblock = ["blocking"] # Used for doc(cfg()) nightly = [] @@ -50,6 +51,7 @@ url = { version = "2.1.1", optional = true } bytes = { version = "0.5.4", optional = true } multer = { version = "1.2.2", optional = true } tempfile = { version = "3.1.0", optional = true } +blocking = { version = "1.0.0", optional = true } [dev-dependencies] async-std = { version = "1.5.0", features = ["attributes"] } diff --git a/src/lib.rs b/src/lib.rs index 8ccfcb3f..dcef4baf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,7 +100,8 @@ //! extension](extensions/struct.ApolloTracing.html). //! - `log`: Enable the [logger extension](extensions/struct.Logger.html). //! - `tracing`: Enable the [tracing extension](extensions/struct.Tracing.html). -//! - `multipart`: Support [sending files over HTTP multipart](http/fn.receive_multipart.html). +//! - `multipart`: Support [sending files over HTTP multipart](http/fn.receive_body.html). +//! - `unblock`: Support [asynchronous reader for Upload](http/struct.Upload.html) //! - `bson`: Integrate with the [`bson` crate](https://crates.io/crates/bson). //! - `chrono`: Integrate with the [`chrono` crate](https://crates.io/crates/chrono). //! - `chrono-tz`: Integrate with the [`chrono-tz` crate](https://crates.io/crates/chrono-tz). diff --git a/src/types/upload.rs b/src/types/upload.rs index d62c4437..e97de74e 100644 --- a/src/types/upload.rs +++ b/src/types/upload.rs @@ -1,5 +1,7 @@ use crate::parser::types::UploadValue; use crate::{registry, InputValueError, InputValueResult, InputValueType, Type, Value}; +use blocking::Unblock; +use futures::AsyncRead; use std::borrow::Cow; use std::io::Read; @@ -66,6 +68,13 @@ impl Upload { pub fn into_read(self) -> impl Read + Sync + Send + 'static { self.0.content } + + #[cfg(feature = "unblock")] + #[cfg_attr(feature = "nightly", doc(cfg(feature = "unblock")))] + /// Convert to a `AsyncRead`. + pub fn into_async_read(self) -> impl AsyncRead + Sync + Send + 'static { + Unblock::new(self.0.content) + } } impl Type for Upload {