Skip to content

Removed obligatory async-std dep #168

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

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 18 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ features = ["docs"]
rustdoc-args = ["--cfg", "feature=\"docs\""]

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

[dependencies.futures-io]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using futures and piper I would suggest using async-std with the default features, especially the runtime turned off. This way nothing changes in terms of code, but you are not forced to pull in the async-std runtime anymore, just the tooling like channels etc.

Copy link
Member

@dignifiedquire dignifiedquire Jun 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with async-rs/async-std#806 this

async-std = { version = "1.6", default-features = false, features = ["unstable"] }

should not pull in any runtime

default-features = false
features = ["std"]
version = "0.3"

[dependencies.futures-util]
default-features = false
features = ["std","sink"]
version = "0.3"

[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, features = ["unstable"] }

# features: hyperium/http
http = { version = "0.2.0", optional = true }
Expand All @@ -35,10 +45,11 @@ anyhow = "1.0.26"
cookie = { version = "0.13.0", features = ["percent-encode"] }
infer = "0.1.2"
pin-project-lite = "0.1.0"
url = "2.1.1"
serde_json = "1.0.51"
serde = { version = "1.0.106", features = ["derive"] }
serde_urlencoded = "0.6.1"
url = "*"
serde_json = "*"
serde = { version = "*", features = ["derive"] }
serde_urlencoded = "*"
async-channel = "1"

[dev-dependencies]
http = "0.2.0"
Expand Down
31 changes: 17 additions & 14 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::io::prelude::*;
use async_std::io::{self, Cursor};
//use async_std::io::prelude::*;
//use async_std::io::{self, Cursor};
use serde::{de::DeserializeOwned, Serialize};

use std::fmt::{self, Debug};
Expand All @@ -8,6 +8,9 @@ use std::task::{Context, Poll};

use crate::{mime, Mime};
use crate::{Status, StatusCode};
use futures_io::{AsyncRead, AsyncBufRead};
use futures_util::{AsyncReadExt};
use futures_util::io::Cursor;

pin_project_lite::pin_project! {
/// A streaming HTTP body.
Expand Down Expand Up @@ -54,7 +57,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 All @@ -76,7 +79,7 @@ impl Body {
/// ```
pub fn empty() -> Self {
Self {
reader: Box::new(io::empty()),
reader: Box::new(futures_util::io::empty()),
mime: mime::BYTE_STREAM,
length: Some(0),
}
Expand All @@ -102,7 +105,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 +128,7 @@ impl Body {
/// let body = Body::from_reader(cursor, None);
/// let _ = body.into_reader();
/// ```
pub fn into_reader(self) -> Box<dyn BufRead + Unpin + Send + 'static> {
pub fn into_reader(self) -> Box<dyn AsyncBufRead + Unpin + Send + 'static> {
self.reader
}

Expand All @@ -151,7 +154,7 @@ impl Body {
Self {
mime: mime::BYTE_STREAM,
length: Some(bytes.len()),
reader: Box::new(io::Cursor::new(bytes)),
reader: Box::new(Cursor::new(bytes)),
}
}

Expand Down Expand Up @@ -200,7 +203,7 @@ impl Body {
Self {
mime: mime::PLAIN,
length: Some(s.len()),
reader: Box::new(io::Cursor::new(s.into_bytes())),
reader: Box::new(Cursor::new(s.into_bytes())),
}
}

Expand Down Expand Up @@ -449,20 +452,20 @@ 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>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
) -> Poll<futures_io::Result<usize>> {
Pin::new(&mut self.reader).poll_read(cx, buf)
}
}

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]>> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<futures_io::Result<&'_ [u8]>> {
let this = self.project();
this.reader.poll_fill_buf(cx)
}
Expand All @@ -475,14 +478,14 @@ 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")))]
async fn peek_mime(file: &mut async_std::fs::File) -> io::Result<Option<Mime>> {
async fn peek_mime(file: &mut async_std::fs::File) -> futures_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];
file.read(&mut buf).await?;
let mime = Mime::sniff(&buf).ok();

// Reset the file cursor back to the start.
file.seek(io::SeekFrom::Start(0)).await?;
file.seek(futures_io::SeekFrom::Start(0)).await?;
Ok(mime)
}

Expand Down
19 changes: 10 additions & 9 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::io::{self, BufRead, Read};
use async_std::sync;
//use async_std::io::{self, BufRead, Read};
//use async_std::sync;

use std::convert::{Into, TryInto};
use std::mem;
Expand All @@ -15,6 +15,7 @@ use crate::headers::{
use crate::mime::Mime;
use crate::trailers::{self, Trailers};
use crate::{Body, Extensions, Method, StatusCode, Url, Version};
use futures_io::{AsyncRead, AsyncBufRead};

pin_project_lite::pin_project! {
/// An HTTP request.
Expand All @@ -38,8 +39,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>>,
}
}

Expand All @@ -51,7 +52,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 @@ -877,20 +878,20 @@ 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>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
) -> Poll<futures_io::Result<usize>> {
Pin::new(&mut self.body).poll_read(cx, buf)
}
}

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]>> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<futures_io::Result<&'_ [u8]>> {
let this = self.project();
this.body.poll_fill_buf(cx)
}
Expand Down
33 changes: 17 additions & 16 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::io::{self, BufRead, Read};
use async_std::sync;
//use async_std::io::{self, BufRead, Read};
//use async_std::sync;

use std::convert::{Into, TryInto};
use std::fmt::Debug;
Expand All @@ -16,6 +16,7 @@ use crate::headers::{
use crate::mime::Mime;
use crate::trailers::{self, Trailers};
use crate::{Body, Extensions, StatusCode, Version};
use futures_io::{AsyncRead, AsyncBufRead};

cfg_unstable! {
use crate::upgrade;
Expand All @@ -42,8 +43,8 @@ pin_project_lite::pin_project! {
status: StatusCode,
headers: Headers,
version: Option<Version>,
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>>,
#[pin]
body: Body,
ext: Extensions,
Expand Down Expand Up @@ -73,10 +74,10 @@ pin_project_lite::pin_project! {
status: StatusCode,
headers: Headers,
version: Option<Version>,
trailers_sender: Option<sync::Sender<Trailers>>,
trailers_receiver: Option<sync::Receiver<Trailers>>,
upgrade_sender: Option<sync::Sender<upgrade::Connection>>,
upgrade_receiver: Option<sync::Receiver<upgrade::Connection>>,
trailers_sender: Option<async_channel::Sender<Trailers>>,
trailers_receiver: Option<async_channel::Receiver<Trailers>>,
upgrade_sender: Option<async_channel::Sender<upgrade::Connection>>,
upgrade_receiver: Option<async_channel::Receiver<upgrade::Connection>>,
has_upgrade: bool,
#[pin]
body: Body,
Expand All @@ -97,7 +98,7 @@ impl Response {
let status = status
.try_into()
.expect("Could not convert into a valid `StatusCode`");
let (trailers_sender, trailers_receiver) = sync::channel(1);
let (trailers_sender, trailers_receiver) = async_channel::bounded(1);
Self {
status,
headers: Headers::new(),
Expand All @@ -121,8 +122,8 @@ impl Response {
let status = status
.try_into()
.expect("Could not convert into a valid `StatusCode`");
let (trailers_sender, trailers_receiver) = sync::channel(1);
let (upgrade_sender, upgrade_receiver) = sync::channel(1);
let (trailers_sender, trailers_receiver) = async_channel::bounded(1);
let (upgrade_sender, upgrade_receiver) = async_channel::bounded(1);
Self {
status,
headers: Headers::new(),
Expand Down Expand Up @@ -268,7 +269,7 @@ impl Response {
/// req.swap_body(&mut body);
///
/// let mut string = String::new();
/// body.read_to_string(&mut string).await?;
/// body.read_to(&mut string).await?;
/// assert_eq!(&string, "Hello, Nori!");
/// #
/// # Ok(()) }) }
Expand Down Expand Up @@ -654,20 +655,20 @@ impl Clone for Response {
}
}

impl Read for Response {
impl AsyncRead for Response {
#[allow(missing_doc_code_examples)]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
) -> Poll<futures_io::Result<usize>> {
Pin::new(&mut self.body).poll_read(cx, buf)
}
}

impl BufRead for Response {
impl AsyncBufRead for Response {
#[allow(missing_doc_code_examples)]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<futures_io::Result<&'_ [u8]>> {
let this = self.project();
this.body.poll_fill_buf(cx)
}
Expand Down
15 changes: 8 additions & 7 deletions src/trailers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@
use crate::headers::{
HeaderName, HeaderValues, Headers, Iter, IterMut, Names, ToHeaderValues, Values,
};
use async_std::prelude::*;
use async_std::sync;
//use async_std::prelude::*;
//use async_std::sync;

use std::convert::Into;
use std::future::Future;
use std::ops::{Deref, DerefMut, Index};
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_util::stream::Stream;

/// A collection of trailing HTTP headers.
#[derive(Debug)]
Expand Down Expand Up @@ -212,21 +213,21 @@ impl Index<&str> for Trailers {
/// `Trailers` should be created.
#[derive(Debug)]
pub struct Sender {
sender: sync::Sender<Trailers>,
sender: async_channel::Sender<Trailers>,
}

impl Sender {
/// Create a new instance of `Sender`.
#[doc(hidden)]
pub fn new(sender: sync::Sender<Trailers>) -> Self {
pub fn new(sender: async_channel::Sender<Trailers>) -> Self {
Self { sender }
}

/// Send a `Trailer`.
///
/// The channel will be consumed after having sent trailers.
pub async fn send(self, trailers: Trailers) {
self.sender.send(trailers).await
let _ = self.sender.send(trailers).await;
}
}

Expand All @@ -238,12 +239,12 @@ impl Sender {
#[must_use = "Futures do nothing unless polled or .awaited"]
#[derive(Debug)]
pub struct Receiver {
receiver: sync::Receiver<Trailers>,
receiver: async_channel::Receiver<Trailers>,
}

impl Receiver {
/// Create a new instance of `Receiver`.
pub(crate) fn new(receiver: sync::Receiver<Trailers>) -> Self {
pub(crate) fn new(receiver: async_channel::Receiver<Trailers>) -> Self {
Self { receiver }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/upgrade/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use async_std::io::{self, prelude::*};
//use async_std::io::{self, prelude::*};

use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down
4 changes: 2 additions & 2 deletions src/upgrade/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::prelude::*;
use async_std::sync;
//use async_std::prelude::*;
//use async_std::sync;

use std::future::Future;
use std::pin::Pin;
Expand Down
2 changes: 1 addition & 1 deletion src/upgrade/sender.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use async_std::sync;
//use async_std::sync;

use crate::upgrade::Connection;

Expand Down