summaryrefslogtreecommitdiffstats
path: root/tokio-macros
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-04-25 19:22:32 -0700
committerDavid Barsky <dbarsky@amazon.com>2019-04-25 22:22:32 -0400
commit0e400af78c049c4b52fa4cb346237b43218b0ec9 (patch)
treeff9f15c215826f3d3fdd3e9dc4d3f912b109ceba /tokio-macros
parentdf702130d6b3d0c9a84b3cc3851423d99f8274bc (diff)
Async/await polish (#1058)
A general refresh of Tokio's experimental async / await support.
Diffstat (limited to 'tokio-macros')
-rw-r--r--tokio-macros/Cargo.toml19
-rw-r--r--tokio-macros/LICENSE47
-rw-r--r--tokio-macros/README.md13
-rw-r--r--tokio-macros/src/lib.rs79
4 files changed, 158 insertions, 0 deletions
diff --git a/tokio-macros/Cargo.toml b/tokio-macros/Cargo.toml
new file mode 100644
index 00000000..9e4b6f8b
--- /dev/null
+++ b/tokio-macros/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "tokio-macros"
+version = "0.1.0"
+authors = ["Tokio Contributors <team@tokio.rs>"]
+edition = "2018"
+publish = false
+
+[lib]
+proc-macro = true
+
+[features]
+# This feature comes with no promise of stability. Things will
+# break with each patch release. Use at your own risk.
+async-await-preview = []
+
+[dependencies]
+proc-macro2 = "0.4.27"
+quote = "0.6.11"
+syn = { version = "0.15.27", features = ["full", "extra-traits", "visit-mut"] }
diff --git a/tokio-macros/LICENSE b/tokio-macros/LICENSE
new file mode 100644
index 00000000..0fbb7399
--- /dev/null
+++ b/tokio-macros/LICENSE
@@ -0,0 +1,47 @@
+Copyright (c) 2019 Tokio Contributors
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+
+The MIT License (MIT)
+
+Copyright (c) 2019 Yoshua Wuyts
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/tokio-macros/README.md b/tokio-macros/README.md
new file mode 100644
index 00000000..988726f4
--- /dev/null
+++ b/tokio-macros/README.md
@@ -0,0 +1,13 @@
+# Tokio Macros
+
+Procedural macros for use with Tokio
+
+## License
+
+This project is licensed under the [MIT license](LICENSE).
+
+### Contribution
+
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in Tokio by you, shall be licensed as MIT, without any additional
+terms or conditions.
diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs
new file mode 100644
index 00000000..afe3b1f5
--- /dev/null
+++ b/tokio-macros/src/lib.rs
@@ -0,0 +1,79 @@
+#![cfg(feature = "async-await-preview")]
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+use quote::{quote, quote_spanned};
+use syn::spanned::Spanned;
+
+/// Define the program entry point
+///
+/// # Examples
+///
+/// ```
+/// #[tokio::main]
+/// async fn main() {
+/// println!("Hello world");
+/// }
+#[proc_macro_attribute]
+pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream {
+ let input = syn::parse_macro_input!(item as syn::ItemFn);
+
+ let ret = &input.decl.output;
+ let name = &input.ident;
+ let body = &input.block;
+
+ if input.asyncness.is_none() {
+ let tokens = quote_spanned! { input.span() =>
+ compile_error!("the async keyword is missing from the function declaration");
+ };
+
+ return TokenStream::from(tokens);
+ }
+
+ let result = quote! {
+ fn #name() #ret {
+ let mut rt = tokio::runtime::Runtime::new().unwrap();
+ rt.block_on_async(async { #body })
+ }
+ };
+
+ result.into()
+}
+
+/// Define a Tokio aware unit test
+///
+/// # Examples
+///
+/// ```
+/// #[tokio::test]
+/// async fn my_test() {
+/// assert!(true);
+/// }
+/// ```
+#[proc_macro_attribute]
+pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
+ let input = syn::parse_macro_input!(item as syn::ItemFn);
+
+ let ret = &input.decl.output;
+ let name = &input.ident;
+ let body = &input.block;
+
+ if input.asyncness.is_none() {
+ let tokens = quote_spanned! { input.span() =>
+ compile_error!("the async keyword is missing from the function declaration");
+ };
+
+ return TokenStream::from(tokens);
+ }
+
+ let result = quote! {
+ #[test]
+ fn #name() #ret {
+ let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
+ rt.block_on_async(async { #body })
+ }
+ };
+
+ result.into()
+}