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

feat: use smol as runtime #757

Merged
merged 26 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
690ab16
add dependency
dignifiedquire Apr 24, 2020
1308fbd
switch to smol instead of an internal runtime
dignifiedquire Apr 24, 2020
fc9ee0d
keep std::sync::Arc
dignifiedquire Apr 24, 2020
e082634
fix spawning
dignifiedquire Apr 24, 2020
75ab721
bring back random
dignifiedquire Apr 24, 2020
b96afc4
implement task locals
dignifiedquire Apr 24, 2020
f5fa0d7
avoid boxing futures
dignifiedquire Apr 25, 2020
ab9d655
switch to smol::Timer
dignifiedquire Apr 25, 2020
fd6ae40
add timeout stress test
dignifiedquire Apr 25, 2020
10c8b9a
silence must use
dignifiedquire Apr 25, 2020
0a7a52a
update to work on smol/master
dignifiedquire Apr 25, 2020
228cc59
feat: add spawn_local
dignifiedquire Apr 25, 2020
3161a4e
add some missing windows imports
dignifiedquire Apr 25, 2020
2cd2ba3
remove unused dependencies
dignifiedquire Apr 26, 2020
e4df140
feat: add basic wasm support
dignifiedquire Apr 26, 2020
804a52b
use published smol
dignifiedquire Apr 26, 2020
48dd683
fix feature settings
dignifiedquire Apr 26, 2020
280b1a4
remove invalid doc comment
dignifiedquire Apr 27, 2020
7a9afbd
update smol
dignifiedquire Apr 27, 2020
1a6d4f6
fix windows trait declarations for rawsocket
dignifiedquire Apr 27, 2020
9253261
mark spawn_local unstable
dignifiedquire Apr 27, 2020
e092846
fix windows traits
dignifiedquire Apr 28, 2020
26f62aa
make wasm deps part of std
dignifiedquire Apr 28, 2020
1214bc2
increase timeouts
dignifiedquire Apr 29, 2020
faea222
fix: use run instead of block_on
dignifiedquire May 2, 2020
27c605b
cr: bring back trace call
dignifiedquire May 7, 2020
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
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ jobs:
with:
command: check
args: --features unstable --all --bins --examples --tests

- name: check wasm
uses: actions-rs/cargo@v1
with:
command: check
target: wasm32-unknown-unknown
override: true
args: --features unstable --all --bins --tests

- name: check bench
uses: actions-rs/cargo@v1
Expand Down
33 changes: 19 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.5.0"
authors = [
"Stjepan Glavina <stjepang@gmail.com>",
"Yoshua Wuyts <yoshuawuyts@gmail.com>",
"Friedel Ziegelmayer <me@dignifiedquire.com>",
"Contributors to async-std",
]
edition = "2018"
Expand All @@ -24,19 +25,13 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
default = [
"std",
"async-task",
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-queue",
"futures-timer",
"kv-log-macro",
"log",
"mio",
"mio-uds",
"num_cpus",
"pin-project-lite",
]
docs = ["attributes", "unstable", "default"]
unstable = ["std", "broadcaster", "futures-timer"]
unstable = ["std", "broadcaster"]
attributes = ["async-attributes"]
std = [
"alloc",
Expand All @@ -47,6 +42,10 @@ std = [
"once_cell",
"pin-utils",
"slab",
"smol",
"wasm-timer",
"wasm-bindgen-futures",
"futures-channel",
]
alloc = [
"futures-core/alloc",
Expand All @@ -55,32 +54,38 @@ alloc = [

[dependencies]
async-attributes = { version = "1.1.1", optional = true }
async-task = { version = "1.3.1", optional = true }
async-task = { version = "3.0.0", optional = true }
broadcaster = { version = "1.0.0", optional = true }
crossbeam-channel = { version = "0.4.2", optional = true }
crossbeam-deque = { version = "0.7.3", optional = true }
crossbeam-queue = { version = "0.2.0", optional = true }
crossbeam-utils = { version = "0.7.2", optional = true }
futures-core = { version = "0.3.4", optional = true, default-features = false }
futures-io = { version = "0.3.4", optional = true }
futures-timer = { version = "3.0.2", optional = true }
kv-log-macro = { version = "1.0.4", optional = true }
log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
memchr = { version = "2.3.3", optional = true }
mio = { version = "0.6.19", optional = true }
mio-uds = { version = "0.6.7", optional = true }
num_cpus = { version = "1.12.0", optional = true }
once_cell = { version = "1.3.1", optional = true }
pin-project-lite = { version = "0.1.4", optional = true }
pin-utils = { version = "0.1.0-alpha.4", optional = true }
slab = { version = "0.4.2", optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
smol = { version = "0.1.1", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-timer = { version = "0.2.4", optional = true }
wasm-bindgen-futures = { version = "0.4.10", optional = true }
futures-channel = { version = "0.3.4", optional = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we could use our own channels in this context, which already work. But for the purpose of this PR this seems fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

They didn't quite do what I needed, we can improve on that later on.


[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.10"
Comment on lines +79 to +80
Copy link
Contributor

Choose a reason for hiding this comment

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

This is so exciting!


[dev-dependencies]
femme = "1.3.0"
rand = "0.7.3"
surf = "1.0.3"
tempdir = "0.3.7"
futures = "0.3.4"
rand_xorshift = "0.2.0"

[[test]]
name = "stream"
Expand Down
6 changes: 3 additions & 3 deletions src/future/future/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

use futures_timer::Delay;
use pin_project_lite::pin_project;

use crate::task::{Context, Poll};
use crate::utils::Timer;

pin_project! {
#[doc(hidden)]
Expand All @@ -14,13 +14,13 @@ pin_project! {
#[pin]
future: F,
#[pin]
delay: Delay,
delay: Timer,
}
}

impl<F> DelayFuture<F> {
pub fn new(future: F, dur: Duration) -> DelayFuture<F> {
let delay = Delay::new(dur);
let delay = Timer::after(dur);

DelayFuture { future, delay }
}
Expand Down
17 changes: 8 additions & 9 deletions src/future/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::error::Error;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
use std::future::Future;

use futures_timer::Delay;
use pin_project_lite::pin_project;

use crate::task::{Context, Poll};
use crate::utils::Timer;

/// Awaits a future or times out after a duration of time.
///
Expand All @@ -33,11 +33,7 @@ pub async fn timeout<F, T>(dur: Duration, f: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
{
let f = TimeoutFuture {
future: f,
delay: Delay::new(dur),
};
f.await
TimeoutFuture::new(f, dur).await
}

pin_project! {
Expand All @@ -46,14 +42,17 @@ pin_project! {
#[pin]
future: F,
#[pin]
delay: Delay,
delay: Timer,
}
}

impl<F> TimeoutFuture<F> {
#[allow(dead_code)]
pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
TimeoutFuture { future: future, delay: Delay::new(dur) }
TimeoutFuture {
future,
delay: Timer::after(dur),
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,33 @@ cfg_std! {
cfg_default! {
// For use in the print macros.
#[doc(hidden)]
#[cfg(not(target_os = "unknown"))]
pub use stdio::{_eprint, _print};

#[cfg(not(target_os = "unknown"))]
pub use stderr::{stderr, Stderr};
#[cfg(not(target_os = "unknown"))]
pub use stdin::{stdin, Stdin};
#[cfg(not(target_os = "unknown"))]
pub use stdout::{stdout, Stdout};
pub use timeout::timeout;

mod timeout;
#[cfg(not(target_os = "unknown"))]
mod stderr;
#[cfg(not(target_os = "unknown"))]
mod stdin;
#[cfg(not(target_os = "unknown"))]
mod stdio;
#[cfg(not(target_os = "unknown"))]
mod stdout;
}

cfg_unstable_default! {
#[cfg(not(target_os = "unknown"))]
pub use stderr::StderrLock;
#[cfg(not(target_os = "unknown"))]
pub use stdin::StdinLock;
#[cfg(not(target_os = "unknown"))]
pub use stdout::StdoutLock;
}
13 changes: 6 additions & 7 deletions src/io/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::mem;

use crate::io::IoSliceMut;

pub use take::Take;
pub use bytes::Bytes;
pub use chain::Chain;
pub use take::Take;

extension_trait! {
use std::pin::Pin;
Expand Down Expand Up @@ -477,13 +477,13 @@ unsafe fn initialize<R: futures_io::AsyncRead>(_reader: &R, buf: &mut [u8]) {
std::ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len())
}

#[cfg(test)]
#[cfg(all(test, not(target_os = "unknown")))]
mod tests {
use crate::io;
use crate::prelude::*;

#[test]
fn test_read_by_ref() -> io::Result<()> {
fn test_read_by_ref() {
crate::task::block_on(async {
let mut f = io::Cursor::new(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8]);
let mut buffer = Vec::new();
Expand All @@ -493,14 +493,13 @@ mod tests {
let reference = f.by_ref();

// read at most 5 bytes
assert_eq!(reference.take(5).read_to_end(&mut buffer).await?, 5);
assert_eq!(reference.take(5).read_to_end(&mut buffer).await.unwrap(), 5);
assert_eq!(&buffer, &[0, 1, 2, 3, 4])
} // drop our &mut reference so we can use f again

// original file still usable, read the rest
assert_eq!(f.read_to_end(&mut other_buffer).await?, 4);
assert_eq!(f.read_to_end(&mut other_buffer).await.unwrap(), 4);
assert_eq!(&other_buffer, &[5, 6, 7, 8]);
Ok(())
})
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was ? removed in this file? Is that a stylistic choice, or does this point to a change in behavior?

Copy link
Member Author

Choose a reason for hiding this comment

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

because block_on can't return a value in wasm, making this blow up, now that I cfged that out I can undo that change probably

}
}
2 changes: 1 addition & 1 deletion src/io/read/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<T: BufRead> BufRead for Take<T> {
}
}

#[cfg(test)]
#[cfg(all(test, not(target_os = "unknown")))]
mod tests {
use crate::io;
use crate::prelude::*;
Expand Down
8 changes: 4 additions & 4 deletions src/io/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use std::future::Future;

use futures_timer::Delay;
use pin_project_lite::pin_project;

use crate::io;
use crate::utils::Timer;

/// Awaits an I/O future or times out after a duration of time.
///
Expand Down Expand Up @@ -37,7 +37,7 @@ where
F: Future<Output = io::Result<T>>,
{
Timeout {
timeout: Delay::new(dur),
timeout: Timer::after(dur),
future: f,
}
.await
Expand All @@ -53,7 +53,7 @@ pin_project! {
#[pin]
future: F,
#[pin]
timeout: Delay,
timeout: Timer,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,17 @@ cfg_std! {
}

cfg_default! {
#[cfg(not(target_os = "unknown"))]
pub mod fs;
pub mod path;
pub mod net;
#[cfg(not(target_os = "unknown"))]
pub(crate) mod rt;
}

cfg_unstable! {
pub mod pin;
#[cfg(not(target_os = "unknown"))]
pub mod process;

mod unit;
Expand Down
6 changes: 6 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ pub use std::net::Shutdown;
pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};

#[cfg(not(target_os = "unknown"))]
pub use addr::ToSocketAddrs;
#[cfg(not(target_os = "unknown"))]
pub use tcp::{Incoming, TcpListener, TcpStream};
#[cfg(not(target_os = "unknown"))]
pub use udp::UdpSocket;

#[cfg(not(target_os = "unknown"))]
mod addr;
#[cfg(not(target_os = "unknown"))]
mod tcp;
#[cfg(not(target_os = "unknown"))]
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
mod udp;
Loading