From 4770201ce4be5df344db687ce1e8efbd4a597a78 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 7 Nov 2019 22:53:11 +0100 Subject: [PATCH 1/7] core feature Signed-off-by: Yoshua Wuyts --- Cargo.toml | 3 ++- src/lib.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 54ab0f1bd..ede43d499 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,11 @@ features = ["docs"] rustdoc-args = ["--cfg", "feature=\"docs\""] [features] -default = [] +default = ["core"] docs = ["unstable", "attributes"] unstable = ["broadcaster"] attributes = ["async-attributes"] +core = [] [dependencies] async-attributes = { version = "1.1.0", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 4863cbbcb..98326118b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -158,6 +158,7 @@ //! features = ["unstable"] //! ``` //! +<<<<<<< HEAD //! Items marked with //! Date: Thu, 7 Nov 2019 23:44:32 +0100 Subject: [PATCH 2/7] introduce std + default features Signed-off-by: Yoshua Wuyts --- Cargo.toml | 68 +++++++++++++++++++++++------------- src/io/mod.rs | 80 +++++++++++++++++++++++-------------------- src/lib.rs | 36 ++++++++++--------- src/os/unix/mod.rs | 11 ++++-- src/os/windows/mod.rs | 4 ++- src/prelude.rs | 56 ++++++++++++++++-------------- src/sync/waker_set.rs | 4 +-- src/task/mod.rs | 62 +++++++++++++++++---------------- src/utils.rs | 24 +++++++++++++ 9 files changed, 207 insertions(+), 138 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ede43d499..4790f4adf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,33 +21,55 @@ features = ["docs"] rustdoc-args = ["--cfg", "feature=\"docs\""] [features] -default = ["core"] -docs = ["unstable", "attributes"] -unstable = ["broadcaster"] -attributes = ["async-attributes"] -core = [] +default = [ + "std", + "async-task", + "crossbeam-deque", + "futures-timer", + "kv-log-macro", + "log", + "mio", + "mio-uds", + "num_cpus", + "pin-project-lite", +] +docs = ["unstable"] +unstable = ["default", "broadcaster"] +std = [ + "async-macros", + "crossbeam-channel", + "crossbeam-utils", + "futures-core", + "futures-io", + "futures-timer", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", +] [dependencies] async-attributes = { version = "1.1.0", optional = true } -async-macros = "1.0.0" -async-task = "1.0.0" +async-macros = { version = "1.0.0", optional = true } +async-task = { version = "1.0.0", optional = true } broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] } -crossbeam-channel = "0.3.9" -crossbeam-deque = "0.7.1" -crossbeam-utils = "0.6.6" -futures-core = "0.3.0" -futures-io = "0.3.0" -futures-timer = "1.0.2" -kv-log-macro = "1.0.4" -log = { version = "0.4.8", features = ["kv_unstable"] } -memchr = "2.2.1" -mio = "0.6.19" -mio-uds = "0.6.7" -num_cpus = "1.10.1" -once_cell = "1.2.0" -pin-project-lite = "0.1" -pin-utils = "0.1.0-alpha.4" -slab = "0.4.2" +crossbeam-channel = { version = "0.3.9", optional = true } +crossbeam-deque = { version = "0.7.1", optional = true } +crossbeam-utils = { version = "0.6.6", optional = true } +futures-core = { version = "0.3.0", optional = true } +futures-io = { version = "0.3.0", optional = true } +futures-timer = { version = "1.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.2.1", optional = true } +mio = { version = "0.6.19", optional = true } +mio-uds = { version = "0.6.7", optional = true } +num_cpus = { version = "1.10.1", optional = true } +once_cell = { version = "1.2.0", optional = true } +pin-project-lite = { version = "0.1", optional = true } +pin-utils = { version = "0.1.0-alpha.4", optional = true } +slab = { version = "0.4.2", optional = true } [dev-dependencies] femme = "1.2.0" diff --git a/src/io/mod.rs b/src/io/mod.rs index 93753d104..c47115931 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -269,48 +269,54 @@ //! [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html //! [`.unwrap()`]: https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap -#[doc(inline)] -pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom}; +cfg_std! { + #[doc(inline)] + pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom}; -pub use buf_read::{BufRead, Lines}; -pub use buf_reader::BufReader; -pub use buf_writer::BufWriter; -pub use copy::copy; -pub use cursor::Cursor; -pub use empty::{empty, Empty}; -pub use read::Read; -pub use repeat::{repeat, Repeat}; -pub use seek::Seek; -pub use sink::{sink, Sink}; -pub use stderr::{stderr, Stderr}; -pub use stdin::{stdin, Stdin}; -pub use stdout::{stdout, Stdout}; -pub use timeout::timeout; -pub use write::Write; + pub use buf_read::{BufRead, Lines}; + pub use buf_reader::BufReader; + pub use buf_writer::BufWriter; + pub use copy::copy; + pub use cursor::Cursor; + pub use empty::{empty, Empty}; + pub use read::Read; + pub use repeat::{repeat, Repeat}; + pub use seek::Seek; + pub use sink::{sink, Sink}; + pub use write::Write; -// For use in the print macros. -#[doc(hidden)] -pub use stdio::{_eprint, _print}; + pub mod prelude; -pub mod prelude; + pub(crate) mod buf_read; + pub(crate) mod read; + pub(crate) mod seek; + pub(crate) mod write; -pub(crate) mod buf_read; -pub(crate) mod read; -pub(crate) mod seek; -pub(crate) mod write; + mod buf_reader; + mod buf_writer; + mod copy; + mod cursor; + mod empty; + mod repeat; + mod sink; +} + +cfg_default! { + // For use in the print macros. + #[doc(hidden)] + pub use stdio::{_eprint, _print}; -mod buf_reader; -mod buf_writer; -mod copy; -mod cursor; -mod empty; -mod repeat; -mod sink; -mod stderr; -mod stdin; -mod stdio; -mod stdout; -mod timeout; + pub use stderr::{stderr, Stderr}; + pub use stdin::{stdin, Stdin}; + pub use stdout::{stdout, Stdout}; + pub use timeout::timeout; + + mod timeout; + mod stderr; + mod stdin; + mod stdio; + mod stdout; +} cfg_unstable! { pub use stderr::StderrLock; diff --git a/src/lib.rs b/src/lib.rs index 98326118b..9d2e60244 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ //! # Async version of the Rust standard library //! //! `async-std` is a foundation of portable Rust software, a set of minimal and battle-tested -//! shared abstractions for the [broader Rust ecosystem][crates.io]. It offers core types, like +//! shared abstractions for the [broader Rust ecosystem][crates.io]. It offers std types, like //! [`Future`] and [`Stream`], library-defined [operations on language primitives](#primitives), //! [standard macros](#macros), [I/O] and [multithreading], among [many other things][other]. //! @@ -158,7 +158,6 @@ //! features = ["unstable"] //! ``` //! -<<<<<<< HEAD //! Items marked with //! { + $( + #[cfg(feature = "std")] + $item + )* + } +} + +/// Declares default items. +#[allow(unused_macros)] +#[doc(hidden)] +macro_rules! cfg_default { + ($($item:item)*) => { + $( + #[cfg(feature = "default")] + $item + )* + } +} + /// Defines an extension trait for a base trait. /// /// In generated docs, the base trait will contain methods from the extension trait. In actual From 85de4959482d7a58524f2339a3d395cc50df1d2e Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 7 Nov 2019 23:45:55 +0100 Subject: [PATCH 3/7] test std features on ci Signed-off-by: Yoshua Wuyts --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d5e7c7e9..031ffc96f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,6 +40,12 @@ jobs: command: check args: --features unstable --all --benches --bins --examples --tests + - name: check std only + uses: actions-rs/cargo@v1 + with: + command: check + args: --no-default-features --features std + - name: tests uses: actions-rs/cargo@v1 with: From b12d2984a3428ae2e17360d7e4c8e0942387eabc Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 7 Nov 2019 23:55:12 +0100 Subject: [PATCH 4/7] finish up all features Signed-off-by: Yoshua Wuyts --- src/task/mod.rs | 10 +++++----- src/utils.rs | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/task/mod.rs b/src/task/mod.rs index 9bb19f381..bcdea72c2 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -151,12 +151,12 @@ cfg_default! { mod task; mod task_id; mod task_local; -} -#[cfg(any(feature = "unstable", test))] -pub use spawn_blocking::spawn_blocking; -#[cfg(any(feature = "default", test))] -pub(crate) use spawn_blocking::spawn_blocking; + #[cfg(any(feature = "unstable", test))] + pub use spawn_blocking::spawn_blocking; + #[cfg(not(any(feature = "unstable", test)))] + pub(crate) use spawn_blocking::spawn_blocking; +} cfg_unstable! { pub use yield_now::yield_now; diff --git a/src/utils.rs b/src/utils.rs index 5607a2d7c..bff03fba8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,7 @@ -use std::mem; - /// Calls a function and aborts if it panics. /// /// This is useful in unsafe code where we can't recover from panics. +#[cfg(feature = "default")] #[inline] pub fn abort_on_panic(f: impl FnOnce() -> T) -> T { struct Bomb; @@ -15,11 +14,12 @@ pub fn abort_on_panic(f: impl FnOnce() -> T) -> T { let bomb = Bomb; let t = f(); - mem::forget(bomb); + std::mem::forget(bomb); t } /// Generates a random number in `0..n`. +#[cfg(feature = "default")] pub fn random(n: u32) -> u32 { use std::cell::Cell; use std::num::Wrapping; @@ -47,6 +47,7 @@ pub fn random(n: u32) -> u32 { } /// Defers evaluation of a block of code until the end of the scope. +#[cfg(feature = "default")] #[doc(hidden)] macro_rules! defer { ($($body:tt)*) => { From b0a03eb262d00455c8d81586e9c92f9ea2a6c2f2 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Fri, 8 Nov 2019 00:25:58 +0100 Subject: [PATCH 5/7] Fix task_local macro --- src/lib.rs | 4 +++- src/macros.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ src/prelude.rs | 8 +++---- src/task/task_local.rs | 51 ----------------------------------------- 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9d2e60244..04ed8fb63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -197,6 +197,9 @@ mod utils; #[doc(inline)] pub use async_attributes::{main, test}; +#[cfg(feature = "std")] +mod macros; + cfg_std! { pub mod future; pub mod io; @@ -205,7 +208,6 @@ cfg_std! { pub mod stream; pub mod sync; pub mod task; - mod macros; } cfg_default! { diff --git a/src/macros.rs b/src/macros.rs index f932e47e8..b7811d2ea 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -165,3 +165,55 @@ macro_rules! eprintln { } ); } + +/// Declares task-local values. +/// +/// The macro wraps any number of static declarations and makes them task-local. Attributes and +/// visibility modifiers are allowed. +/// +/// Each declared value is of the accessor type [`LocalKey`]. +/// +/// [`LocalKey`]: task/struct.LocalKey.html +/// +/// # Examples +/// +/// ``` +/// # +/// use std::cell::Cell; +/// +/// use async_std::task; +/// use async_std::prelude::*; +/// +/// task_local! { +/// static VAL: Cell = Cell::new(5); +/// } +/// +/// task::block_on(async { +/// let v = VAL.with(|c| c.get()); +/// assert_eq!(v, 5); +/// }); +/// ``` +#[cfg(feature = "default")] +#[macro_export] +macro_rules! task_local { + () => (); + + ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => ( + $(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = { + #[inline] + fn __init() -> $t { + $init + } + + $crate::task::LocalKey { + __init, + __key: ::std::sync::atomic::AtomicU32::new(0), + } + }; + ); + + ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => ( + $crate::task_local!($(#[$attr])* $vis static $name: $t = $init); + $crate::task_local!($($rest)*); + ); +} diff --git a/src/prelude.rs b/src/prelude.rs index 4dc7265e7..2a1fa4154 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -40,10 +40,10 @@ cfg_std! { pub use crate::io::prelude::WriteExt as _; } -// cfg_default! { -// #[doc(no_inline)] -// pub use crate::task_local; -// } +cfg_default! { + #[doc(no_inline)] + pub use crate::task_local; +} cfg_unstable! { #[doc(no_inline)] diff --git a/src/task/task_local.rs b/src/task/task_local.rs index 0869cab46..72e53d72a 100644 --- a/src/task/task_local.rs +++ b/src/task/task_local.rs @@ -5,57 +5,6 @@ use std::sync::atomic::{AtomicU32, Ordering}; use crate::task::Task; -/// Declares task-local values. -/// -/// The macro wraps any number of static declarations and makes them task-local. Attributes and -/// visibility modifiers are allowed. -/// -/// Each declared value is of the accessor type [`LocalKey`]. -/// -/// [`LocalKey`]: task/struct.LocalKey.html -/// -/// # Examples -/// -/// ``` -/// # -/// use std::cell::Cell; -/// -/// use async_std::task; -/// use async_std::prelude::*; -/// -/// task_local! { -/// static VAL: Cell = Cell::new(5); -/// } -/// -/// task::block_on(async { -/// let v = VAL.with(|c| c.get()); -/// assert_eq!(v, 5); -/// }); -/// ``` -#[macro_export] -macro_rules! task_local { - () => (); - - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => ( - $(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = { - #[inline] - fn __init() -> $t { - $init - } - - $crate::task::LocalKey { - __init, - __key: ::std::sync::atomic::AtomicU32::new(0), - } - }; - ); - - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => ( - $crate::task_local!($(#[$attr])* $vis static $name: $t = $init); - $crate::task_local!($($rest)*); - ); -} - /// The key for accessing a task-local value. /// /// Every task-local value is lazily initialized on first access and destroyed when the task From 75aa6233bc96b756325d79c482039a296a019e26 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Fri, 8 Nov 2019 00:37:31 +0100 Subject: [PATCH 6/7] Remove crossbeam-channel and futures-timer from std --- Cargo.toml | 3 +-- src/future/future/mod.rs | 8 ++++---- src/sync/waker_set.rs | 1 - 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4790f4adf..7c860c03a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""] default = [ "std", "async-task", + "crossbeam-channel", "crossbeam-deque", "futures-timer", "kv-log-macro", @@ -37,11 +38,9 @@ docs = ["unstable"] unstable = ["default", "broadcaster"] std = [ "async-macros", - "crossbeam-channel", "crossbeam-utils", "futures-core", "futures-io", - "futures-timer", "memchr", "once_cell", "pin-project-lite", diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index d7bd75be7..d712dc806 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -144,8 +144,8 @@ extension_trait! { /// dbg!(a.await); /// # }) /// ``` + #[cfg(all(feature = "default", feature = "unstable"))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] - #[cfg(any(feature = "unstable", feature = "docs"))] fn delay(self, dur: Duration) -> impl Future [DelayFuture] where Self: Future + Sized @@ -167,8 +167,8 @@ extension_trait! { /// assert_eq!(future.await, 1); /// # }) /// ``` + #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] - #[cfg(any(feature = "unstable", feature = "docs"))] fn flatten(self) -> impl Future::Output as IntoFuture>::Output> [FlattenFuture::Output as IntoFuture>::Future>] where Self: Future + Sized, @@ -206,7 +206,7 @@ extension_trait! { # }); ``` "#] - #[cfg(any(feature = "unstable", feature = "docs"))] + #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] fn race( self, @@ -252,7 +252,7 @@ extension_trait! { # Ok(()) }) } ``` "#] - #[cfg(any(feature = "unstable", feature = "docs"))] + #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] fn try_race( self, diff --git a/src/sync/waker_set.rs b/src/sync/waker_set.rs index 646233de8..5ba4cfbd9 100644 --- a/src/sync/waker_set.rs +++ b/src/sync/waker_set.rs @@ -7,7 +7,6 @@ use std::cell::UnsafeCell; use std::ops::{Deref, DerefMut}; use std::sync::atomic::{AtomicUsize, Ordering}; - use std::task::{Context, Waker}; use crossbeam_utils::Backoff; From 764f200a8d901644317d23bd00ee05966698bd42 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Fri, 8 Nov 2019 00:42:29 +0100 Subject: [PATCH 7/7] Move future::timeout() behind cfg_default --- src/future/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/future/mod.rs b/src/future/mod.rs index dd28f2848..4e7d79a87 100644 --- a/src/future/mod.rs +++ b/src/future/mod.rs @@ -53,13 +53,16 @@ pub use future::Future; pub use pending::pending; pub use poll_fn::poll_fn; pub use ready::ready; -pub use timeout::{timeout, TimeoutError}; pub(crate) mod future; mod pending; mod poll_fn; mod ready; -mod timeout; + +cfg_default! { + pub use timeout::{timeout, TimeoutError}; + mod timeout; +} cfg_unstable! { pub use into_future::IntoFuture;