summaryrefslogtreecommitdiffstats
path: root/tokio-macros
diff options
context:
space:
mode:
authorLucio Franco <luciofranco14@gmail.com>2020-10-12 13:44:54 -0400
committerGitHub <noreply@github.com>2020-10-12 13:44:54 -0400
commit8880222036f37c6204c8466f25e828447f16dacb (patch)
treefd623afc20f73bbce65746a3d1b1b2731ecf30a5 /tokio-macros
parent0893841f31542b2b04c5050a8a4a3c45cf867e55 (diff)
rt: Remove `threaded_scheduler()` and `basic_scheduler()` (#2876)
Co-authored-by: Alice Ryhl <alice@ryhl.io> Co-authored-by: Carl Lerche <me@carllerche.com>
Diffstat (limited to 'tokio-macros')
-rw-r--r--tokio-macros/src/entry.rs409
-rw-r--r--tokio-macros/src/lib.rs238
2 files changed, 248 insertions, 399 deletions
diff --git a/tokio-macros/src/entry.rs b/tokio-macros/src/entry.rs
index 2681f50d..f3c7c230 100644
--- a/tokio-macros/src/entry.rs
+++ b/tokio-macros/src/entry.rs
@@ -1,13 +1,139 @@
use proc_macro::TokenStream;
+use proc_macro2::Span;
use quote::quote;
-use std::num::NonZeroUsize;
+use syn::spanned::Spanned;
#[derive(Clone, Copy, PartialEq)]
-enum Runtime {
- Basic,
+enum RuntimeFlavor {
+ CurrentThread,
Threaded,
}
+impl RuntimeFlavor {
+ fn from_str(s: &str) -> Result<RuntimeFlavor, String> {
+ match s {
+ "current_thread" => Ok(RuntimeFlavor::CurrentThread),
+ "multi_thread" => Ok(RuntimeFlavor::Threaded),
+ "single_thread" => Err("The single threaded runtime flavor is called `current_thread`.".to_string()),
+ "basic_scheduler" => Err("The `basic_scheduler` runtime flavor has been renamed to `current_thread`.".to_string()),
+ "threaded_scheduler" => Err("The `threaded_scheduler` runtime flavor has been renamed to `multi_thread`.".to_string()),
+ _ => Err(format!("No such runtime flavor `{}`. The runtime flavors are `current_thread` and `multi_thread`.", s)),
+ }
+ }
+}
+
+struct FinalConfig {
+ flavor: RuntimeFlavor,
+ worker_threads: Option<usize>,
+}
+
+struct Configuration {
+ rt_threaded_available: bool,
+ default_flavor: RuntimeFlavor,
+ flavor: Option<RuntimeFlavor>,
+ worker_threads: Option<(usize, Span)>,
+}
+
+impl Configuration {
+ fn new(is_test: bool, rt_threaded: bool) -> Self {
+ Configuration {
+ rt_threaded_available: rt_threaded,
+ default_flavor: match is_test {
+ true => RuntimeFlavor::CurrentThread,
+ false => RuntimeFlavor::Threaded,
+ },
+ flavor: None,
+ worker_threads: None,
+ }
+ }
+
+ fn set_flavor(&mut self, runtime: syn::Lit, span: Span) -> Result<(), syn::Error> {
+ if self.flavor.is_some() {
+ return Err(syn::Error::new(span, "`flavor` set multiple times."));
+ }
+
+ let runtime_str = parse_string(runtime, span, "flavor")?;
+ let runtime =
+ RuntimeFlavor::from_str(&runtime_str).map_err(|err| syn::Error::new(span, err))?;
+ self.flavor = Some(runtime);
+ Ok(())
+ }
+
+ fn set_worker_threads(
+ &mut self,
+ worker_threads: syn::Lit,
+ span: Span,
+ ) -> Result<(), syn::Error> {
+ if self.worker_threads.is_some() {
+ return Err(syn::Error::new(
+ span,
+ "`worker_threads` set multiple times.",
+ ));
+ }
+
+ let worker_threads = parse_int(worker_threads, span, "worker_threads")?;
+ if worker_threads == 0 {
+ return Err(syn::Error::new(span, "`worker_threads` may not be 0."));
+ }
+ self.worker_threads = Some((worker_threads, span));
+ Ok(())
+ }
+
+ fn build(&self) -> Result<FinalConfig, syn::Error> {
+ let flavor = self.flavor.unwrap_or(self.default_flavor);
+ use RuntimeFlavor::*;
+ match (flavor, self.worker_threads) {
+ (CurrentThread, Some((_, worker_threads_span))) => Err(syn::Error::new(
+ worker_threads_span,
+ "The `worker_threads` option requires the `multi_thread` runtime flavor.",
+ )),
+ (CurrentThread, None) => Ok(FinalConfig {
+ flavor,
+ worker_threads: None,
+ }),
+ (Threaded, worker_threads) if self.rt_threaded_available => Ok(FinalConfig {
+ flavor,
+ worker_threads: worker_threads.map(|(val, _span)| val),
+ }),
+ (Threaded, _) => {
+ let msg = if self.flavor.is_none() {
+ "The default runtime flavor is `multi_thread`, but the `rt-threaded` feature is disabled."
+ } else {
+ "The runtime flavor `multi_thread` requires the `rt-threaded` feature."
+ };
+ Err(syn::Error::new(Span::call_site(), msg))
+ }
+ }
+ }
+}
+
+fn parse_int(int: syn::Lit, span: Span, field: &str) -> Result<usize, syn::Error> {
+ match int {
+ syn::Lit::Int(lit) => match lit.base10_parse::<usize>() {
+ Ok(value) => Ok(value),
+ Err(e) => Err(syn::Error::new(
+ span,
+ format!("Failed to parse {} as integer: {}", field, e),
+ )),
+ },
+ _ => Err(syn::Error::new(
+ span,
+ format!("Failed to parse {} as integer.", field),
+ )),
+ }
+}
+
+fn parse_string(int: syn::Lit, span: Span, field: &str) -> Result<String, syn::Error> {
+ match int {
+ syn::Lit::Str(s) => Ok(s.value()),
+ syn::Lit::Verbatim(s) => Ok(s.to_string()),
+ _ => Err(syn::Error::new(
+ span,
+ format!("Failed to parse {} as string.", field),
+ )),
+ }
+}
+
fn parse_knobs(
mut input: syn::ItemFn,
args: syn::AttributeArgs,
@@ -26,9 +152,12 @@ fn parse_knobs(
sig.asyncness = None;
- let mut runtime = None;
- let mut core_threads = None;
- let mut max_threads = None;
+ let macro_name = if is_test {
+ "tokio::test"
+ } else {
+ "tokio::main"
+ };
+ let mut config = Configuration::new(is_test, rt_threaded);
for arg in args {
match arg {
@@ -39,65 +168,18 @@ fn parse_knobs(
return Err(syn::Error::new_spanned(namevalue, msg));
}
match ident.unwrap().to_string().to_lowercase().as_str() {
+ "worker_threads" => {
+ config.set_worker_threads(namevalue.lit.clone(), namevalue.span())?;
+ }
+ "flavor" => {
+ config.set_flavor(namevalue.lit.clone(), namevalue.span())?;
+ }
"core_threads" => {
- if rt_threaded {
- match &namevalue.lit {
- syn::Lit::Int(expr) => {
- let num = expr.base10_parse::<NonZeroUsize>().unwrap();
- if num.get() > 1 {
- runtime = Some(Runtime::Threaded);
- } else {
- runtime = Some(Runtime::Basic);
- }
-
- if let Some(v) = max_threads {
- if v < num {
- return Err(syn::Error::new_spanned(
- namevalue,
- "max_threads cannot be less than core_threads",
- ));
- }
- }
-
- core_threads = Some(num);
- }
- _ => {
- return Err(syn::Error::new_spanned(
- namevalue,
- "core_threads argument must be an int",
- ))
- }
- }
- } else {
- return Err(syn::Error::new_spanned(
- namevalue,
- "core_threads can only be set with rt-threaded feature flag enabled",
- ));
- }
+ let msg = "Attribute `core_threads` is renamed to `worker_threads`";
+ return Err(syn::Error::new_spanned(namevalue, msg));
}
- "max_threads" => match &namevalue.lit {
- syn::Lit::Int(expr) => {
- let num = expr.base10_parse::<NonZeroUsize>().unwrap();
-
- if let Some(v) = core_threads {
- if num < v {
- return Err(syn::Error::new_spanned(
- namevalue,
- "max_threads cannot be less than core_threads",
- ));
- }
- }
- max_threads = Some(num);
- }
- _ => {
- return Err(syn::Error::new_spanned(
- namevalue,
- "max_threads argument must be an int",
- ))
- }
- },
name => {
- let msg = format!("Unknown attribute pair {} is specified; expected one of: `core_threads`, `max_threads`", name);
+ let msg = format!("Unknown attribute {} is specified; expected one of: `flavor`, `worker_threads`, `max_threads`", name);
return Err(syn::Error::new_spanned(namevalue, msg));
}
}
@@ -108,16 +190,22 @@ fn parse_knobs(
let msg = "Must have specified ident";
return Err(syn::Error::new_spanned(path, msg));
}
- match ident.unwrap().to_string().to_lowercase().as_str() {
- "threaded_scheduler" => {
- runtime = Some(runtime.unwrap_or_else(|| Runtime::Threaded))
- }
- "basic_scheduler" => runtime = Some(runtime.unwrap_or_else(|| Runtime::Basic)),
+ let name = ident.unwrap().to_string().to_lowercase();
+ let msg = match name.as_str() {
+ "threaded_scheduler" | "multi_thread" => {
+ format!("Set the runtime flavor with #[{}(flavor = \"multi_thread\")].", macro_name)
+ },
+ "basic_scheduler" | "current_thread" | "single_threaded" => {
+ format!("Set the runtime flavor with #[{}(flavor = \"current_thread\")].", macro_name)
+ },
+ "flavor" | "worker_threads" => {
+ format!("The `{}` attribute requires an argument.", name)
+ },
name => {
- let msg = format!("Unknown attribute {} is specified; expected `basic_scheduler` or `threaded_scheduler`", name);
- return Err(syn::Error::new_spanned(path, msg));
- }
- }
+ format!("Unknown attribute {} is specified; expected one of: `flavor`, `worker_threads`", name)
+ },
+ };
+ return Err(syn::Error::new_spanned(path, msg));
}
other => {
return Err(syn::Error::new_spanned(
@@ -128,15 +216,18 @@ fn parse_knobs(
}
}
- let mut rt = quote! { tokio::runtime::Builder::new().basic_scheduler() };
- if rt_threaded && (runtime == Some(Runtime::Threaded) || (runtime.is_none() && !is_test)) {
- rt = quote! { #rt.threaded_scheduler() };
- }
- if let Some(v) = core_threads.map(|v| v.get()) {
- rt = quote! { #rt.core_threads(#v) };
- }
- if let Some(v) = max_threads.map(|v| v.get()) {
- rt = quote! { #rt.max_threads(#v) };
+ let config = config.build()?;
+
+ let mut rt = match config.flavor {
+ RuntimeFlavor::CurrentThread => quote! {
+ tokio::runtime::Builder::new_current_thread()
+ },
+ RuntimeFlavor::Threaded => quote! {
+ tokio::runtime::Builder::new_multi_thread()
+ },
+ };
+ if let Some(v) = config.worker_threads {
+ rt = quote! { #rt.worker_threads(#v) };
}
let header = {
@@ -171,7 +262,7 @@ pub(crate) fn main(args: TokenStream, item: TokenStream, rt_threaded: bool) -> T
if input.sig.ident == "main" && !input.sig.inputs.is_empty() {
let msg = "the main function cannot accept arguments";
- return syn::Error::new_spanned(&input.sig.inputs, msg)
+ return syn::Error::new_spanned(&input.sig.ident, msg)
.to_compile_error()
.into();
}
@@ -201,159 +292,3 @@ pub(crate) fn test(args: TokenStream, item: TokenStream, rt_threaded: bool) -> T
parse_knobs(input, args, true, rt_threaded).unwrap_or_else(|e| e.to_compile_error().into())
}
-
-pub(crate) mod old {
- use proc_macro::TokenStream;
- use quote::quote;
-
- enum Runtime {
- Basic,
- Threaded,
- Auto,
- }
-
- #[cfg(not(test))] // Work around for rust-lang/rust#62127
- pub(crate) fn main(args: TokenStream, item: TokenStream) -> TokenStream {
- let mut input = syn::parse_macro_input!(item as syn::ItemFn);
- let args = syn::parse_macro_input!(args as syn::AttributeArgs);
-
- let sig = &mut input.sig;
- let name = &sig.ident;
- let inputs = &sig.inputs;
- let body = &input.block;
- let attrs = &input.attrs;
- let vis = input.vis;
-
- if sig.asyncness.is_none() {
- let msg = "the async keyword is missing from the function declaration";
- return syn::Error::new_spanned(sig.fn_token, msg)
- .to_compile_error()
- .into();
- } else if name == "main" && !inputs.is_empty() {
- let msg = "the main function cannot accept arguments";
- return syn::Error::new_spanned(&sig.inputs, msg)
- .to_compile_error()
- .into();
- }
-
- sig.asyncness = None;
-
- let mut runtime = Runtime::Auto;
-
- for arg in args {
- if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg {
- let ident = path.get_ident();
- if ident.is_none() {
- let msg = "Must have specified ident";
- return syn::Error::new_spanned(path, msg).to_compile_error().into();
- }
- match ident.unwrap().to_string().to_lowercase().as_str() {
- "threaded_scheduler" => runtime = Runtime::Threaded,
- "basic_scheduler" => runtime = Runtime::Basic,
- name => {
- let msg = format!("Unknown attribute {} is specified; expected `basic_scheduler` or `threaded_scheduler`", name);
- return syn::Error::new_spanned(path, msg).to_compile_error().into();
- }
- }
- }
- }
-
- let result = match runtime {
- Runtime::Threaded | Runtime::Auto => quote! {
- #(#attrs)*
- #vis #sig {
- tokio::runtime::Runtime::new().unwrap().block_on(async { #body })
- }
- },
- Runtime::Basic => quote! {
- #(#attrs)*
- #vis #sig {
- tokio::runtime::Builder::new()
- .basic_scheduler()
- .enable_all()
- .build()
- .unwrap()
- .block_on(async { #body })
- }
- },
- };
-
- result.into()
- }
-
- pub(crate) fn test(args: TokenStream, item: TokenStream) -> TokenStream {
- let input = syn::parse_macro_input!(item as syn::ItemFn);
- let args = syn::parse_macro_input!(args as syn::AttributeArgs);
-
- let ret = &input.sig.output;
- let name = &input.sig.ident;
- let body = &input.block;
- let attrs = &input.attrs;
- let vis = input.vis;
-
- for attr in attrs {
- if attr.path.is_ident("test") {
- let msg = "second test attribute is supplied";
- return syn::Error::new_spanned(&attr, msg)
- .to_compile_error()
- .into();
- }
- }
-
- if input.sig.asyncness.is_none() {
- let msg = "the async keyword is missing from the function declaration";
- return syn::Error::new_spanned(&input.sig.fn_token, msg)
- .to_compile_error()
- .into();
- } else if !input.sig.inputs.is_empty() {
- let msg = "the test function cannot accept arguments";
- return syn::Error::new_spanned(&input.sig.inputs, msg)
- .to_compile_error()
- .into();
- }
-
- let mut runtime = Runtime::Auto;
-
- for arg in args {
- if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg {
- let ident = path.get_ident();
- if ident.is_none() {
- let msg = "Must have specified ident";
- return syn::Error::new_spanned(path, msg).to_compile_error().into();
- }
- match ident.unwrap().to_string().to_lowercase().as_str() {
- "threaded_scheduler" => runtime = Runtime::Threaded,
- "basic_scheduler" => runtime = Runtime::Basic,
- name => {
- let msg = format!("Unknown attribute {} is specified; expected `basic_scheduler` or `threaded_scheduler`", name);
- return syn::Error::new_spanned(path, msg).to_compile_error().into();
- }
- }
- }
- }
-
- let result = match runtime {
- Runtime::Threaded => quote! {
- #[::core::prelude::v1::test]
- #(#attrs)*
- #vis fn #name() #ret {
- tokio::runtime::Runtime::new().unwrap().block_on(async { #body })
- }
- },
- Runtime::Basic | Runtime::Auto => quote! {
- #[::core::prelude::v1::test]
- #(#attrs)*
- #vis fn #name() #ret {
- tokio::runtime::Builder::new()
- .basic_scheduler()
- .enable_all()
- .build()
- .unwrap()
- .block_on(async { #body })
- }
- },
- };
-
- result.into()
- }
-}
diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs
index 09733ba5..1d6f577a 100644
--- a/tokio-macros/src/lib.rs
+++ b/tokio-macros/src/lib.rs
@@ -24,23 +24,38 @@ mod select;
use proc_macro::TokenStream;
-/// Marks async function to be executed by selected runtime. This macro helps set up a `Runtime`
-/// without requiring the user to use [Runtime](../tokio/runtime/struct.Runtime.html) or
+/// Marks async function to be executed by the selected runtime. This macro helps
+/// set up a `Runtime` without requiring the user to use
+/// [Runtime](../tokio/runtime/struct.Runtime.html) or
/// [Builder](../tokio/runtime/struct.Builder.html) directly.
///
-/// Note: This macro is designed to be simplistic and targets applications that do not require
-/// a complex setup. If provided functionality is not sufficient, user may be interested in
-/// using [Builder](../tokio/runtime/struct.Builder.html), which provides a more powerful
-/// interface.
+/// Note: This macro is designed to be simplistic and targets applications that
+/// do not require a complex setup. If the provided functionality is not
+/// sufficient, you may be interested in using
+/// [Builder](../tokio/runtime/struct.Builder.html), which provides a more
+/// powerful interface.
///
-/// ## Options:
+/// # Multi-threaded runtime
///
-/// If you want to set the number of worker threads used for asynchronous code, use the
-/// `core_threads` option.
+/// To use the multi-threaded runtime, the macro can be configured using
///
-/// - `core_threads=n` - Sets core threads to `n` (requires `rt-threaded` feature).
-/// - `max_threads=n` - Sets max threads to `n` (requires `rt-core` or `rt-threaded` feature).
-/// - `basic_scheduler` - Use the basic schduler (requires `rt-core`).
+/// ```
+/// #[tokio::main(flavor = "multi_thread", worker_threads = 10)]
+/// # async fn main() {}
+/// ```
+///
+/// The `worker_threads` option configures the number of worker threads, and
+/// defaults to the number of cpus on the system. This is the default flavor.
+///
+/// # Current thread runtime
+///
+/// To use the single-threaded runtime known as the `current_thread` runtime,
+/// the macro can be configured using
+///
+/// ```
+/// #[tokio::main(flavor = "current_thread")]
+/// # async fn main() {}
+/// ```
///
/// ## Function arguments:
///
@@ -48,7 +63,7 @@ use proc_macro::TokenStream;
///
/// ## Usage
///
-/// ### Using default
+/// ### Using the multi-thread runtime
///
/// ```rust
/// #[tokio::main]
@@ -61,8 +76,7 @@ use proc_macro::TokenStream;
///
/// ```rust
/// fn main() {
-/// tokio::runtime::Builder::new()
-/// .threaded_scheduler()
+/// tokio::runtime::Builder::new_multi_thread()
/// .enable_all()
/// .build()
/// .unwrap()
@@ -72,12 +86,12 @@ use proc_macro::TokenStream;
/// }
/// ```
///
-/// ### Using basic scheduler
+/// ### Using current thread runtime
///
/// The basic scheduler is single-threaded.
///
/// ```rust
-/// #[tokio::main(basic_scheduler)]
+/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// println!("Hello world");
/// }
@@ -87,8 +101,7 @@ use proc_macro::TokenStream;
///
/// ```rust
/// fn main() {
-/// tokio::runtime::Builder::new()
-/// .basic_scheduler()
+/// tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .build()
/// .unwrap()
@@ -98,89 +111,10 @@ use proc_macro::TokenStream;
/// }
/// ```
///
-/// ### Set number of core threads
-///
-/// ```rust
-/// #[tokio::main(core_threads = 2)]
-/// async fn main() {
-/// println!("Hello world");
-/// }
-/// ```
-///
-/// Equivalent code not using `#[tokio::main]`
-///
-/// ```rust
-/// fn main() {
-/// tokio::runtime::Builder::new()
-/// .threaded_scheduler()
-/// .core_threads(2)
-/// .enable_all()
-/// .build()
-/// .unwrap()
-/// .block_on(async {
-/// println!("Hello world");
-/// })
-/// }
-/// ```
-///
-/// ### NOTE:
-///
-/// If you rename the tokio crate in your dependencies this macro
-/// will not work. If you must rename the 0.2 version of tokio because
-/// you're also using the 0.1 version of tokio, you _must_ make the
-/// tokio 0.2 crate available as `tokio` in the module where this
-/// macro is expanded.
-#[proc_macro_attribute]
-#[cfg(not(test))] // Work around for rust-lang/rust#62127
-pub fn main_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
- entry::main(args, item, true)
-}
-
-/// Marks async function to be executed by selected runtime. This macro helps set up a `Runtime`
-/// without requiring the user to use [Runtime](../tokio/runtime/struct.Runtime.html) or
-/// [Builder](../tokio/runtime/struct.Builder.html) directly.
-///
-/// Note: This macro is designed to be simplistic and targets applications that do not require
-/// a complex setup. If provided functionality is not sufficient, user may be interested in
-/// using [Builder](../tokio/runtime/struct.Builder.html), which provides a more powerful
-/// interface.
-///
-/// ## Options:
-///
-/// - `basic_scheduler` - All tasks are executed on the current thread.
-/// - `threaded_scheduler` - Uses the multi-threaded scheduler. Used by default (requires `rt-threaded` feature).
-///
-/// ## Function arguments:
-///
-/// Arguments are allowed for any functions aside from `main` which is special
-///
-/// ## Usage
-///
-/// ### Using default
-///
-/// ```rust
-/// #[tokio::main]
-/// async fn main() {
-/// println!("Hello world");
-/// }
-/// ```
-///
-/// Equivalent code not using `#[tokio::main]`
-///
-/// ```rust
-/// fn main() {
-/// tokio::runtime::Runtime::new()
-/// .unwrap()
-/// .block_on(async {
-/// println!("Hello world");
-/// })
-/// }
-/// ```
-///
-/// ### Select runtime
+/// ### Set number of worker threads
///
/// ```rust
-/// #[tokio::main(basic_scheduler)]
+/// #[tokio::main(worker_threads = 2)]
/// async fn main() {
/// println!("Hello world");
/// }
@@ -190,8 +124,8 @@ pub fn main_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ```rust
/// fn main() {
-/// tokio::runtime::Builder::new()
-/// .basic_scheduler()
+/// tokio::runtime::Builder::new_multi_thread()
+/// .worker_threads(2)
/// .enable_all()
/// .build()
/// .unwrap()
@@ -203,25 +137,20 @@ pub fn main_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ### NOTE:
///
-/// If you rename the tokio crate in your dependencies this macro
-/// will not work. If you must rename the 0.2 version of tokio because
-/// you're also using the 0.1 version of tokio, you _must_ make the
-/// tokio 0.2 crate available as `tokio` in the module where this
-/// macro is expanded.
+/// If you rename the tokio crate in your dependencies this macro will not work.
+/// If you must rename the 0.2 version of tokio because you're also using the
+/// 0.1 version of tokio, you _must_ make the tokio 0.2 crate available as
+/// `tokio` in the module where this macro is expanded.
#[proc_macro_attribute]
#[cfg(not(test))] // Work around for rust-lang/rust#62127
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
- entry::old::main(args, item)
+ entry::main(args, item, true)
}
/// Marks async function to be executed by selected runtime. This macro helps set up a `Runtime`
/// without requiring the user to use [Runtime](../tokio/runtime/struct.Runtime.html) or
/// [Builder](../tokio/runtime/struct.builder.html) directly.
///
-/// ## Options:
-///
-/// - `max_threads=n` - Sets max threads to `n`.
-///
/// ## Function arguments:
///
/// Arguments are allowed for any functions aside from `main` which is special
@@ -231,7 +160,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
/// ### Using default
///
/// ```rust
-/// #[tokio::main]
+/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// println!("Hello world");
/// }
@@ -241,8 +170,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ```rust
/// fn main() {
-/// tokio::runtime::Builder::new()
-/// .basic_scheduler()
+/// tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .build()
/// .unwrap()
@@ -261,23 +189,18 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
/// macro is expanded.
#[proc_macro_attribute]
#[cfg(not(test))] // Work around for rust-lang/rust#62127
-pub fn main_basic(args: TokenStream, item: TokenStream) -> TokenStream {
+pub fn main_rt_core(args: TokenStream, item: TokenStream) -> TokenStream {
entry::main(args, item, false)
}
/// Marks async function to be executed by runtime, suitable to test environment
///
-/// ## Options:
-///
-/// - `core_threads=n` - Sets core threads to `n` (requires `rt-threaded` feature).
-/// - `max_threads=n` - Sets max threads to `n` (requires `rt-core` or `rt-threaded` feature).
-///
/// ## Usage
///
-/// ### Select runtime
+/// ### Multi-thread runtime
///
/// ```no_run
-/// #[tokio::test(core_threads = 1)]
+/// #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
/// async fn my_test() {
/// assert!(true);
/// }
@@ -285,6 +208,8 @@ pub fn main_basic(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ### Using default
///
+/// The default test runtime is multi-threaded.
+///
/// ```no_run
/// #[tokio::test]
/// async fn my_test() {
@@ -300,7 +225,7 @@ pub fn main_basic(args: TokenStream, item: TokenStream) -> TokenStream {
/// tokio 0.2 crate available as `tokio` in the module where this
/// macro is expanded.
#[proc_macro_attribute]
-pub fn test_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
+pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
entry::test(args, item, true)
}
@@ -308,22 +233,10 @@ pub fn test_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ## Options:
///
-/// - `basic_scheduler` - All tasks are executed on the current thread. Used by default.
-/// - `threaded_scheduler` - Use multi-threaded scheduler (requires `rt-threaded` feature).
+/// - `max_threads=n` - Sets max threads to `n`.
///
/// ## Usage
///
-/// ### Select runtime
-///
-/// ```no_run
-/// #[tokio::test(threaded_scheduler)]
-/// async fn my_test() {
-/// assert!(true);
-/// }
-/// ```
-///
-/// ### Using default
-///
/// ```no_run
/// #[tokio::test]
/// async fn my_test() {
@@ -339,35 +252,36 @@ pub fn test_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
/// tokio 0.2 crate available as `tokio` in the module where this
/// macro is expanded.
#[proc_macro_attribute]
-pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
- entry::old::test(args, item)
+pub fn test_rt_core(args: TokenStream, item: TokenStream) -> TokenStream {
+ entry::test(args, item, false)
}
-/// Marks async function to be executed by runtime, suitable to test environment
-///
-/// ## Options:
-///
-/// - `max_threads=n` - Sets max threads to `n`.
-///
-/// ## Usage
-///
-/// ```no_run
-/// #[tokio::test]
-/// async fn my_test() {
-/// assert!(true);
-/// }
+/// Always fails with the error message below.
+/// ```text
+/// The #[tokio::main] macro requires rt-core or rt-threaded.
/// ```
-///
-/// ### NOTE:
-///
-/// If you rename the tokio crate in your dependencies this macro
-/// will not work. If you must rename the 0.2 version of tokio because
-/// you're also using the 0.1 version of tokio, you _must_ make the
-/// tokio 0.2 crate available as `tokio` in the module where this
-/// macro is expanded.
#[proc_macro_attribute]
-pub fn test_basic(args: TokenStream, item: TokenStream) -> TokenStream {
- entry::test(args, item, false)
+pub fn main_fail(_args: TokenStream, _item: TokenStream) -> TokenStream {
+ syn::Error::new(
+ proc_macro2::Span::call_site(),
+ "The #[tokio::main] macro requires rt-core or rt-threaded.",
+ )
+ .to_compile_error()
+ .into()
+}
+
+/// Always fails with the error message below.
+/// ```text
+/// The #[tokio::test] macro requires rt-core or rt-threaded.
+/// ```
+#[proc_macro_attribute]
+pub fn test_fail(_args: TokenStream, _item: TokenStream) -> TokenStream {
+ syn::Error::new(
+ proc_macro2::Span::call_site(),
+ "The #[tokio::test] macro requires rt-core or rt-threaded.",
+ )
+ .to_compile_error()
+ .into()
}
/// Implementation detail of the `select!` macro. This macro is **not** intended