tokio: add rt-current-thread optional feature

- Adds a minimum `rt-current-thread` optional feature that exports
  `tokio::runtime::current_thread`.
- Adds a `macros` optional feature to enable the `#[tokio::main]` and
  `#[tokio::test]` attributes.
- Adjusts `#[tokio::main]` macro to select a runtime "automatically" if
  a specific strategy isn't specified. Allows using the macro with only
  the rt-current-thread feature.
This commit is contained in:
Sean McArthur
2019-09-24 12:17:04 -07:00
parent c81447fdcc
commit 18cef1901f
4 changed files with 36 additions and 3 deletions
+9 -1
View File
@@ -49,6 +49,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
enum RuntimeType {
Single,
Multi,
Auto,
}
let input = syn::parse_macro_input!(item as syn::ItemFn);
@@ -71,7 +72,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
.into();
}
let mut runtime = RuntimeType::Multi;
let mut runtime = RuntimeType::Auto;
for arg in args {
if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg {
@@ -106,6 +107,13 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
rt.block_on(async { #body })
}
},
RuntimeType::Auto => quote! {
#(#attrs)*
fn #name() #ret {
let mut rt = tokio::runtime::__main::Runtime::new().unwrap();
rt.block_on(async { #body })
}
},
};
result.into()
+7 -1
View File
@@ -37,15 +37,21 @@ default = [
codec = ["io", "tokio-codec", "bytes"]
fs = ["tokio-fs"]
io = ["tokio-io"]
macros = ["tokio-macros"]
net = ["tcp", "udp", "uds"]
rt-current-thread = [
"timer",
"tokio-net",
"tokio-executor/current-thread",
]
rt-full = [
"macros",
"num_cpus",
"net",
"sync",
"timer",
"tokio-executor/current-thread",
"tokio-executor/threadpool",
"tokio-macros",
"tracing-core",
]
signal = ["tokio-net/signal"]
+6 -1
View File
@@ -72,7 +72,10 @@
macro_rules! if_runtime {
($($i:item)*) => ($(
#[cfg(any(feature = "rt-full"))]
#[cfg(any(
feature = "rt-full",
feature = "rt-current-thread",
))]
$i
)*)
}
@@ -103,8 +106,10 @@ if_runtime! {
pub use crate::executor::spawn;
#[cfg(not(test))] // Work around for rust-lang/rust#62127
#[cfg(feature = "macros")]
#[doc(inline)]
pub use tokio_macros::main;
#[cfg(feature = "macros")]
#[doc(inline)]
pub use tokio_macros::test;
}
+14
View File
@@ -135,10 +135,24 @@
//! [`tokio::main`]: ../../tokio_macros/attr.main.html
pub mod current_thread;
#[cfg(feature = "rt-full")]
mod threadpool;
#[cfg(feature = "rt-full")]
pub use self::threadpool::{
Builder,
Runtime,
TaskExecutor,
};
// Internal export, don't use.
// This exists to support "auto" runtime selection when using the
// #[tokio::main] attribute.
#[doc(hidden)]
pub mod __main {
#[cfg(feature = "rt-full")]
pub use super::Runtime;
#[cfg(not(feature = "rt-full"))]
pub use super::current_thread::Runtime;
}