mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-26 18:26:48 +00:00
Add "std" feature flag (#476)
* core feature Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * introduce std + default features Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * test std features on ci Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * finish up all features Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * Fix task_local macro * Remove crossbeam-channel and futures-timer from std * Move future::timeout() behind cfg_default
This commit is contained in:
parent
f588ba6bdd
commit
335bd34470
14 changed files with 287 additions and 195 deletions
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
|
@ -40,6 +40,12 @@ jobs:
|
||||||
command: check
|
command: check
|
||||||
args: --features unstable --all --benches --bins --examples --tests
|
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
|
- name: tests
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
|
|
66
Cargo.toml
66
Cargo.toml
|
@ -21,32 +21,54 @@ features = ["docs"]
|
||||||
rustdoc-args = ["--cfg", "feature=\"docs\""]
|
rustdoc-args = ["--cfg", "feature=\"docs\""]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = [
|
||||||
docs = ["unstable", "attributes"]
|
"std",
|
||||||
unstable = ["broadcaster"]
|
"async-task",
|
||||||
attributes = ["async-attributes"]
|
"crossbeam-channel",
|
||||||
|
"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-utils",
|
||||||
|
"futures-core",
|
||||||
|
"futures-io",
|
||||||
|
"memchr",
|
||||||
|
"once_cell",
|
||||||
|
"pin-project-lite",
|
||||||
|
"pin-utils",
|
||||||
|
"slab",
|
||||||
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-attributes = { version = "1.1.0", optional = true }
|
async-attributes = { version = "1.1.0", optional = true }
|
||||||
async-macros = "1.0.0"
|
async-macros = { version = "1.0.0", optional = true }
|
||||||
async-task = "1.0.0"
|
async-task = { version = "1.0.0", optional = true }
|
||||||
broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] }
|
broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] }
|
||||||
crossbeam-channel = "0.3.9"
|
crossbeam-channel = { version = "0.3.9", optional = true }
|
||||||
crossbeam-deque = "0.7.1"
|
crossbeam-deque = { version = "0.7.1", optional = true }
|
||||||
crossbeam-utils = "0.6.6"
|
crossbeam-utils = { version = "0.6.6", optional = true }
|
||||||
futures-core = "0.3.0"
|
futures-core = { version = "0.3.0", optional = true }
|
||||||
futures-io = "0.3.0"
|
futures-io = { version = "0.3.0", optional = true }
|
||||||
futures-timer = "1.0.2"
|
futures-timer = { version = "1.0.2", optional = true }
|
||||||
kv-log-macro = "1.0.4"
|
kv-log-macro = { version = "1.0.4", optional = true }
|
||||||
log = { version = "0.4.8", features = ["kv_unstable"] }
|
log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
|
||||||
memchr = "2.2.1"
|
memchr = { version = "2.2.1", optional = true }
|
||||||
mio = "0.6.19"
|
mio = { version = "0.6.19", optional = true }
|
||||||
mio-uds = "0.6.7"
|
mio-uds = { version = "0.6.7", optional = true }
|
||||||
num_cpus = "1.10.1"
|
num_cpus = { version = "1.10.1", optional = true }
|
||||||
once_cell = "1.2.0"
|
once_cell = { version = "1.2.0", optional = true }
|
||||||
pin-project-lite = "0.1"
|
pin-project-lite = { version = "0.1", optional = true }
|
||||||
pin-utils = "0.1.0-alpha.4"
|
pin-utils = { version = "0.1.0-alpha.4", optional = true }
|
||||||
slab = "0.4.2"
|
slab = { version = "0.4.2", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
femme = "1.2.0"
|
femme = "1.2.0"
|
||||||
|
|
|
@ -144,8 +144,8 @@ extension_trait! {
|
||||||
/// dbg!(a.await);
|
/// dbg!(a.await);
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(all(feature = "default", feature = "unstable"))]
|
||||||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
#[cfg(any(feature = "unstable", feature = "docs"))]
|
|
||||||
fn delay(self, dur: Duration) -> impl Future<Output = Self::Output> [DelayFuture<Self>]
|
fn delay(self, dur: Duration) -> impl Future<Output = Self::Output> [DelayFuture<Self>]
|
||||||
where
|
where
|
||||||
Self: Future + Sized
|
Self: Future + Sized
|
||||||
|
@ -167,8 +167,8 @@ extension_trait! {
|
||||||
/// assert_eq!(future.await, 1);
|
/// assert_eq!(future.await, 1);
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(feature = "unstable")]
|
||||||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
#[cfg(any(feature = "unstable", feature = "docs"))]
|
|
||||||
fn flatten(self) -> impl Future<Output = <<Self as Future>::Output as IntoFuture>::Output> [FlattenFuture<Self, <<Self as Future>::Output as IntoFuture>::Future>]
|
fn flatten(self) -> impl Future<Output = <<Self as Future>::Output as IntoFuture>::Output> [FlattenFuture<Self, <<Self as Future>::Output as IntoFuture>::Future>]
|
||||||
where
|
where
|
||||||
Self: Future + Sized,
|
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)))]
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
fn race<F>(
|
fn race<F>(
|
||||||
self,
|
self,
|
||||||
|
@ -252,7 +252,7 @@ extension_trait! {
|
||||||
# Ok(()) }) }
|
# Ok(()) }) }
|
||||||
```
|
```
|
||||||
"#]
|
"#]
|
||||||
#[cfg(any(feature = "unstable", feature = "docs"))]
|
#[cfg(feature = "unstable")]
|
||||||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
fn try_race<F: std::future::Future, T, E>(
|
fn try_race<F: std::future::Future, T, E>(
|
||||||
self,
|
self,
|
||||||
|
|
|
@ -53,13 +53,16 @@ pub use future::Future;
|
||||||
pub use pending::pending;
|
pub use pending::pending;
|
||||||
pub use poll_fn::poll_fn;
|
pub use poll_fn::poll_fn;
|
||||||
pub use ready::ready;
|
pub use ready::ready;
|
||||||
pub use timeout::{timeout, TimeoutError};
|
|
||||||
|
|
||||||
pub(crate) mod future;
|
pub(crate) mod future;
|
||||||
mod pending;
|
mod pending;
|
||||||
mod poll_fn;
|
mod poll_fn;
|
||||||
mod ready;
|
mod ready;
|
||||||
mod timeout;
|
|
||||||
|
cfg_default! {
|
||||||
|
pub use timeout::{timeout, TimeoutError};
|
||||||
|
mod timeout;
|
||||||
|
}
|
||||||
|
|
||||||
cfg_unstable! {
|
cfg_unstable! {
|
||||||
pub use into_future::IntoFuture;
|
pub use into_future::IntoFuture;
|
||||||
|
|
|
@ -269,48 +269,54 @@
|
||||||
//! [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
|
//! [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
|
||||||
//! [`.unwrap()`]: https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap
|
//! [`.unwrap()`]: https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap
|
||||||
|
|
||||||
#[doc(inline)]
|
cfg_std! {
|
||||||
pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};
|
#[doc(inline)]
|
||||||
|
pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};
|
||||||
|
|
||||||
pub use buf_read::{BufRead, Lines};
|
pub use buf_read::{BufRead, Lines};
|
||||||
pub use buf_reader::BufReader;
|
pub use buf_reader::BufReader;
|
||||||
pub use buf_writer::BufWriter;
|
pub use buf_writer::BufWriter;
|
||||||
pub use copy::copy;
|
pub use copy::copy;
|
||||||
pub use cursor::Cursor;
|
pub use cursor::Cursor;
|
||||||
pub use empty::{empty, Empty};
|
pub use empty::{empty, Empty};
|
||||||
pub use read::Read;
|
pub use read::Read;
|
||||||
pub use repeat::{repeat, Repeat};
|
pub use repeat::{repeat, Repeat};
|
||||||
pub use seek::Seek;
|
pub use seek::Seek;
|
||||||
pub use sink::{sink, Sink};
|
pub use sink::{sink, Sink};
|
||||||
pub use stderr::{stderr, Stderr};
|
pub use write::Write;
|
||||||
pub use stdin::{stdin, Stdin};
|
|
||||||
pub use stdout::{stdout, Stdout};
|
|
||||||
pub use timeout::timeout;
|
|
||||||
pub use write::Write;
|
|
||||||
|
|
||||||
// For use in the print macros.
|
pub mod prelude;
|
||||||
#[doc(hidden)]
|
|
||||||
pub use stdio::{_eprint, _print};
|
|
||||||
|
|
||||||
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;
|
mod buf_reader;
|
||||||
pub(crate) mod read;
|
mod buf_writer;
|
||||||
pub(crate) mod seek;
|
mod copy;
|
||||||
pub(crate) mod write;
|
mod cursor;
|
||||||
|
mod empty;
|
||||||
|
mod repeat;
|
||||||
|
mod sink;
|
||||||
|
}
|
||||||
|
|
||||||
mod buf_reader;
|
cfg_default! {
|
||||||
mod buf_writer;
|
// For use in the print macros.
|
||||||
mod copy;
|
#[doc(hidden)]
|
||||||
mod cursor;
|
pub use stdio::{_eprint, _print};
|
||||||
mod empty;
|
|
||||||
mod repeat;
|
pub use stderr::{stderr, Stderr};
|
||||||
mod sink;
|
pub use stdin::{stdin, Stdin};
|
||||||
mod stderr;
|
pub use stdout::{stdout, Stdout};
|
||||||
mod stdin;
|
pub use timeout::timeout;
|
||||||
mod stdio;
|
|
||||||
mod stdout;
|
mod timeout;
|
||||||
mod timeout;
|
mod stderr;
|
||||||
|
mod stdin;
|
||||||
|
mod stdio;
|
||||||
|
mod stdout;
|
||||||
|
}
|
||||||
|
|
||||||
cfg_unstable! {
|
cfg_unstable! {
|
||||||
pub use stderr::StderrLock;
|
pub use stderr::StderrLock;
|
||||||
|
|
43
src/lib.rs
43
src/lib.rs
|
@ -1,7 +1,7 @@
|
||||||
//! # Async version of the Rust standard library
|
//! # Async version of the Rust standard library
|
||||||
//!
|
//!
|
||||||
//! `async-std` is a foundation of portable Rust software, a set of minimal and battle-tested
|
//! `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),
|
//! [`Future`] and [`Stream`], library-defined [operations on language primitives](#primitives),
|
||||||
//! [standard macros](#macros), [I/O] and [multithreading], among [many other things][other].
|
//! [standard macros](#macros), [I/O] and [multithreading], among [many other things][other].
|
||||||
//!
|
//!
|
||||||
|
@ -170,8 +170,17 @@
|
||||||
//! version = "0.99"
|
//! version = "0.99"
|
||||||
//! features = ["attributes"]
|
//! features = ["attributes"]
|
||||||
//! ```
|
//! ```
|
||||||
|
//!
|
||||||
|
//! Additionally it's possible to only use the core traits and combinators by
|
||||||
|
//! only enabling the `std` Cargo feature:
|
||||||
|
//!
|
||||||
|
//! ```toml
|
||||||
|
//! [dependencies.async-std]
|
||||||
|
//! version = "0.99"
|
||||||
|
//! default-features = false
|
||||||
|
//! features = ["std"]
|
||||||
|
//! ```
|
||||||
|
|
||||||
#![cfg(feature = "default")]
|
|
||||||
#![cfg_attr(feature = "docs", feature(doc_cfg))]
|
#![cfg_attr(feature = "docs", feature(doc_cfg))]
|
||||||
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
|
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
|
||||||
#![allow(clippy::mutex_atomic, clippy::module_inception)]
|
#![allow(clippy::mutex_atomic, clippy::module_inception)]
|
||||||
|
@ -188,16 +197,24 @@ mod utils;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use async_attributes::{main, test};
|
pub use async_attributes::{main, test};
|
||||||
|
|
||||||
pub mod fs;
|
#[cfg(feature = "std")]
|
||||||
pub mod future;
|
mod macros;
|
||||||
pub mod io;
|
|
||||||
pub mod net;
|
cfg_std! {
|
||||||
pub mod os;
|
pub mod future;
|
||||||
pub mod path;
|
pub mod io;
|
||||||
pub mod prelude;
|
pub mod os;
|
||||||
pub mod stream;
|
pub mod prelude;
|
||||||
pub mod sync;
|
pub mod stream;
|
||||||
pub mod task;
|
pub mod sync;
|
||||||
|
pub mod task;
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_default! {
|
||||||
|
pub mod fs;
|
||||||
|
pub mod path;
|
||||||
|
pub mod net;
|
||||||
|
}
|
||||||
|
|
||||||
cfg_unstable! {
|
cfg_unstable! {
|
||||||
pub mod pin;
|
pub mod pin;
|
||||||
|
@ -213,5 +230,3 @@ cfg_unstable! {
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use std::{write, writeln};
|
pub use std::{write, writeln};
|
||||||
}
|
}
|
||||||
|
|
||||||
mod macros;
|
|
||||||
|
|
|
@ -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<u32> = 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)*);
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
//! Platform-specific extensions for Unix platforms.
|
//! Platform-specific extensions for Unix platforms.
|
||||||
|
|
||||||
pub mod fs;
|
cfg_std! {
|
||||||
pub mod io;
|
pub mod io;
|
||||||
pub mod net;
|
}
|
||||||
|
|
||||||
|
cfg_default! {
|
||||||
|
pub mod fs;
|
||||||
|
pub mod net;
|
||||||
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
//! Platform-specific extensions for Windows.
|
//! Platform-specific extensions for Windows.
|
||||||
|
|
||||||
pub mod io;
|
cfg_std! {
|
||||||
|
pub mod io;
|
||||||
|
}
|
||||||
|
|
|
@ -11,35 +11,39 @@
|
||||||
//! use async_std::prelude::*;
|
//! use async_std::prelude::*;
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
#[doc(no_inline)]
|
cfg_std! {
|
||||||
pub use crate::future::Future;
|
#[doc(no_inline)]
|
||||||
#[doc(no_inline)]
|
pub use crate::future::Future;
|
||||||
pub use crate::stream::Stream;
|
#[doc(no_inline)]
|
||||||
#[doc(no_inline)]
|
pub use crate::stream::Stream;
|
||||||
pub use crate::task_local;
|
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use crate::future::future::FutureExt;
|
pub use crate::future::future::FutureExt;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use crate::stream::stream::StreamExt;
|
pub use crate::stream::stream::StreamExt;
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::io::BufRead as _;
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::io::Read as _;
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::io::Seek as _;
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::io::Write as _;
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::io::BufRead as _;
|
pub use crate::io::prelude::BufReadExt as _;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::io::Read as _;
|
pub use crate::io::prelude::ReadExt as _;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::io::Seek as _;
|
pub use crate::io::prelude::SeekExt as _;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::io::Write as _;
|
pub use crate::io::prelude::WriteExt as _;
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(no_inline)]
|
cfg_default! {
|
||||||
pub use crate::io::prelude::BufReadExt as _;
|
#[doc(no_inline)]
|
||||||
#[doc(no_inline)]
|
pub use crate::task_local;
|
||||||
pub use crate::io::prelude::ReadExt as _;
|
}
|
||||||
#[doc(no_inline)]
|
|
||||||
pub use crate::io::prelude::SeekExt as _;
|
|
||||||
#[doc(no_inline)]
|
|
||||||
pub use crate::io::prelude::WriteExt as _;
|
|
||||||
|
|
||||||
cfg_unstable! {
|
cfg_unstable! {
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
|
|
@ -7,12 +7,11 @@
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::task::{Context, Waker};
|
||||||
|
|
||||||
use crossbeam_utils::Backoff;
|
use crossbeam_utils::Backoff;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
|
|
||||||
use crate::task::{Context, Waker};
|
|
||||||
|
|
||||||
/// Set when the entry list is locked.
|
/// Set when the entry list is locked.
|
||||||
#[allow(clippy::identity_op)]
|
#[allow(clippy::identity_op)]
|
||||||
const LOCKED: usize = 1 << 0;
|
const LOCKED: usize = 1 << 0;
|
||||||
|
|
|
@ -118,41 +118,45 @@
|
||||||
//! [`task_local!`]: ../macro.task_local.html
|
//! [`task_local!`]: ../macro.task_local.html
|
||||||
//! [`with`]: struct.LocalKey.html#method.with
|
//! [`with`]: struct.LocalKey.html#method.with
|
||||||
|
|
||||||
#[doc(inline)]
|
cfg_std! {
|
||||||
pub use std::task::{Context, Poll, Waker};
|
#[doc(inline)]
|
||||||
|
pub use std::task::{Context, Poll, Waker};
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use async_macros::ready;
|
pub use async_macros::ready;
|
||||||
|
}
|
||||||
|
|
||||||
pub use block_on::block_on;
|
cfg_default! {
|
||||||
pub use builder::Builder;
|
pub use block_on::block_on;
|
||||||
pub use current::current;
|
pub use builder::Builder;
|
||||||
pub use join_handle::JoinHandle;
|
pub use current::current;
|
||||||
pub use sleep::sleep;
|
pub use task::Task;
|
||||||
pub use spawn::spawn;
|
pub use task_id::TaskId;
|
||||||
pub use task::Task;
|
pub use join_handle::JoinHandle;
|
||||||
pub use task_id::TaskId;
|
pub use sleep::sleep;
|
||||||
pub use task_local::{AccessError, LocalKey};
|
pub use spawn::spawn;
|
||||||
|
pub use task_local::{AccessError, LocalKey};
|
||||||
|
|
||||||
#[cfg(any(feature = "unstable", test))]
|
use builder::Runnable;
|
||||||
pub use spawn_blocking::spawn_blocking;
|
use task_local::LocalsMap;
|
||||||
#[cfg(not(any(feature = "unstable", test)))]
|
|
||||||
pub(crate) use spawn_blocking::spawn_blocking;
|
|
||||||
|
|
||||||
use builder::Runnable;
|
mod block_on;
|
||||||
use task_local::LocalsMap;
|
mod builder;
|
||||||
|
mod current;
|
||||||
|
mod executor;
|
||||||
|
mod join_handle;
|
||||||
|
mod sleep;
|
||||||
|
mod spawn;
|
||||||
|
mod spawn_blocking;
|
||||||
|
mod task;
|
||||||
|
mod task_id;
|
||||||
|
mod task_local;
|
||||||
|
|
||||||
mod block_on;
|
#[cfg(any(feature = "unstable", test))]
|
||||||
mod builder;
|
pub use spawn_blocking::spawn_blocking;
|
||||||
mod current;
|
#[cfg(not(any(feature = "unstable", test)))]
|
||||||
mod executor;
|
pub(crate) use spawn_blocking::spawn_blocking;
|
||||||
mod join_handle;
|
}
|
||||||
mod sleep;
|
|
||||||
mod spawn;
|
|
||||||
mod spawn_blocking;
|
|
||||||
mod task;
|
|
||||||
mod task_id;
|
|
||||||
mod task_local;
|
|
||||||
|
|
||||||
cfg_unstable! {
|
cfg_unstable! {
|
||||||
pub use yield_now::yield_now;
|
pub use yield_now::yield_now;
|
||||||
|
|
|
@ -5,57 +5,6 @@ use std::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
use crate::task::Task;
|
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<u32> = 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.
|
/// The key for accessing a task-local value.
|
||||||
///
|
///
|
||||||
/// Every task-local value is lazily initialized on first access and destroyed when the task
|
/// Every task-local value is lazily initialized on first access and destroyed when the task
|
||||||
|
|
31
src/utils.rs
31
src/utils.rs
|
@ -1,8 +1,7 @@
|
||||||
use std::mem;
|
|
||||||
|
|
||||||
/// Calls a function and aborts if it panics.
|
/// Calls a function and aborts if it panics.
|
||||||
///
|
///
|
||||||
/// This is useful in unsafe code where we can't recover from panics.
|
/// This is useful in unsafe code where we can't recover from panics.
|
||||||
|
#[cfg(feature = "default")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
|
pub fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
|
||||||
struct Bomb;
|
struct Bomb;
|
||||||
|
@ -15,11 +14,12 @@ pub fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
|
||||||
|
|
||||||
let bomb = Bomb;
|
let bomb = Bomb;
|
||||||
let t = f();
|
let t = f();
|
||||||
mem::forget(bomb);
|
std::mem::forget(bomb);
|
||||||
t
|
t
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a random number in `0..n`.
|
/// Generates a random number in `0..n`.
|
||||||
|
#[cfg(feature = "default")]
|
||||||
pub fn random(n: u32) -> u32 {
|
pub fn random(n: u32) -> u32 {
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::num::Wrapping;
|
use std::num::Wrapping;
|
||||||
|
@ -53,6 +53,7 @@ pub fn random(n: u32) -> u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Defers evaluation of a block of code until the end of the scope.
|
/// Defers evaluation of a block of code until the end of the scope.
|
||||||
|
#[cfg(feature = "default")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
macro_rules! defer {
|
macro_rules! defer {
|
||||||
($($body:tt)*) => {
|
($($body:tt)*) => {
|
||||||
|
@ -130,6 +131,30 @@ macro_rules! cfg_not_docs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Declares std items.
|
||||||
|
#[allow(unused_macros)]
|
||||||
|
#[doc(hidden)]
|
||||||
|
macro_rules! cfg_std {
|
||||||
|
($($item:item)*) => {
|
||||||
|
$(
|
||||||
|
#[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.
|
/// Defines an extension trait for a base trait.
|
||||||
///
|
///
|
||||||
/// In generated docs, the base trait will contain methods from the extension trait. In actual
|
/// In generated docs, the base trait will contain methods from the extension trait. In actual
|
||||||
|
|
Loading…
Reference in a new issue