diff options
author | Carl Lerche <me@carllerche.com> | 2019-11-18 07:00:55 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-18 07:00:55 -0800 |
commit | 0d38936b35779b604770120da2e98560bbb6241f (patch) | |
tree | 843d46e999becdb580cb02655b4290acadd64474 /tokio/src/macros | |
parent | 13b6e9939e062dc01bcf34abe3d75de4b66e20e1 (diff) |
chore: refine feature flags (#1785)
Removes dependencies between Tokio feature flags. For example, `process`
should not depend on `sync` simply because it uses the `mpsc` channel.
Instead, feature flags represent **public** APIs that become available
with the feature enabled. When the feature is not enabled, the
functionality is removed. If another Tokio component requires the
functionality, it is stays as `pub(crate)`.
The threaded scheduler is now exposed under `rt-threaded`. This feature
flag only enables the threaded scheduler and does not include I/O,
networking, or time. Those features must be explictly enabled.
A `full` feature flag is added that enables all features.
`stdin`, `stdout`, `stderr` are exposed under `io-std`.
Macros are used to scope code by feature flag.
Diffstat (limited to 'tokio/src/macros')
-rw-r--r-- | tokio/src/macros/assert.rs | 19 | ||||
-rw-r--r-- | tokio/src/macros/cfg.rs | 217 | ||||
-rw-r--r-- | tokio/src/macros/loom.rs | 12 | ||||
-rw-r--r-- | tokio/src/macros/mod.rs | 17 | ||||
-rw-r--r-- | tokio/src/macros/ready.rs | 8 | ||||
-rw-r--r-- | tokio/src/macros/thread_local.rs | 4 |
6 files changed, 277 insertions, 0 deletions
diff --git a/tokio/src/macros/assert.rs b/tokio/src/macros/assert.rs new file mode 100644 index 00000000..fd6601b4 --- /dev/null +++ b/tokio/src/macros/assert.rs @@ -0,0 +1,19 @@ +/// Assert option is some +macro_rules! assert_some { + ($e:expr) => {{ + match $e { + Some(v) => v, + _ => panic!("expected some, was none"), + } + }}; +} + +/// Assert option is none +macro_rules! assert_none { + ($e:expr) => {{ + match $e { + Some(v) => panic!("expected none, was {:?}", v), + _ => {} + } + }}; +} diff --git a/tokio/src/macros/cfg.rs b/tokio/src/macros/cfg.rs new file mode 100644 index 00000000..5e84a3ac --- /dev/null +++ b/tokio/src/macros/cfg.rs @@ -0,0 +1,217 @@ +#![allow(unused_macros)] + +macro_rules! cfg_atomic_waker { + ($($item:item)*) => { + $( #[cfg(any(feature = "io-driver", feature = "time"))] $item )* + } +} + +macro_rules! cfg_blocking { + ($($item:item)*) => { + $( #[cfg(feature = "blocking")] $item )* + } +} + +/// Enable blocking API internals +macro_rules! cfg_blocking_impl { + ($($item:item)*) => { + $( + #[cfg(any( + feature = "blocking", + feature = "fs", + feature = "dns", + feature = "io-std", + feature = "rt-threaded", + ))] + $item + )* + } +} + +/// Enable blocking API internals +macro_rules! cfg_not_blocking_impl { + ($($item:item)*) => { + $( + #[cfg(not(any( + feature = "blocking", + feature = "fs", + feature = "dns", + feature = "io-std", + feature = "rt-threaded", + )))] + $item + )* + } +} + +macro_rules! cfg_dns { + ($($item:item)*) => { + $( #[cfg(feature = "dns")] $item )* + } +} + +macro_rules! cfg_fs { + ($($item:item)*) => { $( #[cfg(feature = "fs")] $item )* } +} + +macro_rules! cfg_io_blocking { + ($($item:item)*) => { + $( #[cfg(any(feature = "io-std", feature = "fs"))] $item )* + } +} + +macro_rules! cfg_io_driver { + ($($item:item)*) => { + $( #[cfg(feature = "io-driver")] $item )* + } +} + +macro_rules! cfg_not_io_driver { + ($($item:item)*) => { + $( #[cfg(not(feature = "io-driver"))] $item )* + } +} + +macro_rules! cfg_io_std { + ($($item:item)*) => { + $( #[cfg(feature = "io-std")] $item )* + } +} + +macro_rules! cfg_io_util { + ($($item:item)*) => { + $( #[cfg(feature = "io-util")] $item )* + } +} + +macro_rules! cfg_not_io_util { + ($($item:item)*) => { + $( #[cfg(not(feature = "io-util"))] $item )* + } +} + +macro_rules! cfg_loom { + ($($item:item)*) => { + $( #[cfg(loom)] $item )* + } +} + +macro_rules! cfg_not_loom { + ($($item:item)*) => { + $( #[cfg(not(loom))] $item )* + } +} + +macro_rules! cfg_macros { + ($($item:item)*) => { + $( + #[cfg(feature = "macros")] + #[doc(inline)] + $item + )* + } +} + +macro_rules! cfg_process { + ($($item:item)*) => { + $( + #[cfg(feature = "process")] + #[cfg(not(loom))] + $item + )* + } +} + +macro_rules! cfg_signal { + ($($item:item)*) => { + $( + #[cfg(feature = "signal")] + #[cfg(not(loom))] + $item + )* + } +} + +macro_rules! cfg_stream { + ($($item:item)*) => { + $( #[cfg(feature = "stream")] $item )* + } +} + +macro_rules! cfg_sync { + ($($item:item)*) => { + $( #[cfg(feature = "sync")] $item )* + } +} + +macro_rules! cfg_not_sync { + ($($item:item)*) => { + $( #[cfg(not(feature = "sync"))] $item )* + } +} + +macro_rules! cfg_rt_core { + ($($item:item)*) => { + $( #[cfg(feature = "rt-core")] $item )* + } +} + +macro_rules! cfg_not_rt_core { + ($($item:item)*) => { + $( #[cfg(not(feature = "rt-core"))] $item )* + } +} + +macro_rules! cfg_rt_threaded { + ($($item:item)*) => { + $( #[cfg(feature = "rt-threaded")] $item )* + } +} + +macro_rules! cfg_not_rt_threaded { + ($($item:item)*) => { + $( #[cfg(not(feature = "rt-threaded"))] $item )* + } +} + +macro_rules! cfg_tcp { + ($($item:item)*) => { + $( #[cfg(feature = "tcp")] $item )* + } +} + +macro_rules! cfg_test_util { + ($($item:item)*) => { + $( #[cfg(feature = "test-util")] $item )* + } +} + +macro_rules! cfg_not_test_util { + ($($item:item)*) => { + $( #[cfg(not(feature = "test-util"))] $item )* + } +} + +macro_rules! cfg_time { + ($($item:item)*) => { + $( #[cfg(feature = "time")] $item )* + } +} + +macro_rules! cfg_not_time { + ($($item:item)*) => { + $( #[cfg(not(feature = "time"))] $item )* + } +} + +macro_rules! cfg_udp { + ($($item:item)*) => { + $( #[cfg(feature = "udp")] $item )* + } +} + +macro_rules! cfg_uds { + ($($item:item)*) => { + $( #[cfg(all(unix, feature = "uds"))] $item )* + } +} diff --git a/tokio/src/macros/loom.rs b/tokio/src/macros/loom.rs new file mode 100644 index 00000000..d57d9fb0 --- /dev/null +++ b/tokio/src/macros/loom.rs @@ -0,0 +1,12 @@ +macro_rules! if_loom { + ($($t:tt)*) => {{ + #[cfg(loom)] + const LOOM: bool = true; + #[cfg(not(loom))] + const LOOM: bool = false; + + if LOOM { + $($t)* + } + }} +} diff --git a/tokio/src/macros/mod.rs b/tokio/src/macros/mod.rs new file mode 100644 index 00000000..9136e594 --- /dev/null +++ b/tokio/src/macros/mod.rs @@ -0,0 +1,17 @@ +#![cfg_attr(not(feature = "full"), allow(unused_macros))] + +#[macro_use] +#[cfg(test)] +mod assert; + +#[macro_use] +mod cfg; + +#[macro_use] +mod loom; + +#[macro_use] +mod ready; + +#[macro_use] +mod thread_local; diff --git a/tokio/src/macros/ready.rs b/tokio/src/macros/ready.rs new file mode 100644 index 00000000..1f48623b --- /dev/null +++ b/tokio/src/macros/ready.rs @@ -0,0 +1,8 @@ +macro_rules! ready { + ($e:expr $(,)?) => { + match $e { + std::task::Poll::Ready(t) => t, + std::task::Poll::Pending => return std::task::Poll::Pending, + } + }; +} diff --git a/tokio/src/macros/thread_local.rs b/tokio/src/macros/thread_local.rs new file mode 100644 index 00000000..d8489473 --- /dev/null +++ b/tokio/src/macros/thread_local.rs @@ -0,0 +1,4 @@ +#[cfg(all(loom, test))] +macro_rules! thread_local { + ($($tts:tt)+) => { loom::thread_local!{ $($tts)+ } } +} |