Skip to content

Make async-std optional #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ features = ["docs"]
rustdoc-args = ["--cfg", "feature=\"docs\""]

[features]
default = ["async_std", "cookie-secure"]
default = ["fs", "cookie-secure"]
docs = ["unstable"]
unstable = []
hyperium_http = ["http"]
async_std = [] # "async-std" when it is not default
async_std = ["fs"]
cookie-secure = ["cookie/secure"]
fs = ["async-std"]

[dependencies]
# Note(yoshuawuyts): used for async_std's `channel` only; use "core" once possible.
# features: async_std
async-std = { version = "1.6.0", features = ["unstable"] }
async-std = { version = "1.6.0", optional = true }
futures-lite = "1.11.1"
async-channel = "1.5.1"

# features: hyperium/http
http = { version = "0.2.0", optional = true }
Expand All @@ -44,4 +46,4 @@ serde_qs = "0.7.0"

[dev-dependencies]
http = "0.2.0"
async-std = { version = "1.6.0", features = ["unstable", "attributes"] }
async-std = { version = "1.6.0", features = ["attributes"] }
37 changes: 17 additions & 20 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_std::io::prelude::*;
use async_std::io::{self, Cursor};
use futures_lite::{io, prelude::*};
use serde::{de::DeserializeOwned, Serialize};

use std::fmt::{self, Debug};
Expand Down Expand Up @@ -54,7 +53,7 @@ pin_project_lite::pin_project! {
/// and not rely on the fallback mechanisms. However, they're still there if you need them.
pub struct Body {
#[pin]
reader: Box<dyn BufRead + Unpin + Send + Sync + 'static>,
reader: Box<dyn AsyncBufRead + Unpin + Send + Sync + 'static>,
mime: Mime,
length: Option<usize>,
}
Expand Down Expand Up @@ -102,7 +101,7 @@ impl Body {
/// req.set_body(Body::from_reader(cursor, Some(len)));
/// ```
pub fn from_reader(
reader: impl BufRead + Unpin + Send + Sync + 'static,
reader: impl AsyncBufRead + Unpin + Send + Sync + 'static,
len: Option<usize>,
) -> Self {
Self {
Expand All @@ -125,7 +124,7 @@ impl Body {
/// let body = Body::from_reader(cursor, None);
/// let _ = body.into_reader();
/// ```
pub fn into_reader(self) -> Box<dyn BufRead + Unpin + Send + Sync + 'static> {
pub fn into_reader(self) -> Box<dyn AsyncBufRead + Unpin + Send + Sync + 'static> {
self.reader
}

Expand Down Expand Up @@ -160,7 +159,7 @@ impl Body {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::Body;
///
/// let bytes = vec![1, 2, 3];
Expand Down Expand Up @@ -209,9 +208,7 @@ impl Body {
/// # Examples
///
/// ```
/// # use std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::Body;
/// use async_std::io::Cursor;
///
Expand Down Expand Up @@ -246,7 +243,7 @@ impl Body {
let bytes = serde_json::to_vec(&json)?;
let body = Self {
length: Some(bytes.len()),
reader: Box::new(Cursor::new(bytes)),
reader: Box::new(io::Cursor::new(bytes)),
mime: mime::JSON,
};
Ok(body)
Expand All @@ -257,7 +254,7 @@ impl Body {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::Body;
/// use http_types::convert::{Serialize, Deserialize};
///
Expand Down Expand Up @@ -290,7 +287,7 @@ impl Body {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::Body;
/// use http_types::convert::{Serialize, Deserialize};
///
Expand All @@ -310,7 +307,7 @@ impl Body {

let body = Self {
length: Some(bytes.len()),
reader: Box::new(Cursor::new(bytes)),
reader: Box::new(io::Cursor::new(bytes)),
mime: mime::FORM,
};
Ok(body)
Expand All @@ -326,7 +323,7 @@ impl Body {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::Body;
/// use http_types::convert::{Serialize, Deserialize};
///
Expand All @@ -353,14 +350,14 @@ impl Body {
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::{Body, Response, StatusCode};
///
/// let mut res = Response::new(StatusCode::Ok);
/// res.set_body(Body::from_file("/path/to/file").await?);
/// # Ok(()) }) }
/// ```
#[cfg(all(feature = "async_std", not(target_os = "unknown")))]
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
pub async fn from_file<P>(path: P) -> io::Result<Self>
where
P: AsRef<std::path::Path>,
Expand Down Expand Up @@ -455,7 +452,7 @@ impl<'a> From<&'a [u8]> for Body {
}
}

impl Read for Body {
impl AsyncRead for Body {
#[allow(missing_doc_code_examples)]
fn poll_read(
mut self: Pin<&mut Self>,
Expand All @@ -466,7 +463,7 @@ impl Read for Body {
}
}

impl BufRead for Body {
impl AsyncBufRead for Body {
#[allow(missing_doc_code_examples)]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
let this = self.project();
Expand All @@ -480,7 +477,7 @@ impl BufRead for Body {

/// Look at first few bytes of a file to determine the mime type.
/// This is used for various binary formats such as images and videos.
#[cfg(all(feature = "async_std", not(target_os = "unknown")))]
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
async fn peek_mime(file: &mut async_std::fs::File) -> io::Result<Option<Mime>> {
// We need to read the first 300 bytes to correctly infer formats such as tar.
let mut buf = [0_u8; 300];
Expand All @@ -494,7 +491,7 @@ async fn peek_mime(file: &mut async_std::fs::File) -> io::Result<Option<Mime>> {

/// Look at the extension of a file to determine the mime type.
/// This is useful for plain-text formats such as HTML and CSS.
#[cfg(all(feature = "async_std", not(target_os = "unknown")))]
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
fn guess_ext(path: &std::path::Path) -> Option<Mime> {
let ext = path.extension().map(|p| p.to_str()).flatten();
ext.and_then(Mime::from_extension)
Expand Down
31 changes: 13 additions & 18 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_std::io::{self, BufRead, Read};
use async_std::sync;
use futures_lite::{io, prelude::*};

use std::convert::{Into, TryInto};
use std::mem;
Expand Down Expand Up @@ -38,8 +37,8 @@ pin_project_lite::pin_project! {
local_addr: Option<String>,
peer_addr: Option<String>,
ext: Extensions,
trailers_sender: Option<sync::Sender<Trailers>>,
trailers_receiver: Option<sync::Receiver<Trailers>>,
trailers_sender: Option<async_channel::Sender<Trailers>>,
trailers_receiver: Option<async_channel::Receiver<Trailers>>,
has_trailers: bool,
}
}
Expand All @@ -52,7 +51,7 @@ impl Request {
U::Error: std::fmt::Debug,
{
let url = url.try_into().expect("Could not convert into a valid url");
let (trailers_sender, trailers_receiver) = sync::channel(1);
let (trailers_sender, trailers_receiver) = async_channel::bounded(1);
Self {
method,
url,
Expand Down Expand Up @@ -207,8 +206,7 @@ impl Request {
///
/// ```
/// # use async_std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// #
/// use http_types::{Body, Method, Request, Url};
///
Expand All @@ -234,8 +232,7 @@ impl Request {
///
/// ```
/// # use async_std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// #
/// use http_types::{Body, Method, Request, Url};
///
Expand All @@ -261,8 +258,7 @@ impl Request {
///
/// ```
/// # use async_std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// #
/// use http_types::{Body, Method, Request, Url};
///
Expand Down Expand Up @@ -295,8 +291,7 @@ impl Request {
///
/// ```
/// # use std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use async_std::io::Cursor;
/// use http_types::{Body, Method, Request, Url};
///
Expand All @@ -323,7 +318,7 @@ impl Request {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::{Body, Method, Request, Url};
///
/// let bytes = vec![1, 2, 3];
Expand All @@ -349,7 +344,7 @@ impl Request {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::convert::{Deserialize, Serialize};
/// use http_types::{Body, Method, Request, Url};
///
Expand Down Expand Up @@ -383,7 +378,7 @@ impl Request {
/// # Examples
///
/// ```
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// use http_types::convert::{Deserialize, Serialize};
/// use http_types::{Body, Method, Request, Url};
///
Expand Down Expand Up @@ -886,7 +881,7 @@ impl Clone for Request {
}
}

impl Read for Request {
impl AsyncRead for Request {
#[allow(missing_doc_code_examples)]
fn poll_read(
mut self: Pin<&mut Self>,
Expand All @@ -897,7 +892,7 @@ impl Read for Request {
}
}

impl BufRead for Request {
impl AsyncBufRead for Request {
#[allow(missing_doc_code_examples)]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
let this = self.project();
Expand Down
Loading