aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2019-08-23 19:53:50 -0700
committerandroid-build-merger <android-build-merger@google.com>2019-08-23 19:53:50 -0700
commitb8c46d81cdf60b981a73d70dff48e225605bd668 (patch)
tree026e38b0e4a74c8d49bcc9d02fedfcaef1627a23
parent37df65b39a9bda7acb45a4889de971374eb8e3a3 (diff)
parentb027b17e08e552f1a51f6eafd1cec4598b3b03cb (diff)
downloadquote-b8c46d81cdf60b981a73d70dff48e225605bd668.tar.gz
Merge branch '0.6.13'
am: b027b17e08 Change-Id: Id20b104414b59ad4f2381980cc2a81a927aed46c
-rw-r--r--.gitignore2
-rw-r--r--.travis.yml18
-rw-r--r--Cargo.toml24
-rw-r--r--LICENSE-APACHE201
-rw-r--r--LICENSE-MIT25
-rw-r--r--README.md243
-rw-r--r--benches/bench.rs194
-rw-r--r--build.rs48
-rw-r--r--src/ext.rs112
-rw-r--r--src/lib.rs861
-rw-r--r--src/runtime.rs108
-rw-r--r--src/to_tokens.rs205
-rw-r--r--tests/conditional/integer128.rs11
-rw-r--r--tests/test.rs295
14 files changed, 2347 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4fffb2f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+/Cargo.lock
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..46d8006
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,18 @@
+sudo: false
+
+language: rust
+
+rust:
+ - stable
+ - 1.15.1
+ - beta
+
+script:
+ - cargo test
+
+matrix:
+ include:
+ - rust: nightly
+ script:
+ - cargo test
+ - cargo update -Z minimal-versions && cargo build
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..8171fb0
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,24 @@
+[package]
+name = "quote"
+version = "0.6.13" # don't forget to update html_root_url, version in readme for breaking changes
+authors = ["David Tolnay <dtolnay@gmail.com>"]
+license = "MIT OR Apache-2.0"
+description = "Quasi-quoting macro quote!(...)"
+repository = "https://github.com/dtolnay/quote"
+documentation = "https://docs.rs/quote/"
+keywords = ["syn"]
+categories = ["development-tools::procedural-macro-helpers"]
+readme = "README.md"
+include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
+
+[dependencies]
+proc-macro2 = { version = "0.4.21", default-features = false }
+
+[features]
+default = ["proc-macro"]
+# Disabling the proc-macro feature removes the dynamic library dependency on
+# libproc_macro in the rustc compiler.
+proc-macro = ["proc-macro2/proc-macro"]
+
+[badges]
+travis-ci = { repository = "dtolnay/quote" }
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644
index 0000000..16fe87b
--- /dev/null
+++ b/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..40b8817
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,25 @@
+Copyright (c) 2016 The Rust Project Developers
+
+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/README.md b/README.md
new file mode 100644
index 0000000..8a5d590
--- /dev/null
+++ b/README.md
@@ -0,0 +1,243 @@
+Rust Quasi-Quoting
+==================
+
+[![Build Status](https://api.travis-ci.org/dtolnay/quote.svg?branch=master)](https://travis-ci.org/dtolnay/quote)
+[![Latest Version](https://img.shields.io/crates/v/quote.svg)](https://crates.io/crates/quote)
+[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/quote/)
+
+This crate provides the [`quote!`] macro for turning Rust syntax tree data
+structures into tokens of source code.
+
+[`quote!`]: https://docs.rs/quote/0.6/quote/macro.quote.html
+
+Procedural macros in Rust receive a stream of tokens as input, execute arbitrary
+Rust code to determine how to manipulate those tokens, and produce a stream of
+tokens to hand back to the compiler to compile into the caller's crate.
+Quasi-quoting is a solution to one piece of that -- producing tokens to return
+to the compiler.
+
+The idea of quasi-quoting is that we write *code* that we treat as *data*.
+Within the `quote!` macro, we can write what looks like code to our text editor
+or IDE. We get all the benefits of the editor's brace matching, syntax
+highlighting, indentation, and maybe autocompletion. But rather than compiling
+that as code into the current crate, we can treat it as data, pass it around,
+mutate it, and eventually hand it back to the compiler as tokens to compile into
+the macro caller's crate.
+
+This crate is motivated by the procedural macro use case, but is a
+general-purpose Rust quasi-quoting library and is not specific to procedural
+macros.
+
+*Version requirement: Quote supports any compiler version back to Rust's very
+first support for procedural macros in Rust 1.15.0.*
+
+[*Release notes*](https://github.com/dtolnay/quote/releases)
+
+```toml
+[dependencies]
+quote = "0.6"
+```
+
+## Syntax
+
+The quote crate provides a [`quote!`] macro within which you can write Rust code
+that gets packaged into a [`TokenStream`] and can be treated as data. You should
+think of `TokenStream` as representing a fragment of Rust source code.
+
+[`TokenStream`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.TokenStream.html
+
+Within the `quote!` macro, interpolation is done with `#var`. Any type
+implementing the [`quote::ToTokens`] trait can be interpolated. This includes
+most Rust primitive types as well as most of the syntax tree types from [`syn`].
+
+[`quote::ToTokens`]: https://docs.rs/quote/0.6/quote/trait.ToTokens.html
+[`syn`]: https://github.com/dtolnay/syn
+
+```rust
+let tokens = quote! {
+ struct SerializeWith #generics #where_clause {
+ value: &'a #field_ty,
+ phantom: core::marker::PhantomData<#item_ty>,
+ }
+
+ impl #generics serde::Serialize for SerializeWith #generics #where_clause {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ #path(self.value, serializer)
+ }
+ }
+
+ SerializeWith {
+ value: #value,
+ phantom: core::marker::PhantomData::<#item_ty>,
+ }
+};
+```
+
+## Repetition
+
+Repetition is done using `#(...)*` or `#(...),*` similar to `macro_rules!`. This
+iterates through the elements of any variable interpolated within the repetition
+and inserts a copy of the repetition body for each one. The variables in an
+interpolation may be anything that implements `IntoIterator`, including `Vec` or
+a pre-existing iterator.
+
+- `#(#var)*` — no separators
+- `#(#var),*` — the character before the asterisk is used as a separator
+- `#( struct #var; )*` — the repetition can contain other things
+- `#( #k => println!("{}", #v), )*` — even multiple interpolations
+
+Note that there is a difference between `#(#var ,)*` and `#(#var),*`—the latter
+does not produce a trailing comma. This matches the behavior of delimiters in
+`macro_rules!`.
+
+## Returning tokens to the compiler
+
+The `quote!` macro evaluates to an expression of type
+`proc_macro2::TokenStream`. Meanwhile Rust procedural macros are expected to
+return the type `proc_macro::TokenStream`.
+
+The difference between the two types is that `proc_macro` types are entirely
+specific to procedural macros and cannot ever exist in code outside of a
+procedural macro, while `proc_macro2` types may exist anywhere including tests
+and non-macro code like main.rs and build.rs. This is why even the procedural
+macro ecosystem is largely built around `proc_macro2`, because that ensures the
+libraries are unit testable and accessible in non-macro contexts.
+
+There is a [`From`]-conversion in both directions so returning the output of
+`quote!` from a procedural macro usually looks like `tokens.into()` or
+`proc_macro::TokenStream::from(tokens)`.
+
+[`From`]: https://doc.rust-lang.org/std/convert/trait.From.html
+
+## Examples
+
+### Combining quoted fragments
+
+Usually you don't end up constructing an entire final `TokenStream` in one
+piece. Different parts may come from different helper functions. The tokens
+produced by `quote!` themselves implement `ToTokens` and so can be interpolated
+into later `quote!` invocations to build up a final result.
+
+```rust
+let type_definition = quote! {...};
+let methods = quote! {...};
+
+let tokens = quote! {
+ #type_definition
+ #methods
+};
+```
+
+### Constructing identifiers
+
+Suppose we have an identifier `ident` which came from somewhere in a macro
+input and we need to modify it in some way for the macro output. Let's consider
+prepending the identifier with an underscore.
+
+Simply interpolating the identifier next to an underscore will not have the
+behavior of concatenating them. The underscore and the identifier will continue
+to be two separate tokens as if you had written `_ x`.
+
+```rust
+// incorrect
+quote! {
+ let mut _#ident = 0;
+}
+```
+
+The solution is to perform token-level manipulations using the APIs provided by
+Syn and proc-macro2.
+
+```rust
+let concatenated = format!("_{}", ident);
+let varname = syn::Ident::new(&concatenated, ident.span());
+quote! {
+ let mut #varname = 0;
+}
+```
+
+### Making method calls
+
+Let's say our macro requires some type specified in the macro input to have a
+constructor called `new`. We have the type in a variable called `field_type` of
+type `syn::Type` and want to invoke the constructor.
+
+```rust
+// incorrect
+quote! {
+ let value = #field_type::new();
+}
+```
+
+This works only sometimes. If `field_type` is `String`, the expanded code
+contains `String::new()` which is fine. But if `field_type` is something like
+`Vec<i32>` then the expanded code is `Vec<i32>::new()` which is invalid syntax.
+Ordinarily in handwritten Rust we would write `Vec::<i32>::new()` but for macros
+often the following is more convenient.
+
+```rust
+quote! {
+ let value = <#field_type>::new();
+}
+```
+
+This expands to `<Vec<i32>>::new()` which behaves correctly.
+
+A similar pattern is appropriate for trait methods.
+
+```rust
+quote! {
+ let value = <#field_type as core::default::Default>::default();
+}
+```
+
+## Hygiene
+
+Any interpolated tokens preserve the `Span` information provided by their
+`ToTokens` implementation. Tokens that originate within a `quote!` invocation
+are spanned with [`Span::call_site()`].
+
+[`Span::call_site()`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.Span.html#method.call_site
+
+A different span can be provided explicitly through the [`quote_spanned!`]
+macro.
+
+[`quote_spanned!`]: https://docs.rs/quote/0.6/quote/macro.quote_spanned.html
+
+### Limitations
+
+- A non-repeating variable may not be interpolated inside of a repeating block
+ ([#7]).
+- The same variable may not be interpolated more than once inside of a repeating
+ block ([#8]).
+
+[#7]: https://github.com/dtolnay/quote/issues/7
+[#8]: https://github.com/dtolnay/quote/issues/8
+
+### Recursion limit
+
+The `quote!` macro relies on deep recursion so some large invocations may fail
+with "recursion limit reached" when you compile. If it fails, bump up the
+recursion limit by adding `#![recursion_limit = "128"]` to your crate. An even
+higher limit may be necessary for especially large invocations. You don't need
+this unless the compiler tells you that you need it.
+
+<br>
+
+#### License
+
+<sup>
+Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
+2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
+</sup>
+
+<br>
+
+<sub>
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
+be dual licensed as above, without any additional terms or conditions.
+</sub>
diff --git a/benches/bench.rs b/benches/bench.rs
new file mode 100644
index 0000000..cdbdb45
--- /dev/null
+++ b/benches/bench.rs
@@ -0,0 +1,194 @@
+#![feature(test)]
+#![recursion_limit = "512"]
+
+extern crate quote;
+extern crate test;
+
+use quote::quote;
+use test::Bencher;
+
+#[bench]
+fn bench_impl(b: &mut Bencher) {
+ b.iter(|| {
+ quote! {
+ impl<'de> _serde::Deserialize<'de> for Response {
+ fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<Self, __D::Error>
+ where
+ __D: _serde::Deserializer<'de>,
+ {
+ #[allow(non_camel_case_types)]
+ enum __Field {
+ __field0,
+ __field1,
+ __ignore,
+ }
+ struct __FieldVisitor;
+ impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
+ type Value = __Field;
+ fn expecting(
+ &self,
+ __formatter: &mut _serde::export::Formatter,
+ ) -> _serde::export::fmt::Result {
+ _serde::export::Formatter::write_str(__formatter, "field identifier")
+ }
+ fn visit_u64<__E>(self, __value: u64) -> _serde::export::Result<Self::Value, __E>
+ where
+ __E: _serde::de::Error,
+ {
+ match __value {
+ 0u64 => _serde::export::Ok(__Field::__field0),
+ 1u64 => _serde::export::Ok(__Field::__field1),
+ _ => _serde::export::Err(_serde::de::Error::invalid_value(
+ _serde::de::Unexpected::Unsigned(__value),
+ &"field index 0 <= i < 2",
+ )),
+ }
+ }
+ fn visit_str<__E>(self, __value: &str) -> _serde::export::Result<Self::Value, __E>
+ where
+ __E: _serde::de::Error,
+ {
+ match __value {
+ "id" => _serde::export::Ok(__Field::__field0),
+ "s" => _serde::export::Ok(__Field::__field1),
+ _ => _serde::export::Ok(__Field::__ignore),
+ }
+ }
+ fn visit_bytes<__E>(
+ self,
+ __value: &[u8],
+ ) -> _serde::export::Result<Self::Value, __E>
+ where
+ __E: _serde::de::Error,
+ {
+ match __value {
+ b"id" => _serde::export::Ok(__Field::__field0),
+ b"s" => _serde::export::Ok(__Field::__field1),
+ _ => _serde::export::Ok(__Field::__ignore),
+ }
+ }
+ }
+ impl<'de> _serde::Deserialize<'de> for __Field {
+ #[inline]
+ fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<Self, __D::Error>
+ where
+ __D: _serde::Deserializer<'de>,
+ {
+ _serde::Deserializer::deserialize_identifier(__deserializer, __FieldVisitor)
+ }
+ }
+ struct __Visitor<'de> {
+ marker: _serde::export::PhantomData<Response>,
+ lifetime: _serde::export::PhantomData<&'de ()>,
+ }
+ impl<'de> _serde::de::Visitor<'de> for __Visitor<'de> {
+ type Value = Response;
+ fn expecting(
+ &self,
+ __formatter: &mut _serde::export::Formatter,
+ ) -> _serde::export::fmt::Result {
+ _serde::export::Formatter::write_str(__formatter, "struct Response")
+ }
+ #[inline]
+ fn visit_seq<__A>(
+ self,
+ mut __seq: __A,
+ ) -> _serde::export::Result<Self::Value, __A::Error>
+ where
+ __A: _serde::de::SeqAccess<'de>,
+ {
+ let __field0 =
+ match try!(_serde::de::SeqAccess::next_element::<u64>(&mut __seq)) {
+ _serde::export::Some(__value) => __value,
+ _serde::export::None => {
+ return _serde::export::Err(_serde::de::Error::invalid_length(
+ 0usize,
+ &"struct Response with 2 elements",
+ ));
+ }
+ };
+ let __field1 =
+ match try!(_serde::de::SeqAccess::next_element::<String>(&mut __seq)) {
+ _serde::export::Some(__value) => __value,
+ _serde::export::None => {
+ return _serde::export::Err(_serde::de::Error::invalid_length(
+ 1usize,
+ &"struct Response with 2 elements",
+ ));
+ }
+ };
+ _serde::export::Ok(Response {
+ id: __field0,
+ s: __field1,
+ })
+ }
+ #[inline]
+ fn visit_map<__A>(
+ self,
+ mut __map: __A,
+ ) -> _serde::export::Result<Self::Value, __A::Error>
+ where
+ __A: _serde::de::MapAccess<'de>,
+ {
+ let mut __field0: _serde::export::Option<u64> = _serde::export::None;
+ let mut __field1: _serde::export::Option<String> = _serde::export::None;
+ while let _serde::export::Some(__key) =
+ try!(_serde::de::MapAccess::next_key::<__Field>(&mut __map))
+ {
+ match __key {
+ __Field::__field0 => {
+ if _serde::export::Option::is_some(&__field0) {
+ return _serde::export::Err(
+ <__A::Error as _serde::de::Error>::duplicate_field("id"),
+ );
+ }
+ __field0 = _serde::export::Some(
+ try!(_serde::de::MapAccess::next_value::<u64>(&mut __map)),
+ );
+ }
+ __Field::__field1 => {
+ if _serde::export::Option::is_some(&__field1) {
+ return _serde::export::Err(
+ <__A::Error as _serde::de::Error>::duplicate_field("s"),
+ );
+ }
+ __field1 = _serde::export::Some(
+ try!(_serde::de::MapAccess::next_value::<String>(&mut __map)),
+ );
+ }
+ _ => {
+ let _ = try!(_serde::de::MapAccess::next_value::<
+ _serde::de::IgnoredAny,
+ >(&mut __map));
+ }
+ }
+ }
+ let __field0 = match __field0 {
+ _serde::export::Some(__field0) => __field0,
+ _serde::export::None => try!(_serde::private::de::missing_field("id")),
+ };
+ let __field1 = match __field1 {
+ _serde::export::Some(__field1) => __field1,
+ _serde::export::None => try!(_serde::private::de::missing_field("s")),
+ };
+ _serde::export::Ok(Response {
+ id: __field0,
+ s: __field1,
+ })
+ }
+ }
+ const FIELDS: &'static [&'static str] = &["id", "s"];
+ _serde::Deserializer::deserialize_struct(
+ __deserializer,
+ "Response",
+ FIELDS,
+ __Visitor {
+ marker: _serde::export::PhantomData::<Response>,
+ lifetime: _serde::export::PhantomData,
+ },
+ )
+ }
+ }
+ }
+ });
+}
diff --git a/build.rs b/build.rs
new file mode 100644
index 0000000..a0474a4
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,48 @@
+use std::env;
+use std::process::Command;
+use std::str::{self, FromStr};
+
+// The rustc-cfg strings below are *not* public API. Please let us know by
+// opening a GitHub issue if your build environment requires some way to enable
+// these cfgs other than by executing our build script.
+fn main() {
+ let minor = match rustc_minor_version() {
+ Some(minor) => minor,
+ None => return,
+ };
+
+ // 128-bit integers stabilized in Rust 1.26:
+ // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
+ if minor >= 26 {
+ println!("cargo:rustc-cfg=integer128");
+ }
+}
+
+fn rustc_minor_version() -> Option<u32> {
+ let rustc = match env::var_os("RUSTC") {
+ Some(rustc) => rustc,
+ None => return None,
+ };
+
+ let output = match Command::new(rustc).arg("--version").output() {
+ Ok(output) => output,
+ Err(_) => return None,
+ };
+
+ let version = match str::from_utf8(&output.stdout) {
+ Ok(version) => version,
+ Err(_) => return None,
+ };
+
+ let mut pieces = version.split('.');
+ if pieces.next() != Some("rustc 1") {
+ return None;
+ }
+
+ let next = match pieces.next() {
+ Some(next) => next,
+ None => return None,
+ };
+
+ u32::from_str(next).ok()
+}
diff --git a/src/ext.rs b/src/ext.rs
new file mode 100644
index 0000000..7ebbe30
--- /dev/null
+++ b/src/ext.rs
@@ -0,0 +1,112 @@
+use super::ToTokens;
+
+use std::iter;
+
+use proc_macro2::{TokenStream, TokenTree};
+
+/// TokenStream extension trait with methods for appending tokens.
+///
+/// This trait is sealed and cannot be implemented outside of the `quote` crate.
+pub trait TokenStreamExt: private::Sealed {
+ /// For use by `ToTokens` implementations.
+ ///
+ /// Appends the token specified to this list of tokens.
+ fn append<U>(&mut self, token: U)
+ where
+ U: Into<TokenTree>;
+
+ /// For use by `ToTokens` implementations.
+ ///
+ /// ```edition2018
+ /// # use quote::{quote, TokenStreamExt, ToTokens};
+ /// # use proc_macro2::TokenStream;
+ /// #
+ /// struct X;
+ ///
+ /// impl ToTokens for X {
+ /// fn to_tokens(&self, tokens: &mut TokenStream) {
+ /// tokens.append_all(&[true, false]);
+ /// }
+ /// }
+ ///
+ /// let tokens = quote!(#X);
+ /// assert_eq!(tokens.to_string(), "true false");
+ /// ```
+ fn append_all<T, I>(&mut self, iter: I)
+ where
+ T: ToTokens,
+ I: IntoIterator<Item = T>;
+
+ /// For use by `ToTokens` implementations.
+ ///
+ /// Appends all of the items in the iterator `I`, separated by the tokens
+ /// `U`.
+ fn append_separated<T, I, U>(&mut self, iter: I, op: U)
+ where
+ T: ToTokens,
+ I: IntoIterator<Item = T>,
+ U: ToTokens;
+
+ /// For use by `ToTokens` implementations.
+ ///
+ /// Appends all tokens in the iterator `I`, appending `U` after each
+ /// element, including after the last element of the iterator.
+ fn append_terminated<T, I, U>(&mut self, iter: I, term: U)
+ where
+ T: ToTokens,
+ I: IntoIterator<Item = T>,
+ U: ToTokens;
+}
+
+impl TokenStreamExt for TokenStream {
+ fn append<U>(&mut self, token: U)
+ where
+ U: Into<TokenTree>,
+ {
+ self.extend(iter::once(token.into()));
+ }
+
+ fn append_all<T, I>(&mut self, iter: I)
+ where
+ T: ToTokens,
+ I: IntoIterator<Item = T>,
+ {
+ for token in iter {
+ token.to_tokens(self);
+ }
+ }
+
+ fn append_separated<T, I, U>(&mut self, iter: I, op: U)
+ where
+ T: ToTokens,
+ I: IntoIterator<Item = T>,
+ U: ToTokens,
+ {
+ for (i, token) in iter.into_iter().enumerate() {
+ if i > 0 {
+ op.to_tokens(self);
+ }
+ token.to_tokens(self);
+ }
+ }
+
+ fn append_terminated<T, I, U>(&mut self, iter: I, term: U)
+ where
+ T: ToTokens,
+ I: IntoIterator<Item = T>,
+ U: ToTokens,
+ {
+ for token in iter {
+ token.to_tokens(self);
+ term.to_tokens(self);
+ }
+ }
+}
+
+mod private {
+ use proc_macro2::TokenStream;
+
+ pub trait Sealed {}
+
+ impl Sealed for TokenStream {}
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..c201d60
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,861 @@
+//! This crate provides the [`quote!`] macro for turning Rust syntax tree data
+//! structures into tokens of source code.
+//!
+//! [`quote!`]: macro.quote.html
+//!
+//! Procedural macros in Rust receive a stream of tokens as input, execute
+//! arbitrary Rust code to determine how to manipulate those tokens, and produce
+//! a stream of tokens to hand back to the compiler to compile into the caller's
+//! crate. Quasi-quoting is a solution to one piece of that -- producing tokens
+//! to return to the compiler.
+//!
+//! The idea of quasi-quoting is that we write *code* that we treat as *data*.
+//! Within the `quote!` macro, we can write what looks like code to our text
+//! editor or IDE. We get all the benefits of the editor's brace matching,
+//! syntax highlighting, indentation, and maybe autocompletion. But rather than
+//! compiling that as code into the current crate, we can treat it as data, pass
+//! it around, mutate it, and eventually hand it back to the compiler as tokens
+//! to compile into the macro caller's crate.
+//!
+//! This crate is motivated by the procedural macro use case, but is a
+//! general-purpose Rust quasi-quoting library and is not specific to procedural
+//! macros.
+//!
+//! *Version requirement: Quote supports any compiler version back to Rust's
+//! very first support for procedural macros in Rust 1.15.0.*
+//!
+//! ```toml
+//! [dependencies]
+//! quote = "0.6"
+//! ```
+//!
+//! # Example
+//!
+//! The following quasi-quoted block of code is something you might find in [a]
+//! procedural macro having to do with data structure serialization. The `#var`
+//! syntax performs interpolation of runtime variables into the quoted tokens.
+//! Check out the documentation of the [`quote!`] macro for more detail about
+//! the syntax. See also the [`quote_spanned!`] macro which is important for
+//! implementing hygienic procedural macros.
+//!
+//! [a]: https://serde.rs/
+//! [`quote_spanned!`]: macro.quote_spanned.html
+//!
+//! ```edition2018
+//! # use quote::quote;
+//! #
+//! # let generics = "";
+//! # let where_clause = "";
+//! # let field_ty = "";
+//! # let item_ty = "";
+//! # let path = "";
+//! # let value = "";
+//! #
+//! let tokens = quote! {
+//! struct SerializeWith #generics #where_clause {
+//! value: &'a #field_ty,
+//! phantom: core::marker::PhantomData<#item_ty>,
+//! }
+//!
+//! impl #generics serde::Serialize for SerializeWith #generics #where_clause {
+//! fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+//! where
+//! S: serde::Serializer,
+//! {
+//! #path(self.value, serializer)
+//! }
+//! }
+//!
+//! SerializeWith {
+//! value: #value,
+//! phantom: core::marker::PhantomData::<#item_ty>,
+//! }
+//! };
+//! ```
+//!
+//! # Recursion limit
+//!
+//! The `quote!` macro relies on deep recursion so some large invocations may
+//! fail with "recursion limit reached" when you compile. If it fails, bump up
+//! the recursion limit by adding `#![recursion_limit = "128"]` to your crate.
+//! An even higher limit may be necessary for especially large invocations.
+
+// Quote types in rustdoc of other crates get linked to here.
+#![doc(html_root_url = "https://docs.rs/quote/0.6.13")]
+
+#[cfg(all(
+ not(all(target_arch = "wasm32", target_os = "unknown")),
+ feature = "proc-macro"
+))]
+extern crate proc_macro;
+extern crate proc_macro2;
+
+mod ext;
+pub use ext::TokenStreamExt;
+
+mod to_tokens;
+pub use to_tokens::ToTokens;
+
+// Not public API.
+#[doc(hidden)]
+#[path = "runtime.rs"]
+pub mod __rt;
+
+/// The whole point.
+///
+/// Performs variable interpolation against the input and produces it as
+/// [`TokenStream`]. For returning tokens to the compiler in a procedural macro, use
+/// `into()` to build a `TokenStream`.
+///
+/// [`TokenStream`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.TokenStream.html
+///
+/// # Interpolation
+///
+/// Variable interpolation is done with `#var` (similar to `$var` in
+/// `macro_rules!` macros). This grabs the `var` variable that is currently in
+/// scope and inserts it in that location in the output tokens. Any type
+/// implementing the [`ToTokens`] trait can be interpolated. This includes most
+/// Rust primitive types as well as most of the syntax tree types from the [Syn]
+/// crate.
+///
+/// [`ToTokens`]: trait.ToTokens.html
+/// [Syn]: https://github.com/dtolnay/syn
+///
+/// Repetition is done using `#(...)*` or `#(...),*` again similar to
+/// `macro_rules!`. This iterates through the elements of any variable
+/// interpolated within the repetition and inserts a copy of the repetition body
+/// for each one. The variables in an interpolation may be anything that
+/// implements `IntoIterator`, including `Vec` or a pre-existing iterator.
+///
+/// - `#(#var)*` — no separators
+/// - `#(#var),*` — the character before the asterisk is used as a separator
+/// - `#( struct #var; )*` — the repetition can contain other tokens
+/// - `#( #k => println!("{}", #v), )*` — even multiple interpolations
+///
+/// There are two limitations around interpolations in a repetition:
+///
+/// - Every interpolation inside of a repetition must be a distinct variable.
+/// That is, `#(#a #a)*` is not allowed. Work around this by collecting `a`
+/// into a vector and taking references `a1 = &a` and `a2 = &a` which you use
+/// inside the repetition: `#(#a1 #a2)*`. Where possible, use meaningful names
+/// that indicate the distinct role of each copy.
+///
+/// - Every interpolation inside of a repetition must be iterable. If we have
+/// `vec` which is a vector and `ident` which is a single identifier,
+/// `#(#ident #vec)*` is not allowed. Work around this by using
+/// `std::iter::repeat(ident)` to produce an iterable that can be used from
+/// within the repetition.
+///
+/// # Hygiene
+///
+/// Any interpolated tokens preserve the `Span` information provided by their
+/// `ToTokens` implementation. Tokens that originate within the `quote!`
+/// invocation are spanned with [`Span::call_site()`].
+///
+/// [`Span::call_site()`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.Span.html#method.call_site
+///
+/// A different span can be provided through the [`quote_spanned!`] macro.
+///
+/// [`quote_spanned!`]: macro.quote_spanned.html
+///
+/// # Return type
+///
+/// The macro evaluates to an expression of type `proc_macro2::TokenStream`.
+/// Meanwhile Rust procedural macros are expected to return the type
+/// `proc_macro::TokenStream`.
+///
+/// The difference between the two types is that `proc_macro` types are entirely
+/// specific to procedural macros and cannot ever exist in code outside of a
+/// procedural macro, while `proc_macro2` types may exist anywhere including
+/// tests and non-macro code like main.rs and build.rs. This is why even the
+/// procedural macro ecosystem is largely built around `proc_macro2`, because
+/// that ensures the libraries are unit testable and accessible in non-macro
+/// contexts.
+///
+/// There is a [`From`]-conversion in both directions so returning the output of
+/// `quote!` from a procedural macro usually looks like `tokens.into()` or
+/// `proc_macro::TokenStream::from(tokens)`.
+///
+/// [`From`]: https://doc.rust-lang.org/std/convert/trait.From.html
+///
+/// # Examples
+///
+/// ## Procedural macro
+///
+/// The structure of a basic procedural macro is as follows. Refer to the [Syn]
+/// crate for further useful guidance on using `quote!` as part of a procedural
+/// macro.
+///
+/// [Syn]: https://github.com/dtolnay/syn
+///
+/// ```edition2018
+/// # #[cfg(any())]
+/// extern crate proc_macro;
+/// # use proc_macro2 as proc_macro;
+///
+/// use proc_macro::TokenStream;
+/// use quote::quote;
+///
+/// # const IGNORE_TOKENS: &'static str = stringify! {
+/// #[proc_macro_derive(HeapSize)]
+/// # };
+/// pub fn derive_heap_size(input: TokenStream) -> TokenStream {
+/// // Parse the input and figure out what implementation to generate...
+/// # const IGNORE_TOKENS: &'static str = stringify! {
+/// let name = /* ... */;
+/// let expr = /* ... */;
+/// # };
+/// #
+/// # let name = 0;
+/// # let expr = 0;
+///
+/// let expanded = quote! {
+/// // The generated impl.
+/// impl heapsize::HeapSize for #name {
+/// fn heap_size_of_children(&self) -> usize {
+/// #expr
+/// }
+/// }
+/// };
+///
+/// // Hand the output tokens back to the compiler.
+/// TokenStream::from(expanded)
+/// }
+/// ```
+///
+/// ## Combining quoted fragments
+///
+/// Usually you don't end up constructing an entire final `TokenStream` in one
+/// piece. Different parts may come from different helper functions. The tokens
+/// produced by `quote!` themselves implement `ToTokens` and so can be
+/// interpolated into later `quote!` invocations to build up a final result.
+///
+/// ```edition2018
+/// # use quote::quote;
+/// #
+/// let type_definition = quote! {...};
+/// let methods = quote! {...};
+///
+/// let tokens = quote! {
+/// #type_definition
+/// #methods
+/// };
+/// ```
+///
+/// ## Constructing identifiers
+///
+/// Suppose we have an identifier `ident` which came from somewhere in a macro
+/// input and we need to modify it in some way for the macro output. Let's
+/// consider prepending the identifier with an underscore.
+///
+/// Simply interpolating the identifier next to an underscore will not have the
+/// behavior of concatenating them. The underscore and the identifier will
+/// continue to be two separate tokens as if you had written `_ x`.
+///
+/// ```edition2018
+/// # use proc_macro2::{self as syn, Span};
+/// # use quote::quote;
+/// #
+/// # let ident = syn::Ident::new("i", Span::call_site());
+/// #
+/// // incorrect
+/// quote! {
+/// let mut _#ident = 0;
+/// }
+/// # ;
+/// ```
+///
+/// The solution is to perform token-level manipulations using the APIs provided
+/// by Syn and proc-macro2.
+///
+/// ```edition2018
+/// # use proc_macro2::{self as syn, Span};
+/// # use quote::quote;
+/// #
+/// # let ident = syn::Ident::new("i", Span::call_site());
+/// #
+/// let concatenated = format!("_{}", ident);
+/// let varname = syn::Ident::new(&concatenated, ident.span());
+/// quote! {
+/// let mut #varname = 0;
+/// }
+/// # ;
+/// ```
+///
+/// ## Making method calls
+///
+/// Let's say our macro requires some type specified in the macro input to have
+/// a constructor called `new`. We have the type in a variable called
+/// `field_type` of type `syn::Type` and want to invoke the constructor.
+///
+/// ```edition2018
+/// # use quote::quote;
+/// #
+/// # let field_type = quote!(...);
+/// #
+/// // incorrect
+/// quote! {
+/// let value = #field_type::new();
+/// }
+/// # ;
+/// ```
+///
+/// This works only sometimes. If `field_type` is `String`, the expanded code
+/// contains `String::new()` which is fine. But if `field_type` is something
+/// like `Vec<i32>` then the expanded code is `Vec<i32>::new()` which is invalid
+/// syntax. Ordinarily in handwritten Rust we would write `Vec::<i32>::new()`
+/// but for macros often the following is more convenient.
+///
+/// ```edition2018
+/// # use quote::quote;
+/// #
+/// # let field_type = quote!(...);
+/// #
+/// quote! {
+/// let value = <#field_type>::new();
+/// }
+/// # ;
+/// ```
+///
+/// This expands to `<Vec<i32>>::new()` which behaves correctly.
+///
+/// A similar pattern is appropriate for trait methods.
+///
+/// ```edition2018
+/// # use quote::quote;
+/// #
+/// # let field_type = quote!(...);
+/// #
+/// quote! {
+/// let value = <#field_type as core::default::Default>::default();
+/// }
+/// # ;
+/// ```
+#[macro_export(local_inner_macros)]
+macro_rules! quote {
+ ($($tt:tt)*) => {
+ quote_spanned!($crate::__rt::Span::call_site()=> $($tt)*)
+ };
+}
+
+/// Same as `quote!`, but applies a given span to all tokens originating within
+/// the macro invocation.
+///
+/// # Syntax
+///
+/// A span expression of type [`Span`], followed by `=>`, followed by the tokens
+/// to quote. The span expression should be brief -- use a variable for anything
+/// more than a few characters. There should be no space before the `=>` token.
+///
+/// [`Span`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.Span.html
+///
+/// ```edition2018
+/// # use proc_macro2::Span;
+/// # use quote::quote_spanned;
+/// #
+/// # const IGNORE_TOKENS: &'static str = stringify! {
+/// let span = /* ... */;
+/// # };
+/// # let span = Span::call_site();
+/// # let init = 0;
+///
+/// // On one line, use parentheses.
+/// let tokens = quote_spanned!(span=> Box::into_raw(Box::new(#init)));
+///
+/// // On multiple lines, place the span at the top and use braces.
+/// let tokens = quote_spanned! {span=>
+/// Box::into_raw(Box::new(#init))
+/// };
+/// ```
+///
+/// The lack of space before the `=>` should look jarring to Rust programmers
+/// and this is intentional. The formatting is designed to be visibly
+/// off-balance and draw the eye a particular way, due to the span expression
+/// being evaluated in the context of the procedural macro and the remaining
+/// tokens being evaluated in the generated code.
+///
+/// # Hygiene
+///
+/// Any interpolated tokens preserve the `Span` information provided by their
+/// `ToTokens` implementation. Tokens that originate within the `quote_spanned!`
+/// invocation are spanned with the given span argument.
+///
+/// # Example
+///
+/// The following procedural macro code uses `quote_spanned!` to assert that a
+/// particular Rust type implements the [`Sync`] trait so that references can be
+/// safely shared between threads.
+///
+/// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
+///
+/// ```edition2018
+/// # use quote::{quote_spanned, TokenStreamExt, ToTokens};
+/// # use proc_macro2::{Span, TokenStream};
+/// #
+/// # struct Type;
+/// #
+/// # impl Type {
+/// # fn span(&self) -> Span {
+/// # Span::call_site()
+/// # }
+/// # }
+/// #
+/// # impl ToTokens for Type {
+/// # fn to_tokens(&self, _tokens: &mut TokenStream) {}
+/// # }
+/// #
+/// # let ty = Type;
+/// # let call_site = Span::call_site();
+/// #
+/// let ty_span = ty.span();
+/// let assert_sync = quote_spanned! {ty_span=>
+/// struct _AssertSync where #ty: Sync;
+/// };
+/// ```
+///
+/// If the assertion fails, the user will see an error like the following. The
+/// input span of their type is hightlighted in the error.
+///
+/// ```text
+/// error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied
+/// --> src/main.rs:10:21
+/// |
+/// 10 | static ref PTR: *const () = &();
+/// | ^^^^^^^^^ `*const ()` cannot be shared between threads safely
+/// ```
+///
+/// In this example it is important for the where-clause to be spanned with the
+/// line/column information of the user's input type so that error messages are
+/// placed appropriately by the compiler. But it is also incredibly important
+/// that `Sync` resolves at the macro definition site and not the macro call
+/// site. If we resolve `Sync` at the same span that the user's type is going to
+/// be resolved, then they could bypass our check by defining their own trait
+/// named `Sync` that is implemented for their type.
+#[macro_export(local_inner_macros)]
+macro_rules! quote_spanned {
+ ($span:expr=> $($tt:tt)*) => {{
+ let mut _s = $crate::__rt::TokenStream::new();
+ let _span = $span;
+ quote_each_token!(_s _span $($tt)*);
+ _s
+ }};
+}
+
+// Extract the names of all #metavariables and pass them to the $finish macro.
+//
+// in: pounded_var_names!(then () a #b c #( #d )* #e)
+// out: then!(() b d e)
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! pounded_var_names {
+ ($finish:ident ($($found:ident)*) # ( $($inner:tt)* ) $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) # [ $($inner:tt)* ] $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) # { $($inner:tt)* } $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) # $first:ident $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)* $first) $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) ( $($inner:tt)* ) $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) [ $($inner:tt)* ] $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) { $($inner:tt)* } $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*) $ignore:tt $($rest:tt)*) => {
+ pounded_var_names!($finish ($($found)*) $($rest)*)
+ };
+
+ ($finish:ident ($($found:ident)*)) => {
+ $finish!(() $($found)*)
+ };
+}
+
+// in: nested_tuples_pat!(() a b c d e)
+// out: ((((a b) c) d) e)
+//
+// in: nested_tuples_pat!(() a)
+// out: a
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! nested_tuples_pat {
+ (()) => {
+ &()
+ };
+
+ (() $first:ident $($rest:ident)*) => {
+ nested_tuples_pat!(($first) $($rest)*)
+ };
+
+ (($pat:pat) $first:ident $($rest:ident)*) => {
+ nested_tuples_pat!((($pat, $first)) $($rest)*)
+ };
+
+ (($done:pat)) => {
+ $done
+ };
+}
+
+// in: multi_zip_expr!(() a b c d e)
+// out: a.into_iter().zip(b).zip(c).zip(d).zip(e)
+//
+// in: multi_zip_iter!(() a)
+// out: a
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! multi_zip_expr {
+ (()) => {
+ &[]
+ };
+
+ (() $single:ident) => {
+ $single
+ };
+
+ (() $first:ident $($rest:ident)*) => {
+ multi_zip_expr!(($first.into_iter()) $($rest)*)
+ };
+
+ (($zips:expr) $first:ident $($rest:ident)*) => {
+ multi_zip_expr!(($zips.zip($first)) $($rest)*)
+ };
+
+ (($done:expr)) => {
+ $done
+ };
+}
+
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! quote_each_token {
+ ($tokens:ident $span:ident) => {};
+
+ ($tokens:ident $span:ident # ! $($rest:tt)*) => {
+ quote_each_token!($tokens $span #);
+ quote_each_token!($tokens $span !);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident # ( $($inner:tt)* ) * $($rest:tt)*) => {
+ for pounded_var_names!(nested_tuples_pat () $($inner)*)
+ in pounded_var_names!(multi_zip_expr () $($inner)*) {
+ quote_each_token!($tokens $span $($inner)*);
+ }
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident # ( $($inner:tt)* ) $sep:tt * $($rest:tt)*) => {
+ for (_i, pounded_var_names!(nested_tuples_pat () $($inner)*))
+ in pounded_var_names!(multi_zip_expr () $($inner)*).into_iter().enumerate() {
+ if _i > 0 {
+ quote_each_token!($tokens $span $sep);
+ }
+ quote_each_token!($tokens $span $($inner)*);
+ }
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident # [ $($inner:tt)* ] $($rest:tt)*) => {
+ quote_each_token!($tokens $span #);
+ $tokens.extend({
+ let mut g = $crate::__rt::Group::new(
+ $crate::__rt::Delimiter::Bracket,
+ quote_spanned!($span=> $($inner)*),
+ );
+ g.set_span($span);
+ Some($crate::__rt::TokenTree::from(g))
+ });
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident # $first:ident $($rest:tt)*) => {
+ $crate::ToTokens::to_tokens(&$first, &mut $tokens);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ( $($first:tt)* ) $($rest:tt)*) => {
+ $tokens.extend({
+ let mut g = $crate::__rt::Group::new(
+ $crate::__rt::Delimiter::Parenthesis,
+ quote_spanned!($span=> $($first)*),
+ );
+ g.set_span($span);
+ Some($crate::__rt::TokenTree::from(g))
+ });
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident [ $($first:tt)* ] $($rest:tt)*) => {
+ $tokens.extend({
+ let mut g = $crate::__rt::Group::new(
+ $crate::__rt::Delimiter::Bracket,
+ quote_spanned!($span=> $($first)*),
+ );
+ g.set_span($span);
+ Some($crate::__rt::TokenTree::from(g))
+ });
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident { $($first:tt)* } $($rest:tt)*) => {
+ $tokens.extend({
+ let mut g = $crate::__rt::Group::new(
+ $crate::__rt::Delimiter::Brace,
+ quote_spanned!($span=> $($first)*),
+ );
+ g.set_span($span);
+ Some($crate::__rt::TokenTree::from(g))
+ });
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident + $($rest:tt)*) => {
+ $crate::__rt::push_add(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident += $($rest:tt)*) => {
+ $crate::__rt::push_add_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident & $($rest:tt)*) => {
+ $crate::__rt::push_and(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident && $($rest:tt)*) => {
+ $crate::__rt::push_and_and(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident &= $($rest:tt)*) => {
+ $crate::__rt::push_and_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident @ $($rest:tt)*) => {
+ $crate::__rt::push_at(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ! $($rest:tt)*) => {
+ $crate::__rt::push_bang(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ^ $($rest:tt)*) => {
+ $crate::__rt::push_caret(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ^= $($rest:tt)*) => {
+ $crate::__rt::push_caret_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident : $($rest:tt)*) => {
+ $crate::__rt::push_colon(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident :: $($rest:tt)*) => {
+ $crate::__rt::push_colon2(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident , $($rest:tt)*) => {
+ $crate::__rt::push_comma(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident / $($rest:tt)*) => {
+ $crate::__rt::push_div(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident /= $($rest:tt)*) => {
+ $crate::__rt::push_div_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident . $($rest:tt)*) => {
+ $crate::__rt::push_dot(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident .. $($rest:tt)*) => {
+ $crate::__rt::push_dot2(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ... $($rest:tt)*) => {
+ $crate::__rt::push_dot3(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ..= $($rest:tt)*) => {
+ $crate::__rt::push_dot_dot_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident = $($rest:tt)*) => {
+ $crate::__rt::push_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident == $($rest:tt)*) => {
+ $crate::__rt::push_eq_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident >= $($rest:tt)*) => {
+ $crate::__rt::push_ge(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident > $($rest:tt)*) => {
+ $crate::__rt::push_gt(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident <= $($rest:tt)*) => {
+ $crate::__rt::push_le(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident < $($rest:tt)*) => {
+ $crate::__rt::push_lt(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident *= $($rest:tt)*) => {
+ $crate::__rt::push_mul_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident != $($rest:tt)*) => {
+ $crate::__rt::push_ne(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident | $($rest:tt)*) => {
+ $crate::__rt::push_or(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident |= $($rest:tt)*) => {
+ $crate::__rt::push_or_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident || $($rest:tt)*) => {
+ $crate::__rt::push_or_or(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident # $($rest:tt)*) => {
+ $crate::__rt::push_pound(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ? $($rest:tt)*) => {
+ $crate::__rt::push_question(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident -> $($rest:tt)*) => {
+ $crate::__rt::push_rarrow(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident <- $($rest:tt)*) => {
+ $crate::__rt::push_larrow(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident % $($rest:tt)*) => {
+ $crate::__rt::push_rem(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident %= $($rest:tt)*) => {
+ $crate::__rt::push_rem_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident => $($rest:tt)*) => {
+ $crate::__rt::push_fat_arrow(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident ; $($rest:tt)*) => {
+ $crate::__rt::push_semi(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident << $($rest:tt)*) => {
+ $crate::__rt::push_shl(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident <<= $($rest:tt)*) => {
+ $crate::__rt::push_shl_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident >> $($rest:tt)*) => {
+ $crate::__rt::push_shr(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident >>= $($rest:tt)*) => {
+ $crate::__rt::push_shr_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident * $($rest:tt)*) => {
+ $crate::__rt::push_star(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident - $($rest:tt)*) => {
+ $crate::__rt::push_sub(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident -= $($rest:tt)*) => {
+ $crate::__rt::push_sub_eq(&mut $tokens, $span);
+ quote_each_token!($tokens $span $($rest)*);
+ };
+
+ ($tokens:ident $span:ident $first:tt $($rest:tt)*) => {
+ $crate::__rt::parse(&mut $tokens, $span, quote_stringify!($first));
+ quote_each_token!($tokens $span $($rest)*);
+ };
+}
+
+// Unhygienically invoke whatever `stringify` the caller has in scope i.e. not a
+// local macro. The macros marked `local_inner_macros` above cannot invoke
+// `stringify` directly.
+#[macro_export]
+#[doc(hidden)]
+macro_rules! quote_stringify {
+ ($tt:tt) => {
+ stringify!($tt)
+ };
+}
diff --git a/src/runtime.rs b/src/runtime.rs
new file mode 100644
index 0000000..75337c9
--- /dev/null
+++ b/src/runtime.rs
@@ -0,0 +1,108 @@
+use ext::TokenStreamExt;
+pub use proc_macro2::*;
+
+fn is_ident_start(c: u8) -> bool {
+ (b'a' <= c && c <= b'z') || (b'A' <= c && c <= b'Z') || c == b'_'
+}
+
+fn is_ident_continue(c: u8) -> bool {
+ (b'a' <= c && c <= b'z') || (b'A' <= c && c <= b'Z') || c == b'_' || (b'0' <= c && c <= b'9')
+}
+
+fn is_ident(token: &str) -> bool {
+ let mut iter = token.bytes();
+ let first_ok = iter.next().map(is_ident_start).unwrap_or(false);
+
+ first_ok && iter.all(is_ident_continue)
+}
+
+pub fn parse(tokens: &mut TokenStream, span: Span, s: &str) {
+ if is_ident(s) {
+ // Fast path, since idents are the most common token.
+ tokens.append(Ident::new(s, span));
+ } else {
+ let s: TokenStream = s.parse().expect("invalid token stream");
+ tokens.extend(s.into_iter().map(|mut t| {
+ t.set_span(span);
+ t
+ }));
+ }
+}
+
+macro_rules! push_punct {
+ ($name:ident $char1:tt) => {
+ pub fn $name(tokens: &mut TokenStream, span: Span) {
+ let mut punct = Punct::new($char1, Spacing::Alone);
+ punct.set_span(span);
+ tokens.append(punct);
+ }
+ };
+ ($name:ident $char1:tt $char2:tt) => {
+ pub fn $name(tokens: &mut TokenStream, span: Span) {
+ let mut punct = Punct::new($char1, Spacing::Joint);
+ punct.set_span(span);
+ tokens.append(punct);
+ let mut punct = Punct::new($char2, Spacing::Alone);
+ punct.set_span(span);
+ tokens.append(punct);
+ }
+ };
+ ($name:ident $char1:tt $char2:tt $char3:tt) => {
+ pub fn $name(tokens: &mut TokenStream, span: Span) {
+ let mut punct = Punct::new($char1, Spacing::Joint);
+ punct.set_span(span);
+ tokens.append(punct);
+ let mut punct = Punct::new($char2, Spacing::Joint);
+ punct.set_span(span);
+ tokens.append(punct);
+ let mut punct = Punct::new($char3, Spacing::Alone);
+ punct.set_span(span);
+ tokens.append(punct);
+ }
+ };
+}
+
+push_punct!(push_add '+');
+push_punct!(push_add_eq '+' '=');
+push_punct!(push_and '&');
+push_punct!(push_and_and '&' '&');
+push_punct!(push_and_eq '&' '=');
+push_punct!(push_at '@');
+push_punct!(push_bang '!');
+push_punct!(push_caret '^');
+push_punct!(push_caret_eq '^' '=');
+push_punct!(push_colon ':');
+push_punct!(push_colon2 ':' ':');
+push_punct!(push_comma ',');
+push_punct!(push_div '/');
+push_punct!(push_div_eq '/' '=');
+push_punct!(push_dot '.');
+push_punct!(push_dot2 '.' '.');
+push_punct!(push_dot3 '.' '.' '.');
+push_punct!(push_dot_dot_eq '.' '.' '=');
+push_punct!(push_eq '=');
+push_punct!(push_eq_eq '=' '=');
+push_punct!(push_ge '>' '=');
+push_punct!(push_gt '>');
+push_punct!(push_le '<' '=');
+push_punct!(push_lt '<');
+push_punct!(push_mul_eq '*' '=');
+push_punct!(push_ne '!' '=');
+push_punct!(push_or '|');
+push_punct!(push_or_eq '|' '=');
+push_punct!(push_or_or '|' '|');
+push_punct!(push_pound '#');
+push_punct!(push_question '?');
+push_punct!(push_rarrow '-' '>');
+push_punct!(push_larrow '<' '-');
+push_punct!(push_rem '%');
+push_punct!(push_rem_eq '%' '=');
+push_punct!(push_fat_arrow '=' '>');
+push_punct!(push_semi ';');
+push_punct!(push_shl '<' '<');
+push_punct!(push_shl_eq '<' '<' '=');
+push_punct!(push_shr '>' '>');
+push_punct!(push_shr_eq '>' '>' '=');
+push_punct!(push_star '*');
+push_punct!(push_sub '-');
+push_punct!(push_sub_eq '-' '=');
diff --git a/src/to_tokens.rs b/src/to_tokens.rs
new file mode 100644
index 0000000..9d221b2
--- /dev/null
+++ b/src/to_tokens.rs
@@ -0,0 +1,205 @@
+use super::TokenStreamExt;
+
+use std::borrow::Cow;
+use std::iter;
+use std::rc::Rc;
+
+use proc_macro2::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
+
+/// Types that can be interpolated inside a [`quote!`] invocation.
+///
+/// [`quote!`]: macro.quote.html
+pub trait ToTokens {
+ /// Write `self` to the given `TokenStream`.
+ ///
+ /// The token append methods provided by the [`TokenStreamExt`] extension
+ /// trait may be useful for implementing `ToTokens`.
+ ///
+ /// [`TokenStreamExt`]: trait.TokenStreamExt.html
+ ///
+ /// # Example
+ ///
+ /// Example implementation for a struct representing Rust paths like
+ /// `std::cmp::PartialEq`:
+ ///
+ /// ```edition2018
+ /// use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream};
+ /// use quote::{TokenStreamExt, ToTokens};
+ ///
+ /// pub struct Path {
+ /// pub global: bool,
+ /// pub segments: Vec<PathSegment>,
+ /// }
+ ///
+ /// impl ToTokens for Path {
+ /// fn to_tokens(&self, tokens: &mut TokenStream) {
+ /// for (i, segment) in self.segments.iter().enumerate() {
+ /// if i > 0 || self.global {
+ /// // Double colon `::`
+ /// tokens.append(Punct::new(':', Spacing::Joint));
+ /// tokens.append(Punct::new(':', Spacing::Alone));
+ /// }
+ /// segment.to_tokens(tokens);
+ /// }
+ /// }
+ /// }
+ /// #
+ /// # pub struct PathSegment;
+ /// #
+ /// # impl ToTokens for PathSegment {
+ /// # fn to_tokens(&self, tokens: &mut TokenStream) {
+ /// # unimplemented!()
+ /// # }
+ /// # }
+ /// ```
+ fn to_tokens(&self, tokens: &mut TokenStream);
+
+ /// Convert `self` directly into a `TokenStream` object.
+ ///
+ /// This method is implicitly implemented using `to_tokens`, and acts as a
+ /// convenience method for consumers of the `ToTokens` trait.
+ fn into_token_stream(self) -> TokenStream
+ where
+ Self: Sized,
+ {
+ let mut tokens = TokenStream::new();
+ self.to_tokens(&mut tokens);
+ tokens
+ }
+}
+
+impl<'a, T: ?Sized + ToTokens> ToTokens for &'a T {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ (**self).to_tokens(tokens);
+ }
+}
+
+impl<'a, T: ?Sized + ToTokens> ToTokens for &'a mut T {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ (**self).to_tokens(tokens);
+ }
+}
+
+impl<'a, T: ?Sized + ToOwned + ToTokens> ToTokens for Cow<'a, T> {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ (**self).to_tokens(tokens);
+ }
+}
+
+impl<T: ?Sized + ToTokens> ToTokens for Box<T> {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ (**self).to_tokens(tokens);
+ }
+}
+
+impl<T: ?Sized + ToTokens> ToTokens for Rc<T> {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ (**self).to_tokens(tokens);
+ }
+}
+
+impl<T: ToTokens> ToTokens for Option<T> {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ if let Some(ref t) = *self {
+ t.to_tokens(tokens);
+ }
+ }
+}
+
+impl ToTokens for str {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(Literal::string(self));
+ }
+}
+
+impl ToTokens for String {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ self.as_str().to_tokens(tokens);
+ }
+}
+
+macro_rules! primitive {
+ ($($t:ident => $name:ident)*) => ($(
+ impl ToTokens for $t {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(Literal::$name(*self));
+ }
+ }
+ )*)
+}
+
+primitive! {
+ i8 => i8_suffixed
+ i16 => i16_suffixed
+ i32 => i32_suffixed
+ i64 => i64_suffixed
+ isize => isize_suffixed
+
+ u8 => u8_suffixed
+ u16 => u16_suffixed
+ u32 => u32_suffixed
+ u64 => u64_suffixed
+ usize => usize_suffixed
+
+ f32 => f32_suffixed
+ f64 => f64_suffixed
+}
+
+#[cfg(integer128)]
+primitive! {
+ i128 => i128_suffixed
+ u128 => u128_suffixed
+}
+
+impl ToTokens for char {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(Literal::character(*self));
+ }
+}
+
+impl ToTokens for bool {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ let word = if *self { "true" } else { "false" };
+ tokens.append(Ident::new(word, Span::call_site()));
+ }
+}
+
+impl ToTokens for Group {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(self.clone());
+ }
+}
+
+impl ToTokens for Ident {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(self.clone());
+ }
+}
+
+impl ToTokens for Punct {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(self.clone());
+ }
+}
+
+impl ToTokens for Literal {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(self.clone());
+ }
+}
+
+impl ToTokens for TokenTree {
+ fn to_tokens(&self, dst: &mut TokenStream) {
+ dst.append(self.clone());
+ }
+}
+
+impl ToTokens for TokenStream {
+ fn to_tokens(&self, dst: &mut TokenStream) {
+ dst.extend(iter::once(self.clone()));
+ }
+
+ fn into_token_stream(self) -> TokenStream {
+ self
+ }
+}
diff --git a/tests/conditional/integer128.rs b/tests/conditional/integer128.rs
new file mode 100644
index 0000000..61e2274
--- /dev/null
+++ b/tests/conditional/integer128.rs
@@ -0,0 +1,11 @@
+#[test]
+fn test_integer128() {
+ let ii128 = -1i128;
+ let uu128 = 1u128;
+
+ let tokens = quote! {
+ #ii128 #uu128
+ };
+ let expected = "-1i128 1u128";
+ assert_eq!(expected, tokens.to_string());
+}
diff --git a/tests/test.rs b/tests/test.rs
new file mode 100644
index 0000000..85562bf
--- /dev/null
+++ b/tests/test.rs
@@ -0,0 +1,295 @@
+#![cfg_attr(feature = "cargo-clippy", allow(blacklisted_name))]
+
+use std::borrow::Cow;
+
+extern crate proc_macro2;
+#[macro_use]
+extern crate quote;
+
+use proc_macro2::{Ident, Span, TokenStream};
+use quote::TokenStreamExt;
+
+mod conditional {
+ #[cfg(integer128)]
+ mod integer128;
+}
+
+struct X;
+
+impl quote::ToTokens for X {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append(Ident::new("X", Span::call_site()));
+ }
+}
+
+#[test]
+fn test_quote_impl() {
+ let tokens = quote! {
+ impl<'a, T: ToTokens> ToTokens for &'a T {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ (**self).to_tokens(tokens)
+ }
+ }
+ };
+
+ let expected = concat!(
+ "impl < 'a , T : ToTokens > ToTokens for & 'a T { ",
+ "fn to_tokens ( & self , tokens : & mut TokenStream ) { ",
+ "( * * self ) . to_tokens ( tokens ) ",
+ "} ",
+ "}"
+ );
+
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_substitution() {
+ let x = X;
+ let tokens = quote!(#x <#x> (#x) [#x] {#x});
+
+ let expected = "X < X > ( X ) [ X ] { X }";
+
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_iter() {
+ let primes = &[X, X, X, X];
+
+ assert_eq!("X X X X", quote!(#(#primes)*).to_string());
+
+ assert_eq!("X , X , X , X ,", quote!(#(#primes,)*).to_string());
+
+ assert_eq!("X , X , X , X", quote!(#(#primes),*).to_string());
+}
+
+#[test]
+fn test_advanced() {
+ let generics = quote!( <'a, T> );
+
+ let where_clause = quote!( where T: Serialize );
+
+ let field_ty = quote!(String);
+
+ let item_ty = quote!(Cow<'a, str>);
+
+ let path = quote!(SomeTrait::serialize_with);
+
+ let value = quote!(self.x);
+
+ let tokens = quote! {
+ struct SerializeWith #generics #where_clause {
+ value: &'a #field_ty,
+ phantom: ::std::marker::PhantomData<#item_ty>,
+ }
+
+ impl #generics ::serde::Serialize for SerializeWith #generics #where_clause {
+ fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
+ where S: ::serde::Serializer
+ {
+ #path(self.value, s)
+ }
+ }
+
+ SerializeWith {
+ value: #value,
+ phantom: ::std::marker::PhantomData::<#item_ty>,
+ }
+ };
+
+ let expected = concat!(
+ "struct SerializeWith < 'a , T > where T : Serialize { ",
+ "value : & 'a String , ",
+ "phantom : :: std :: marker :: PhantomData < Cow < 'a , str > > , ",
+ "} ",
+ "impl < 'a , T > :: serde :: Serialize for SerializeWith < 'a , T > where T : Serialize { ",
+ "fn serialize < S > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > ",
+ "where S : :: serde :: Serializer ",
+ "{ ",
+ "SomeTrait :: serialize_with ( self . value , s ) ",
+ "} ",
+ "} ",
+ "SerializeWith { ",
+ "value : self . x , ",
+ "phantom : :: std :: marker :: PhantomData :: < Cow < 'a , str > > , ",
+ "}"
+ );
+
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_integer() {
+ let ii8 = -1i8;
+ let ii16 = -1i16;
+ let ii32 = -1i32;
+ let ii64 = -1i64;
+ let iisize = -1isize;
+ let uu8 = 1u8;
+ let uu16 = 1u16;
+ let uu32 = 1u32;
+ let uu64 = 1u64;
+ let uusize = 1usize;
+
+ let tokens = quote! {
+ #ii8 #ii16 #ii32 #ii64 #iisize
+ #uu8 #uu16 #uu32 #uu64 #uusize
+ };
+ let expected = "-1i8 -1i16 -1i32 -1i64 -1isize 1u8 1u16 1u32 1u64 1usize";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_floating() {
+ let e32 = 2.345f32;
+
+ let e64 = 2.345f64;
+
+ let tokens = quote! {
+ #e32
+ #e64
+ };
+ let expected = concat!("2.345f32 2.345f64");
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_char() {
+ let zero = '\0';
+ let pound = '#';
+ let quote = '"';
+ let apost = '\'';
+ let newline = '\n';
+ let heart = '\u{2764}';
+
+ let tokens = quote! {
+ #zero #pound #quote #apost #newline #heart
+ };
+ let expected = "'\\u{0}' '#' '\"' '\\'' '\\n' '\\u{2764}'";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_str() {
+ let s = "\0 a 'b \" c";
+ let tokens = quote!(#s);
+ let expected = "\"\\u{0} a 'b \\\" c\"";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_string() {
+ let s = "\0 a 'b \" c".to_string();
+ let tokens = quote!(#s);
+ let expected = "\"\\u{0} a 'b \\\" c\"";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_ident() {
+ let foo = Ident::new("Foo", Span::call_site());
+ let bar = Ident::new(&format!("Bar{}", 7), Span::call_site());
+ let tokens = quote!(struct #foo; enum #bar {});
+ let expected = "struct Foo ; enum Bar7 { }";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_duplicate() {
+ let ch = 'x';
+
+ let tokens = quote!(#ch #ch);
+
+ let expected = "'x' 'x'";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_fancy_repetition() {
+ let foo = vec!["a", "b"];
+ let bar = vec![true, false];
+
+ let tokens = quote! {
+ #(#foo: #bar),*
+ };
+
+ let expected = r#""a" : true , "b" : false"#;
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_nested_fancy_repetition() {
+ let nested = vec![vec!['a', 'b', 'c'], vec!['x', 'y', 'z']];
+
+ let tokens = quote! {
+ #(
+ #(#nested)*
+ ),*
+ };
+
+ let expected = "'a' 'b' 'c' , 'x' 'y' 'z'";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_empty_repetition() {
+ let tokens = quote!(#(a b)* #(c d),*);
+ assert_eq!("", tokens.to_string());
+}
+
+#[test]
+fn test_variable_name_conflict() {
+ // The implementation of `#(...),*` uses the variable `_i` but it should be
+ // fine, if a little confusing when debugging.
+ let _i = vec!['a', 'b'];
+ let tokens = quote! { #(#_i),* };
+ let expected = "'a' , 'b'";
+ assert_eq!(expected, tokens.to_string());
+}
+
+#[test]
+fn test_empty_quote() {
+ let tokens = quote!();
+ assert_eq!("", tokens.to_string());
+}
+
+#[test]
+fn test_box_str() {
+ let b = "str".to_owned().into_boxed_str();
+ let tokens = quote! { #b };
+ assert_eq!("\"str\"", tokens.to_string());
+}
+
+#[test]
+fn test_cow() {
+ let owned: Cow<Ident> = Cow::Owned(Ident::new("owned", Span::call_site()));
+
+ let ident = Ident::new("borrowed", Span::call_site());
+ let borrowed = Cow::Borrowed(&ident);
+
+ let tokens = quote! { #owned #borrowed };
+ assert_eq!("owned borrowed", tokens.to_string());
+}
+
+#[test]
+fn test_closure() {
+ fn field_i(i: usize) -> Ident {
+ Ident::new(&format!("__field{}", i), Span::call_site())
+ }
+
+ let fields = (0usize..3)
+ .map(field_i as fn(_) -> _)
+ .map(|var| quote! { #var });
+
+ let tokens = quote! { #(#fields)* };
+ assert_eq!("__field0 __field1 __field2", tokens.to_string());
+}
+
+#[test]
+fn test_append_tokens() {
+ let mut a = quote!(a);
+ let b = quote!(b);
+ a.append_all(b);
+ assert_eq!("a b", a.to_string());
+}