runtime: rename current_thread -> basic_scheduler (#1769)

It no longer supports executing !Send futures. The use case for
It is wanting a “light” runtime. There will be “local” task execution
using a different strategy coming later.

This patch also renames `thread_pool` -> `threaded_scheduler`, but
only in public APIs for now.
This commit is contained in:
Carl Lerche
2019-11-16 07:19:45 -08:00
committed by GitHub
parent 1474794055
commit 3f0eabe779
16 changed files with 122 additions and 115 deletions
+31 -30
View File
@@ -18,9 +18,9 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
enum RuntimeType {
Single,
Multi,
enum Runtime {
Basic,
Threaded,
Auto,
}
@@ -28,8 +28,8 @@ enum RuntimeType {
///
/// ## Options:
///
/// - `current_thread` - Uses the `current_thread` runtime.
/// - `threadpool` - Uses the multi-threaded `threadpool` runtime. Used by default.
/// - `basic_scheduler` - All tasks are executed on the current thread.
/// - `threaded_scheduler` - Uses the multi-threaded scheduler. Used by default.
///
/// ## Function arguments:
///
@@ -37,14 +37,6 @@ enum RuntimeType {
///
/// ## Usage
///
/// ### Select runtime
///
/// ```rust
/// #[tokio::main(current_thread)]
/// async fn main() {
/// println!("Hello world");
/// }
/// ```
/// ### Using default
///
/// ```rust
@@ -53,6 +45,15 @@ enum RuntimeType {
/// println!("Hello world");
/// }
/// ```
///
/// ### Select runtime
///
/// ```rust
/// #[tokio::main(basic_scheduler)]
/// async fn main() {
/// println!("Hello world");
/// }
/// ```
#[proc_macro_attribute]
#[cfg(not(test))] // Work around for rust-lang/rust#62127
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
@@ -77,7 +78,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
.into();
}
let mut runtime = RuntimeType::Auto;
let mut runtime = Runtime::Auto;
for arg in args {
if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg {
@@ -87,10 +88,10 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
return syn::Error::new_spanned(path, msg).to_compile_error().into();
}
match ident.unwrap().to_string().to_lowercase().as_str() {
"threadpool" => runtime = RuntimeType::Multi,
"current_thread" => runtime = RuntimeType::Single,
"threaded_scheduler" => runtime = Runtime::Threaded,
"basic_scheduler" => runtime = Runtime::Basic,
name => {
let msg = format!("Unknown attribute {} is specified; expected `current_thread` or `threadpool`", 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();
}
}
@@ -98,17 +99,17 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
}
let result = match runtime {
RuntimeType::Multi | RuntimeType::Auto => quote! {
Runtime::Threaded | Runtime::Auto => quote! {
#(#attrs)*
fn #name(#inputs) #ret {
tokio::runtime::Runtime::new().unwrap().block_on(async { #body })
}
},
RuntimeType::Single => quote! {
Runtime::Basic => quote! {
#(#attrs)*
fn #name(#inputs) #ret {
tokio::runtime::Builder::new()
.current_thread()
.basic_scheduler()
.build()
.unwrap()
.block_on(async { #body })
@@ -123,15 +124,15 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ## Options:
///
/// - `current_thread` - Uses the `current_thread` runtime. Used by default.
/// - `threadpool` - Uses multi-threaded runtime.
/// - `basic_scheduler` - All tasks are executed on the current thread. Used by default.
/// - `threaded_scheduler` - Use multi-threaded scheduler.
///
/// ## Usage
///
/// ### Select runtime
///
/// ```no_run
/// #[tokio::test(threadpool)]
/// #[tokio::test(threaded_scheduler)]
/// async fn my_test() {
/// assert!(true);
/// }
@@ -176,7 +177,7 @@ pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
.into();
}
let mut runtime = RuntimeType::Auto;
let mut runtime = Runtime::Auto;
for arg in args {
if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg {
@@ -186,10 +187,10 @@ pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
return syn::Error::new_spanned(path, msg).to_compile_error().into();
}
match ident.unwrap().to_string().to_lowercase().as_str() {
"threadpool" => runtime = RuntimeType::Multi,
"current_thread" => runtime = RuntimeType::Single,
"threaded_scheduler" => runtime = Runtime::Threaded,
"basic_scheduler" => runtime = Runtime::Basic,
name => {
let msg = format!("Unknown attribute {} is specified; expected `current_thread` or `threadpool`", 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();
}
}
@@ -197,19 +198,19 @@ pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
}
let result = match runtime {
RuntimeType::Multi => quote! {
Runtime::Threaded => quote! {
#[test]
#(#attrs)*
fn #name() #ret {
tokio::runtime::Runtime::new().unwrap().block_on(async { #body })
}
},
RuntimeType::Single | RuntimeType::Auto => quote! {
Runtime::Basic | Runtime::Auto => quote! {
#[test]
#(#attrs)*
fn #name() #ret {
tokio::runtime::Builder::new()
.current_thread()
.basic_scheduler()
.build()
.unwrap()
.block_on(async { #body })