mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
Initial impl for async-std support (#87)
* initial impl for async-std support
This commit is contained in:
parent
7751e944f4
commit
56ed50c6e8
21 changed files with 946 additions and 51 deletions
|
@ -1,5 +1,9 @@
|
|||
# Changes
|
||||
|
||||
## [0.1.0-b.9] - 2021-12-29
|
||||
|
||||
* Add `async-std` support
|
||||
|
||||
## [0.1.0-b.8] - 2021-12-28
|
||||
|
||||
* Fix error handing for nested filters
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ntex-io"
|
||||
version = "0.1.0-b.8"
|
||||
version = "0.1.0-b.9"
|
||||
authors = ["ntex contributors <team@ntex.rs>"]
|
||||
description = "Utilities for encoding and decoding frames"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
@ -16,7 +16,7 @@ name = "ntex_io"
|
|||
path = "src/lib.rs"
|
||||
|
||||
[features]
|
||||
default = ["tokio"]
|
||||
default = ["tokio-traits"]
|
||||
|
||||
# tokio traits support
|
||||
tokio-traits = ["tok-io/net", "tok-io/rt"]
|
||||
|
@ -24,6 +24,9 @@ tokio-traits = ["tok-io/net", "tok-io/rt"]
|
|||
# tokio runtime support
|
||||
tokio = ["tok-io/net", "tok-io/rt"]
|
||||
|
||||
# async-std runtime support
|
||||
async-std = ["async_std/unstable"]
|
||||
|
||||
[dependencies]
|
||||
ntex-codec = "0.6.0"
|
||||
ntex-bytes = "0.1.8"
|
||||
|
@ -36,6 +39,7 @@ log = "0.4"
|
|||
pin-project-lite = "0.2"
|
||||
|
||||
tok-io = { version = "1", package = "tokio", default-features = false, optional = true }
|
||||
async_std = { version = "1", package = "async-std", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
ntex = "0.5.0-b.2"
|
||||
|
|
38
ntex-io/src/asyncstd_rt.rs
Normal file
38
ntex-io/src/asyncstd_rt.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
#![allow(dead_code)]
|
||||
//! async net providers
|
||||
use ntex_util::future::lazy;
|
||||
use std::future::Future;
|
||||
|
||||
/// Spawn a future on the current thread. This does not create a new Arbiter
|
||||
/// or Arbiter address, it is simply a helper for spawning futures on the current
|
||||
/// thread.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if ntex system is not running.
|
||||
#[inline]
|
||||
pub fn spawn<F>(f: F) -> async_std::task::JoinHandle<F::Output>
|
||||
where
|
||||
F: Future + 'static,
|
||||
{
|
||||
async_std::task::spawn_local(f)
|
||||
}
|
||||
|
||||
/// Executes a future on the current thread. This does not create a new Arbiter
|
||||
/// or Arbiter address, it is simply a helper for executing futures on the current
|
||||
/// thread.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if ntex system is not running.
|
||||
#[inline]
|
||||
pub fn spawn_fn<F, R>(f: F) -> async_std::task::JoinHandle<R::Output>
|
||||
where
|
||||
F: FnOnce() -> R + 'static,
|
||||
R: Future + 'static,
|
||||
{
|
||||
spawn(async move {
|
||||
let r = lazy(|_| f()).await;
|
||||
r.await
|
||||
})
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
//! Utilities for abstructing io streams
|
||||
#![allow(clippy::return_self_not_must_use)]
|
||||
use std::{
|
||||
any::Any, any::TypeId, fmt, future::Future, io as sio, io::Error as IoError,
|
||||
task::Context, task::Poll,
|
||||
|
@ -16,9 +18,11 @@ mod tasks;
|
|||
mod time;
|
||||
mod utils;
|
||||
|
||||
#[cfg(feature = "async-std")]
|
||||
mod asyncstd_rt;
|
||||
#[cfg(any(feature = "tokio-traits", feature = "tokio"))]
|
||||
mod tokio_impl;
|
||||
#[cfg(any(feature = "tokio"))]
|
||||
#[cfg(feature = "tokio")]
|
||||
mod tokio_rt;
|
||||
|
||||
use ntex_bytes::BytesMut;
|
||||
|
@ -165,6 +169,9 @@ pub mod rt {
|
|||
|
||||
#[cfg(feature = "tokio")]
|
||||
pub use crate::tokio_rt::*;
|
||||
|
||||
#[cfg(all(not(feature = "tokio"), feature = "async-std"))]
|
||||
pub use crate::asyncstd_rt::*;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -221,6 +221,7 @@ impl Future for WriteTask {
|
|||
log::trace!(
|
||||
"write task is closed with err during flush"
|
||||
);
|
||||
this.state.close(None);
|
||||
return Poll::Ready(());
|
||||
}
|
||||
_ => (),
|
||||
|
@ -259,10 +260,11 @@ impl Future for WriteTask {
|
|||
}
|
||||
Poll::Pending => {
|
||||
*count += read_buf.filled().len() as u16;
|
||||
if *count > 8196 {
|
||||
if *count > 4096 {
|
||||
log::trace!(
|
||||
"write task is stopped, too much input"
|
||||
);
|
||||
this.state.close(None);
|
||||
return Poll::Ready(());
|
||||
}
|
||||
break;
|
||||
|
@ -638,6 +640,7 @@ mod unixstream {
|
|||
log::trace!(
|
||||
"write task is closed with err during flush"
|
||||
);
|
||||
this.state.close(None);
|
||||
return Poll::Ready(());
|
||||
}
|
||||
_ => (),
|
||||
|
@ -677,10 +680,11 @@ mod unixstream {
|
|||
}
|
||||
Poll::Pending => {
|
||||
*count += read_buf.filled().len() as u16;
|
||||
if *count > 8196 {
|
||||
if *count > 4096 {
|
||||
log::trace!(
|
||||
"write task is stopped, too much input"
|
||||
);
|
||||
this.state.close(None);
|
||||
return Poll::Ready(());
|
||||
}
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue