Skip to content

Commit 335bd34

Browse files
yoshuawuytsStjepan Glavina
authored and
Stjepan Glavina
committed
Add "std" feature flag (#476)
* core feature Signed-off-by: Yoshua Wuyts <[email protected]> * introduce std + default features Signed-off-by: Yoshua Wuyts <[email protected]> * test std features on ci Signed-off-by: Yoshua Wuyts <[email protected]> * finish up all features Signed-off-by: Yoshua Wuyts <[email protected]> * Fix task_local macro * Remove crossbeam-channel and futures-timer from std * Move future::timeout() behind cfg_default
1 parent f588ba6 commit 335bd34

File tree

14 files changed

+287
-195
lines changed

14 files changed

+287
-195
lines changed

.github/workflows/ci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ jobs:
4040
command: check
4141
args: --features unstable --all --benches --bins --examples --tests
4242

43+
- name: check std only
44+
uses: actions-rs/cargo@v1
45+
with:
46+
command: check
47+
args: --no-default-features --features std
48+
4349
- name: tests
4450
uses: actions-rs/cargo@v1
4551
with:

Cargo.toml

+44-22
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,54 @@ features = ["docs"]
2121
rustdoc-args = ["--cfg", "feature=\"docs\""]
2222

2323
[features]
24-
default = []
25-
docs = ["unstable", "attributes"]
26-
unstable = ["broadcaster"]
27-
attributes = ["async-attributes"]
24+
default = [
25+
"std",
26+
"async-task",
27+
"crossbeam-channel",
28+
"crossbeam-deque",
29+
"futures-timer",
30+
"kv-log-macro",
31+
"log",
32+
"mio",
33+
"mio-uds",
34+
"num_cpus",
35+
"pin-project-lite",
36+
]
37+
docs = ["unstable"]
38+
unstable = ["default", "broadcaster"]
39+
std = [
40+
"async-macros",
41+
"crossbeam-utils",
42+
"futures-core",
43+
"futures-io",
44+
"memchr",
45+
"once_cell",
46+
"pin-project-lite",
47+
"pin-utils",
48+
"slab",
49+
]
2850

2951
[dependencies]
3052
async-attributes = { version = "1.1.0", optional = true }
31-
async-macros = "1.0.0"
32-
async-task = "1.0.0"
53+
async-macros = { version = "1.0.0", optional = true }
54+
async-task = { version = "1.0.0", optional = true }
3355
broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] }
34-
crossbeam-channel = "0.3.9"
35-
crossbeam-deque = "0.7.1"
36-
crossbeam-utils = "0.6.6"
37-
futures-core = "0.3.0"
38-
futures-io = "0.3.0"
39-
futures-timer = "1.0.2"
40-
kv-log-macro = "1.0.4"
41-
log = { version = "0.4.8", features = ["kv_unstable"] }
42-
memchr = "2.2.1"
43-
mio = "0.6.19"
44-
mio-uds = "0.6.7"
45-
num_cpus = "1.10.1"
46-
once_cell = "1.2.0"
47-
pin-project-lite = "0.1"
48-
pin-utils = "0.1.0-alpha.4"
49-
slab = "0.4.2"
56+
crossbeam-channel = { version = "0.3.9", optional = true }
57+
crossbeam-deque = { version = "0.7.1", optional = true }
58+
crossbeam-utils = { version = "0.6.6", optional = true }
59+
futures-core = { version = "0.3.0", optional = true }
60+
futures-io = { version = "0.3.0", optional = true }
61+
futures-timer = { version = "1.0.2", optional = true }
62+
kv-log-macro = { version = "1.0.4", optional = true }
63+
log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
64+
memchr = { version = "2.2.1", optional = true }
65+
mio = { version = "0.6.19", optional = true }
66+
mio-uds = { version = "0.6.7", optional = true }
67+
num_cpus = { version = "1.10.1", optional = true }
68+
once_cell = { version = "1.2.0", optional = true }
69+
pin-project-lite = { version = "0.1", optional = true }
70+
pin-utils = { version = "0.1.0-alpha.4", optional = true }
71+
slab = { version = "0.4.2", optional = true }
5072

5173
[dev-dependencies]
5274
femme = "1.2.0"

src/future/future/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ extension_trait! {
144144
/// dbg!(a.await);
145145
/// # })
146146
/// ```
147+
#[cfg(all(feature = "default", feature = "unstable"))]
147148
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
148-
#[cfg(any(feature = "unstable", feature = "docs"))]
149149
fn delay(self, dur: Duration) -> impl Future<Output = Self::Output> [DelayFuture<Self>]
150150
where
151151
Self: Future + Sized
@@ -167,8 +167,8 @@ extension_trait! {
167167
/// assert_eq!(future.await, 1);
168168
/// # })
169169
/// ```
170+
#[cfg(feature = "unstable")]
170171
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
171-
#[cfg(any(feature = "unstable", feature = "docs"))]
172172
fn flatten(self) -> impl Future<Output = <<Self as Future>::Output as IntoFuture>::Output> [FlattenFuture<Self, <<Self as Future>::Output as IntoFuture>::Future>]
173173
where
174174
Self: Future + Sized,
@@ -206,7 +206,7 @@ extension_trait! {
206206
# });
207207
```
208208
"#]
209-
#[cfg(any(feature = "unstable", feature = "docs"))]
209+
#[cfg(feature = "unstable")]
210210
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
211211
fn race<F>(
212212
self,
@@ -252,7 +252,7 @@ extension_trait! {
252252
# Ok(()) }) }
253253
```
254254
"#]
255-
#[cfg(any(feature = "unstable", feature = "docs"))]
255+
#[cfg(feature = "unstable")]
256256
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
257257
fn try_race<F: std::future::Future, T, E>(
258258
self,

src/future/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ pub use future::Future;
5353
pub use pending::pending;
5454
pub use poll_fn::poll_fn;
5555
pub use ready::ready;
56-
pub use timeout::{timeout, TimeoutError};
5756

5857
pub(crate) mod future;
5958
mod pending;
6059
mod poll_fn;
6160
mod ready;
62-
mod timeout;
61+
62+
cfg_default! {
63+
pub use timeout::{timeout, TimeoutError};
64+
mod timeout;
65+
}
6366

6467
cfg_unstable! {
6568
pub use into_future::IntoFuture;

src/io/mod.rs

+43-37
Original file line numberDiff line numberDiff line change
@@ -269,48 +269,54 @@
269269
//! [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
270270
//! [`.unwrap()`]: https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap
271271
272-
#[doc(inline)]
273-
pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};
272+
cfg_std! {
273+
#[doc(inline)]
274+
pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};
274275

275-
pub use buf_read::{BufRead, Lines};
276-
pub use buf_reader::BufReader;
277-
pub use buf_writer::BufWriter;
278-
pub use copy::copy;
279-
pub use cursor::Cursor;
280-
pub use empty::{empty, Empty};
281-
pub use read::Read;
282-
pub use repeat::{repeat, Repeat};
283-
pub use seek::Seek;
284-
pub use sink::{sink, Sink};
285-
pub use stderr::{stderr, Stderr};
286-
pub use stdin::{stdin, Stdin};
287-
pub use stdout::{stdout, Stdout};
288-
pub use timeout::timeout;
289-
pub use write::Write;
276+
pub use buf_read::{BufRead, Lines};
277+
pub use buf_reader::BufReader;
278+
pub use buf_writer::BufWriter;
279+
pub use copy::copy;
280+
pub use cursor::Cursor;
281+
pub use empty::{empty, Empty};
282+
pub use read::Read;
283+
pub use repeat::{repeat, Repeat};
284+
pub use seek::Seek;
285+
pub use sink::{sink, Sink};
286+
pub use write::Write;
290287

291-
// For use in the print macros.
292-
#[doc(hidden)]
293-
pub use stdio::{_eprint, _print};
288+
pub mod prelude;
294289

295-
pub mod prelude;
290+
pub(crate) mod buf_read;
291+
pub(crate) mod read;
292+
pub(crate) mod seek;
293+
pub(crate) mod write;
296294

297-
pub(crate) mod buf_read;
298-
pub(crate) mod read;
299-
pub(crate) mod seek;
300-
pub(crate) mod write;
295+
mod buf_reader;
296+
mod buf_writer;
297+
mod copy;
298+
mod cursor;
299+
mod empty;
300+
mod repeat;
301+
mod sink;
302+
}
303+
304+
cfg_default! {
305+
// For use in the print macros.
306+
#[doc(hidden)]
307+
pub use stdio::{_eprint, _print};
301308

302-
mod buf_reader;
303-
mod buf_writer;
304-
mod copy;
305-
mod cursor;
306-
mod empty;
307-
mod repeat;
308-
mod sink;
309-
mod stderr;
310-
mod stdin;
311-
mod stdio;
312-
mod stdout;
313-
mod timeout;
309+
pub use stderr::{stderr, Stderr};
310+
pub use stdin::{stdin, Stdin};
311+
pub use stdout::{stdout, Stdout};
312+
pub use timeout::timeout;
313+
314+
mod timeout;
315+
mod stderr;
316+
mod stdin;
317+
mod stdio;
318+
mod stdout;
319+
}
314320

315321
cfg_unstable! {
316322
pub use stderr::StderrLock;

src/lib.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! # Async version of the Rust standard library
22
//!
33
//! `async-std` is a foundation of portable Rust software, a set of minimal and battle-tested
4-
//! shared abstractions for the [broader Rust ecosystem][crates.io]. It offers core types, like
4+
//! shared abstractions for the [broader Rust ecosystem][crates.io]. It offers std types, like
55
//! [`Future`] and [`Stream`], library-defined [operations on language primitives](#primitives),
66
//! [standard macros](#macros), [I/O] and [multithreading], among [many other things][other].
77
//!
@@ -170,8 +170,17 @@
170170
//! version = "0.99"
171171
//! features = ["attributes"]
172172
//! ```
173+
//!
174+
//! Additionally it's possible to only use the core traits and combinators by
175+
//! only enabling the `std` Cargo feature:
176+
//!
177+
//! ```toml
178+
//! [dependencies.async-std]
179+
//! version = "0.99"
180+
//! default-features = false
181+
//! features = ["std"]
182+
//! ```
173183
174-
#![cfg(feature = "default")]
175184
#![cfg_attr(feature = "docs", feature(doc_cfg))]
176185
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
177186
#![allow(clippy::mutex_atomic, clippy::module_inception)]
@@ -188,16 +197,24 @@ mod utils;
188197
#[doc(inline)]
189198
pub use async_attributes::{main, test};
190199

191-
pub mod fs;
192-
pub mod future;
193-
pub mod io;
194-
pub mod net;
195-
pub mod os;
196-
pub mod path;
197-
pub mod prelude;
198-
pub mod stream;
199-
pub mod sync;
200-
pub mod task;
200+
#[cfg(feature = "std")]
201+
mod macros;
202+
203+
cfg_std! {
204+
pub mod future;
205+
pub mod io;
206+
pub mod os;
207+
pub mod prelude;
208+
pub mod stream;
209+
pub mod sync;
210+
pub mod task;
211+
}
212+
213+
cfg_default! {
214+
pub mod fs;
215+
pub mod path;
216+
pub mod net;
217+
}
201218

202219
cfg_unstable! {
203220
pub mod pin;
@@ -213,5 +230,3 @@ cfg_unstable! {
213230
#[doc(inline)]
214231
pub use std::{write, writeln};
215232
}
216-
217-
mod macros;

src/macros.rs

+52
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,55 @@ macro_rules! eprintln {
165165
}
166166
);
167167
}
168+
169+
/// Declares task-local values.
170+
///
171+
/// The macro wraps any number of static declarations and makes them task-local. Attributes and
172+
/// visibility modifiers are allowed.
173+
///
174+
/// Each declared value is of the accessor type [`LocalKey`].
175+
///
176+
/// [`LocalKey`]: task/struct.LocalKey.html
177+
///
178+
/// # Examples
179+
///
180+
/// ```
181+
/// #
182+
/// use std::cell::Cell;
183+
///
184+
/// use async_std::task;
185+
/// use async_std::prelude::*;
186+
///
187+
/// task_local! {
188+
/// static VAL: Cell<u32> = Cell::new(5);
189+
/// }
190+
///
191+
/// task::block_on(async {
192+
/// let v = VAL.with(|c| c.get());
193+
/// assert_eq!(v, 5);
194+
/// });
195+
/// ```
196+
#[cfg(feature = "default")]
197+
#[macro_export]
198+
macro_rules! task_local {
199+
() => ();
200+
201+
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
202+
$(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = {
203+
#[inline]
204+
fn __init() -> $t {
205+
$init
206+
}
207+
208+
$crate::task::LocalKey {
209+
__init,
210+
__key: ::std::sync::atomic::AtomicU32::new(0),
211+
}
212+
};
213+
);
214+
215+
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
216+
$crate::task_local!($(#[$attr])* $vis static $name: $t = $init);
217+
$crate::task_local!($($rest)*);
218+
);
219+
}

src/os/unix/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! Platform-specific extensions for Unix platforms.
22
3-
pub mod fs;
4-
pub mod io;
5-
pub mod net;
3+
cfg_std! {
4+
pub mod io;
5+
}
6+
7+
cfg_default! {
8+
pub mod fs;
9+
pub mod net;
10+
}

src/os/windows/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
//! Platform-specific extensions for Windows.
22
3-
pub mod io;
3+
cfg_std! {
4+
pub mod io;
5+
}

0 commit comments

Comments
 (0)