proc-quote-0.4.0/.cargo_vcs_info.json0000644000000001120000000000000131240ustar { "git": { "sha1": "5a945a044f1ca9b1c6f23174dad225d4bbc5ee2f" } } proc-quote-0.4.0/.gitignore000064400000000000000000000000660000000000000136720ustar 00000000000000/target /proc-quote-impl/target **/*.rs.bk Cargo.lock proc-quote-0.4.0/.travis.yml000064400000000000000000000004570000000000000140170ustar 00000000000000sudo: false language: rust rust: - stable - 1.31.0 - beta script: - cargo test matrix: include: - env: RUSTFMT rust: 1.31.0 # `stable`: Locking down for consistent behavior install: - rustup component add rustfmt script: - cargo fmt -- --checkproc-quote-0.4.0/Cargo.toml0000644000000022720000000000000111330ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "proc-quote" version = "0.4.0" authors = ["PedroGonçaloCorreia "] description = "A procedural macro implementation of quote!." readme = "README.md" keywords = ["syn", "quote", "proc-macro"] categories = ["development-tools::procedural-macro-helpers"] license = "MIT/Apache-2.0" repository = "https://github.com/Goncalerta/proc-quote" [dependencies.proc-macro-hack] version = "0.5" [dependencies.proc-macro2] version = "1.0" default-features = false [dependencies.proc-quote-impl] version = "0.3" [dependencies.quote] version = "1.0" [dependencies.syn] version = "1.0" [badges.travis-ci] repository = "Goncalerta/proc-quote" proc-quote-0.4.0/Cargo.toml.orig000064400000000000000000000015340000000000000145720ustar 00000000000000[package] name = "proc-quote" version = "0.4.0" authors = ["PedroGonçaloCorreia "] description = "A procedural macro implementation of quote!." license = "MIT/Apache-2.0" readme = "README.md" keywords = ["syn", "quote", "proc-macro"] categories = ["development-tools::procedural-macro-helpers"] repository = "https://github.com/Goncalerta/proc-quote" edition = "2018" [workspace] members = [ "proc-quote-impl" ] [dependencies] proc-quote-impl = { version = "0.3", path = "./proc-quote-impl" } quote = "1.0" proc-macro-hack = "0.5" proc-macro2 = { version = "1.0", default-features = false } # TODO(blocked on rust-lang/rust#54723) # https://github.com/rust-lang/rust/issues/54723 # Remove syn dependency once `new_raw` is stabilized syn = "1.0" [badges] travis-ci = { repository = "Goncalerta/proc-quote" } proc-quote-0.4.0/LICENSE-APACHE000064400000000000000000000240540000000000000136310ustar 00000000000000 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 Copyright 2019 Pedro Gonçalo Correia 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. proc-quote-0.4.0/LICENSE-MIT000064400000000000000000000020520000000000000133330ustar 00000000000000Copyright (c) 2019 Pedro Gonçalo Correia 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. proc-quote-0.4.0/README.md000064400000000000000000000247720000000000000131730ustar 00000000000000Rust Quasiquoter ================== [![Build Status](https://travis-ci.org/Goncalerta/proc-quote.svg?branch=master)](https://travis-ci.org/Goncalerta/proc-quote) [![Latest Version](https://img.shields.io/crates/v/proc-quote.svg)](https://crates.io/crates/proc-quote) [![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/proc-quote/0/proc_quote/macro.quote.html) This crate implements the [`quote!`] macro as a procedural macro, instead of [the original `quote!` macro](https://github.com/dtolnay/quote), implemented with `macro_rules!`. The [`quote!`] macro turns Rust syntax tree data structures into tokens of source code. [`quote!`]: https://docs.rs/proc-quote/0/proc_quote/macro.quote.html [`quote_spanned!`]: https://docs.rs/proc-quote/0/proc_quote/macro.quote_spanned.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 it is a general-purpose Rust quasi-quoting library and is not specific to procedural macros. ## From `quote` to `proc-quote` This crate serves the same purpose as [`quote`](https://crates.io/crates/quote) however it is implemented with procedural macros rather than `macro_rules!`. Switching from `quote` to the `proc_quote` crate **should not require any change in the code**. After changing your `Cargo.toml` dependency, change the following: ```rust extern crate quote; use quote::quote; use quote::quote_spanned; ``` respectively into: ```rust extern crate proc_quote; use proc_quote::quote; use proc_quote::quote_spanned; ``` And that's it! [`Repeat`]: https://docs.rs/proc-quote/0/proc_quote/trait.Repeat.html [`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html [`ToTokens`]: https://docs.rs/proc-quote/0/proc_quote/trait.ToTokens.html [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html ## 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/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/proc-quote/0/proc_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(&self, serializer: S) -> Result 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. - `#(#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 - `#(let #var = self.#var;)*` - the same variable can be used more than once 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!`. The [`proc_quote::Repeat`](https://docs.rs/proc-quote/0/proc_quote/trait.Repeat.html) trait defines which types are allowed to be interpolated inside a repition pattern. Which types *do* `Repeat`: - [`Iterator`] consumes the iterator, iterating through every element. - `Borrow<[T]>` (includes [`Vec`], [`array`], and [`slice`]) iterates with the [`slice::iter`] method, thus not consuming the original data. - [`ToTokens`], interpolates the variable in every iteration. Which types *do NOT* `Repeat`: - [`IntoIterator`], to avoid ambiguity (Ex. "Which behavior would have been used for [`Vec`], which implements both [`IntoIterator`] and `Borrow<[T]>`?"; "Which behavior would have been used for [`TokenStream`], which implements both [`IntoIterator`] and [`ToTokens`]?"). To use the iterator, you may call [`IntoIterator::into_iter`] explicitly. - Ambiguous types that implement at least two of the `Repeat` traits. In the very unlikely case this happens, disambiguate the type by wrapping it under some structure that only implements the trait you desire to use. [`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html [`array`]: https://doc.rust-lang.org/std/primitive.array.html [`slice`]: https://doc.rust-lang.org/std/slice/index.html [`slice::iter`]: https://doc.rust-lang.org/std/primitive.slice.html#method.iter [`ToTokens`]: https://docs.rs/proc-quote/0/proc_quote/trait.ToTokens.html [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html [`IntoIterator::into_iter`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#tymethod.into_iter ## 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` then the expanded code is `Vec::new()` which is invalid syntax. Ordinarily in handwritten Rust we would write `Vec::::new()` but for macros often the following is more convenient. ```rust quote! { let value = <#field_type>::new(); } ``` This expands to `>::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/proc_macro2/struct.Span.html#method.call_site A different span can be provided explicitly through the [`quote_spanned!`] macro. ## License Licensed under either of * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. ### Contribution 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. proc-quote-0.4.0/benches/bench/bench_impl.rs000064400000000000000000000233200000000000000170340ustar 00000000000000use super::*; #[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 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 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 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 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 where __D: _serde::Deserializer<'de>, { _serde::Deserializer::deserialize_identifier(__deserializer, __FieldVisitor) } } struct __Visitor<'de> { marker: _serde::export::PhantomData, 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 where __A: _serde::de::SeqAccess<'de>, { let __field0 = match try!(_serde::de::SeqAccess::next_element::(&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::(&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 where __A: _serde::de::MapAccess<'de>, { let mut __field0: _serde::export::Option = _serde::export::None; let mut __field1: _serde::export::Option = _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::(&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::(&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::, lifetime: _serde::export::PhantomData, }, ) } } } }); } proc-quote-0.4.0/benches/bench/mod.rs000064400000000000000000000000420000000000000155070ustar 00000000000000use super::*; mod bench_impl; proc-quote-0.4.0/benches/proc_quote.rs000064400000000000000000000001770000000000000160420ustar 00000000000000#![feature(test)] extern crate proc_quote; extern crate test; use proc_quote::quote; use test::Bencher; mod bench; proc-quote-0.4.0/benches/quote.rs000064400000000000000000000002220000000000000150060ustar 00000000000000#![feature(test)] #![recursion_limit = "512"] extern crate quote; extern crate test; use quote::quote; use test::Bencher; mod bench; proc-quote-0.4.0/src/lib.rs000064400000000000000000000415530000000000000136130ustar 00000000000000//! 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 it is a //! general-purpose Rust quasi-quoting library and is not specific to procedural //! macros. //! //! # 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(&self, serializer: S) -> Result //! where //! S: serde::Serializer, //! { //! #path(self.value, serializer) //! } //! } //! //! SerializeWith { //! value: #value, //! phantom: core::marker::PhantomData::<#item_ty>, //! } //! }; //! ``` use proc_macro_hack::proc_macro_hack; mod repeat; pub use self::repeat::*; pub use quote::ToTokens; pub use quote::TokenStreamExt; /// 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/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. /// /// - `#(#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 /// - `#(let #var = self.#var;)*` - the same variable can be used more than once /// /// The [`proc_quote::Repeat`](https://docs.rs/proc-quote/0/proc_quote/trait.Repeat.html) /// trait defines which types are allowed to be interpolated inside a repition pattern. /// /// Which types that implement the following traits *do* `Repeat`: /// - [`Iterator`] consumes the iterator, iterating through every element. /// - `Borrow<[T]>` /// (includes [`Vec`], [`array`], and [`slice`]) iterates with the [`slice::iter`] method, /// thus not consuming the original data. /// - [`ToTokens`], interpolates the variable in every iteration. /// /// Which types *do NOT* `Repeat`: /// - [`IntoIterator`], to avoid ambiguity (Ex. "Which behavior would have been used for [`Vec`], /// which implements both [`IntoIterator`] and /// `Borrow<[T]>`?"; "Which behavior would have been used for [`TokenStream`], which implements both /// [`IntoIterator`] and [`ToTokens`]?"). To use the iterator, you may call [`IntoIterator::into_iter`] /// explicitly. /// - Ambiguous types that implement at least two of the `Repeat` traits. In the very unlikely case /// this happens, disambiguate the type by wrapping it under some structure that only implements the /// trait you desire to use. /// /// [`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html /// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html /// [`array`]: https://doc.rust-lang.org/std/primitive.array.html /// [`slice`]: https://doc.rust-lang.org/std/slice/index.html /// [`slice::iter`]: https://doc.rust-lang.org/std/primitive.slice.html#method.iter /// [`ToTokens`]: https://docs.rs/proc-quote/0/proc_quote/trait.ToTokens.html /// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html /// [`IntoIterator::into_iter`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#tymethod.into_iter /// /// # 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/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` then the expanded code is `Vec::new()` which is invalid /// syntax. Ordinarily in handwritten Rust we would write `Vec::::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 `>::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(); /// } /// # ; /// ``` #[proc_macro_hack] pub use proc_quote_impl::quote; /// 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. #[proc_macro_hack] pub use proc_quote_impl::quote_spanned; // Not public API. #[doc(hidden)] pub mod __rt { use super::*; pub use proc_macro2::*; pub fn append_ident(stream: &mut TokenStream, ident: &str, span: Span) { // TODO(blocked on rust-lang/rust#54723) // https://github.com/rust-lang/rust/issues/54723 // Use `new_raw` once it's stabilized // stream.append(Ident::new_raw(ident, span)); match syn::parse_str::(ident) { Ok(mut ident) => { ident.set_span(span); stream.append(ident); } Err(_) => stream.append(Ident::new(ident, span)), } } pub fn append_punct(stream: &mut TokenStream, punct: char, spacing: Spacing, span: Span) { let mut punct = Punct::new(punct, spacing); punct.set_span(span); stream.append(punct); } pub fn append_stringified_tokens(stream: &mut TokenStream, s: &str, span: Span) { let s: TokenStream = s.parse().expect("invalid token stream"); stream.extend(s.into_iter().map(|mut t| { t.set_span(span); t })); } pub fn append_to_tokens(stream: &mut TokenStream, to_tokens: &T) { to_tokens.to_tokens(stream); } pub fn append_group( stream: &mut TokenStream, inner: TokenStream, delimiter: Delimiter, span: Span, ) { let mut group = Group::new(delimiter, inner); group.set_span(span); stream.append(group); } } proc-quote-0.4.0/src/repeat.rs000064400000000000000000000073370000000000000143270ustar 00000000000000use super::ToTokens; use std::borrow::Borrow; use std::slice; /// Defines the behavior of types that can be interpolated inside repeating patterns (`#(...)*`). /// /// ### Which types *do* `Repeat` /// - [`Iterator`] consumes the iterator, iterating through every element. /// - `Borrow<[T]>` /// (includes [`Vec`], [`array`], and [`slice`]) iterates with the [`slice::iter`] method, /// thus not consuming the original data. /// - [`ToTokens`], interpolates the variable in every iteration. /// /// ### Which types *do NOT* `Repeat` /// - [`IntoIterator`], to avoid ambiguity (Ex. "Which behavior would have been used for [`Vec`], /// which implements both [`IntoIterator`] and /// `Borrow<[T]>`?"; "Which behavior would have been used for [`TokenStream`], which implements both /// [`IntoIterator`] and [`ToTokens`]?"). To use the iterator, you may call [`IntoIterator::into_iter`] /// explicitly. /// - Ambiguous types that implement at least two of the `Repeat` traits. In the very unlikely case /// this happens, disambiguate the type by wrapping it under some structure that only implements the /// trait you desire to use. /// /// [`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html /// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html /// [`array`]: https://doc.rust-lang.org/std/primitive.array.html /// [`slice`]: https://doc.rust-lang.org/std/slice/index.html /// [`slice::iter`]: https://doc.rust-lang.org/std/primitive.slice.html#method.iter /// [`ToTokens`]: https://docs.rs/proc-quote/0/proc_quote/trait.ToTokens.html /// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html /// [`IntoIterator::into_iter`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#tymethod.into_iter pub trait Repeat: private::Sealed { // Long name to avoid collision // TODO(Blocked on #7): rename to `as_repeat` once collision is solved #[allow(non_snake_case)] #[doc(hidden)] fn __proc_quote__as_repeat(self) -> T; } mod private { use super::*; pub trait Sealed {} impl> Sealed for I {} impl<'a, T: 'a, S: Borrow<[T]>> Sealed> for &'a S {} impl<'a, T: ToTokens + 'a> Sealed> for &'a T {} } /// Types that implement `Iterator` may be used in repeating patterns. /// /// They, will be consumed, so they can only be used in one pattern. impl> Repeat for I { fn __proc_quote__as_repeat(self) -> I { self } } /// Types that implement `Borrow<[T]>` may be used in repeating patterns. /// /// This includes, but is not necessarily limited to, `Vec`, `array` and `slice`. /// /// They, will not be consumed. Instead `slice::iter` will be implicitly called. impl<'a, T: 'a, S: Borrow<[T]>> Repeat> for &'a S { fn __proc_quote__as_repeat(self) -> slice::Iter<'a, T> { (*self).borrow().iter() } } /// Types that implement `ToTokens` may be used in repeating patterns. /// /// The variable will be interpolated in every iteration. impl<'a, T: ToTokens + 'a> Repeat> for &'a T { fn __proc_quote__as_repeat(self) -> ToTokensRepeat<'a, T> { ToTokensRepeat(self) } } /// Struct that wraps a reference to `ToTokens` in order to use it in repeating patterns. pub struct ToTokensRepeat<'a, T: ToTokens + 'a>(&'a T); impl<'a, T: ToTokens + 'a> Iterator for ToTokensRepeat<'a, T> { type Item = &'a T; fn next(&mut self) -> Option { Some(self.0) } } proc-quote-0.4.0/tests/interpolation.rs000064400000000000000000000050200000000000000162740ustar 00000000000000extern crate proc_macro2; extern crate proc_quote; use proc_macro2::{Ident, Span, TokenStream}; use proc_quote::{quote, TokenStreamExt}; 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_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_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_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(&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 >, ", "} ", "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 :: >, ", "}" ); assert_eq!(expected, tokens.to_string()); } proc-quote-0.4.0/tests/repetition.rs000064400000000000000000000112520000000000000155730ustar 00000000000000extern crate proc_macro2; extern crate proc_quote; use proc_macro2::{Ident, Span, TokenStream}; use proc_quote::{quote, TokenStreamExt}; 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_repetition_simple() { 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_repetition_two_vars() { 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_repetition_nested() { 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_var_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_repetition_same_iter_twice() { let a = 1..=3; let b = quote! { #(#a #a)* }; assert_eq!("1i32 1i32 2i32 2i32 3i32 3i32", b.to_string()); } #[test] fn test_repetition_iterators() { let a = vec!["a", "b", "c"].into_iter(); let b = 2..6; let c = [5, 6, 7].into_iter().map(|_| X); let q = quote! { #(#a #b #c)* }; assert_eq!("\"a\" 2i32 X \"b\" 3i32 X \"c\" 4i32 X", q.to_string()); } #[test] fn test_repetition_slices() { let a = vec!["a", "b", "c"]; let b = &[2, 3, 4, 5]; let c = [5, 6, 7]; let q = quote! { #(#a #b #c)* }; assert_eq!( "\"a\" 2i32 5i32 \"b\" 3i32 6i32 \"c\" 4i32 7i32", q.to_string() ); } #[test] fn test_repetition_totokens() { let i = 1..3; // Iterator just to avoid infinite loop let a = 5; let b = "a"; let c = X; let q = quote! { #(#i #a #b #c)* }; assert_eq!("1i32 5i32 \"a\" X 2i32 5i32 \"a\" X", q.to_string()); } #[test] fn test_repetition_without_consuming() { // The following types should not be consumed by `quote!` let a = [1, 2, 3]; let b = vec!['a', 'b', 'c']; let c = 5.0; let q1 = quote! { #(#a #b #c)* #(#a #b #c)* // Usable in two different patterns }; let q2 = quote! { #(#a #b #c)* // Usable in a different `quote!` }; let d = (a[0], b[0], c); // Still usable in the following code assert_eq!( "1i32 'a' 5f64 2i32 'b' 5f64 3i32 'c' 5f64 1i32 'a' 5f64 2i32 'b' 5f64 3i32 'c' 5f64", q1.to_string() ); assert_eq!("1i32 'a' 5f64 2i32 'b' 5f64 3i32 'c' 5f64", q2.to_string()); assert_eq!(1, d.0); assert_eq!('a', d.1); assert_eq!(5.0, d.2); } #[test] fn test_repetition_trait_name_collision() { trait Repeat {} impl Repeat for X {} let a = 0..3; // Avoid infinite loop let x = X; let q = quote! { #(#a #x)* }; assert_eq!("0i32 X 1i32 X 2i32 X", q.to_string()); } #[test] #[ignore] // TODO(#7) trait fn collision fn test_repetition_trait_fn_collision() { unimplemented!("doesn't compile") // trait Repeat { // fn __proc_quote__as_repeat(self) -> !; // } // impl Repeat for X { // fn __proc_quote__as_repeat(self) -> ! { panic!("Wrong trait called.") } // } // let a = 0..3; // Avoid infinite loop // let x = X; // let q = quote! { // #(#a #x)* // }; // assert_eq!("0i32 X 1i32 X 2i32 X", q.to_string()); } #[test] #[ignore] // TODO(#7) struct method name collision fn test_repetition_impl_fn_collision() { unimplemented!("doesn't compile") // struct R; // impl R { // fn __proc_quote__as_repeat(self) -> ! { panic!("Wrong trait called.") } // } // impl quote::ToTokens for R { // fn to_tokens(&self, tokens: &mut TokenStream) { // tokens.append(Ident::new("X", Span::call_site())); // } // } // let a = 0..3; // Avoid infinite loop // let r = R; // let q = quote! { // #(#a #r)* // }; // assert_eq!("0i32 X 1i32 X 2i32 X", q.to_string()); } proc-quote-0.4.0/tests/test.rs000064400000000000000000000032550000000000000143740ustar 00000000000000extern crate proc_macro2; extern crate proc_quote; use proc_macro2::{Ident, Span}; use proc_quote::{quote, TokenStreamExt}; #[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_var_name_conflict() { // The implementation of `quote!` uses the variable `__stream` but it should be // fine, if a little confusing when debugging. let __stream = 'a'; let tokens = quote!( #__stream ); let expected = "'a'"; assert_eq!(expected, tokens.to_string()); } #[test] fn test_empty_quote() { let tokens = quote!(); assert_eq!("", 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()); } #[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_raw_ident() { let tokens = quote!(r#struct r#the); let expected = "r#struct r#the"; assert_eq!(expected, tokens.to_string()); } proc-quote-0.4.0/tests/types.rs000064400000000000000000000044330000000000000145600ustar 00000000000000extern crate proc_macro2; extern crate proc_quote; use std::borrow::Cow; use proc_macro2::{Ident, Span}; use proc_quote::quote; #[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()); } #[cfg(integer128)] #[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()); } #[test] fn test_char() { let zero = '\0'; let pound = '#'; let quote = '"'; let apost = '\''; let newline = '\n'; // ISSUE #19 https://github.com/Goncalerta/proc-quote/issues/19 //let heart = '\u{2764}'; let tokens = quote! { #zero #pound #quote #apost #newline }; let expected = r#"'\u{0}' '#' '"' '\'' '\n'"#; assert_eq!(expected, tokens.to_string()); } #[test] fn test_str() { let s = "\0 a 'b \" c"; let tokens = quote!(#s); let expected = r#""\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 = r#""\u{0} a 'b \" c""#; assert_eq!(expected, 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 = 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()); }