Skip to content
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

Replace async-std with smol crates #201

Merged
merged 3 commits into from
Oct 8, 2023
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
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ readme = "README.md"
edition = "2018"

[dependencies]
httparse = "1.3.4"
async-std = "1.7.0"
async-channel = "1.5.1"
async-dup = "1.2.2"
async-global-executor = "2.3.1"
async-io = "1.13.0"
futures-lite = "1.13.0"
http-types = { version = "2.9.0", default-features = false }
futures-core = "0.3.8"
httparse = "1.3.4"
log = "0.4.11"
pin-project = "1.0.2"
async-channel = "1.5.1"
async-dup = "1.2.2"

[dev-dependencies]
pretty_assertions = "0.6.1"
async-std = { version = "1.7.0", features = ["attributes"] }
pretty_assertions = "0.6.1"
2 changes: 1 addition & 1 deletion src/body_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

use async_std::io::Read;
use futures_lite::io::AsyncRead as Read;
use http_types::Body;
use pin_project::pin_project;

Expand Down
4 changes: 2 additions & 2 deletions src/chunked/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::pin::Pin;
use std::task::{Context, Poll};

use async_std::io::{self, Read};
use futures_core::ready;
use futures_lite::io::{self, AsyncRead as Read};
use futures_lite::ready;
use http_types::trailers::{Sender, Trailers};

/// Decodes a chunked body according to
Expand Down Expand Up @@ -120,7 +120,7 @@
}

impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
#[allow(missing_doc_code_examples)]

Check warning on line 123 in src/chunked/decoder.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, nightly)

unknown lint: `rustdoc::missing_doc_code_examples`

Check warning on line 123 in src/chunked/decoder.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, nightly)

lint `missing_doc_code_examples` has been renamed to `rustdoc::missing_doc_code_examples`

Check warning on line 123 in src/chunked/decoder.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, nightly)

unknown lint: `rustdoc::missing_doc_code_examples`

Check warning on line 123 in src/chunked/decoder.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, nightly)

lint `missing_doc_code_examples` has been renamed to `rustdoc::missing_doc_code_examples`
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
7 changes: 3 additions & 4 deletions src/chunked/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::pin::Pin;
use std::task::{Context, Poll};

use async_std::io;
use async_std::io::prelude::*;
use async_std::task::{Context, Poll};
use futures_core::ready;
use futures_lite::io::AsyncRead as Read;
use futures_lite::{io, ready};

/// An encoder for chunked encoding.
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions src/client/decode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::io::{BufReader, Read};
use async_std::prelude::*;
use futures_lite::io::{AsyncRead as Read, BufReader};
use futures_lite::prelude::*;
use http_types::{ensure, ensure_eq, format_err};
use http_types::{
headers::{CONTENT_LENGTH, DATE, TRANSFER_ENCODING},
Expand Down
4 changes: 2 additions & 2 deletions src/client/encode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::Write;
use std::pin::Pin;
use std::task::{Context, Poll};

use async_std::io::{self, Cursor, Read};
use async_std::task::{Context, Poll};
use futures_lite::io::{self, AsyncRead as Read, Cursor};
use http_types::headers::{CONTENT_LENGTH, HOST, TRANSFER_ENCODING};
use http_types::{Method, Request};

Expand Down
2 changes: 1 addition & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Process HTTP connections on the client.

use async_std::io::{self, Read, Write};
use futures_lite::io::{self, AsyncRead as Read, AsyncWrite as Write};
use http_types::{Request, Response};

mod decode;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, nonstandard_style, rust_2018_idioms)]
#![warn(missing_docs, missing_doc_code_examples, unreachable_pub)]

Check warning on line 93 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, nightly)

unknown lint: `rustdoc::missing_doc_code_examples`

Check warning on line 93 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, nightly)

lint `missing_doc_code_examples` has been renamed to `rustdoc::missing_doc_code_examples`

Check warning on line 93 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, nightly)

unknown lint: `rustdoc::missing_doc_code_examples`

Check warning on line 93 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, nightly)

lint `missing_doc_code_examples` has been renamed to `rustdoc::missing_doc_code_examples`
#![cfg_attr(test, deny(warnings))]
#![allow(clippy::if_same_then_else)]
#![allow(clippy::len_zero)]
Expand All @@ -112,9 +112,9 @@
pub mod client;
pub mod server;

use async_std::io::Cursor;
use body_encoder::BodyEncoder;
pub use client::connect;
use futures_lite::io::Cursor;
pub use server::{accept, accept_with_opts, ServerOptions};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/read_notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};

use async_channel::Sender;
use async_std::io::{self, BufRead, Read};
use futures_lite::io::{self, AsyncBufRead as BufRead, AsyncRead as Read};

/// ReadNotifier forwards [`async_std::io::Read`] and
/// [`async_std::io::BufRead`] to an inner reader. When the
Expand Down
10 changes: 7 additions & 3 deletions src/server/body_reader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::chunked::ChunkedDecoder;
use async_dup::{Arc, Mutex};
use async_std::io::{BufReader, Read, Take};
use async_std::task::{Context, Poll};
use std::{fmt::Debug, io, pin::Pin};
use futures_lite::io::{AsyncRead as Read, BufReader, Take};
use std::{
fmt::Debug,
io,
pin::Pin,
task::{Context, Poll},
};

pub enum BodyReader<IO: Read + Unpin> {
Chunked(Arc<Mutex<ChunkedDecoder<BufReader<IO>>>>),
Expand Down
9 changes: 5 additions & 4 deletions src/server/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::str::FromStr;

use async_dup::{Arc, Mutex};
use async_std::io::{BufReader, Read, Write};
use async_std::{prelude::*, task};
use futures_lite::io::{AsyncRead as Read, AsyncWrite as Write, BufReader};
use futures_lite::prelude::*;
use http_types::content::ContentLength;
use http_types::headers::{EXPECT, TRANSFER_ENCODING};
use http_types::{ensure, ensure_eq, format_err};
Expand Down Expand Up @@ -103,7 +103,7 @@ where
let (body_read_sender, body_read_receiver) = async_channel::bounded(1);

if Some(CONTINUE_HEADER_VALUE) == req.header(EXPECT).map(|h| h.as_str()) {
task::spawn(async move {
async_global_executor::spawn(async move {
// If the client expects a 100-continue header, spawn a
// task to wait for the first read attempt on the body.
if let Ok(()) = body_read_receiver.recv().await {
Expand All @@ -112,7 +112,8 @@ where
// Since the sender is moved into the Body, this task will
// finish when the client disconnects, whether or not
// 100-continue was sent.
});
})
.detach();
}

// Check for Transfer-Encoding
Expand Down
4 changes: 2 additions & 2 deletions src/server/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use std::io::Write;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::SystemTime;

use async_std::io::{self, Cursor, Read};
use async_std::task::{Context, Poll};
use futures_lite::io::{self, AsyncRead as Read, Cursor};
use http_types::headers::{CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
use http_types::{Method, Response};

Expand Down
21 changes: 14 additions & 7 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Process HTTP connections on the server.

use async_std::future::{timeout, Future, TimeoutError};
use async_std::io::{self, Read, Write};
use async_io::Timer;
use futures_lite::io::{self, AsyncRead as Read, AsyncWrite as Write};
use futures_lite::prelude::*;
use http_types::headers::{CONNECTION, UPGRADE};
use http_types::upgrade::Connection;
use http_types::{Request, Response, StatusCode};
use std::{marker::PhantomData, time::Duration};
use std::{future::Future, marker::PhantomData, time::Duration};
mod body_reader;
mod decode;
mod encode;
Expand Down Expand Up @@ -114,10 +115,16 @@ where
let fut = decode(self.io.clone());

let (req, mut body) = if let Some(timeout_duration) = self.opts.headers_timeout {
match timeout(timeout_duration, fut).await {
Ok(Ok(Some(r))) => r,
Ok(Ok(None)) | Err(TimeoutError { .. }) => return Ok(ConnectionStatus::Close), /* EOF or timeout */
Ok(Err(e)) => return Err(e),
match fut
.or(async {
Timer::after(timeout_duration).await;
Ok(None)
})
.await
{
Ok(Some(r)) => r,
Ok(None) => return Ok(ConnectionStatus::Close), /* EOF or timeout */
Err(e) => return Err(e),
}
} else {
match fut.await? {
Expand Down
Loading