commit | 11dc4cdb9bd0745595d526453ef65dadaf4871d6 | [log] [tgz] |
---|---|---|
author | David Tolnay <dtolnay@gmail.com> | Fri Nov 25 12:49:46 2016 -0500 |
committer | GitHub <noreply@github.com> | Fri Nov 25 12:49:46 2016 -0500 |
tree | 5b5e55bdd7d166b8bbfefabedc8ce3bb8166150a | |
parent | 9a9412a5b66c3b80d9232fd0ced1f6c1f778747e [diff] | |
parent | b8e28326c86babdc70d87ba52c0048feb483440f [diff] |
Merge pull request #19 from SimonSapin/hex Add a Hex wrapper for integer type, with a ToTokens impl using `{:X}`.
Quasi-quoting without a Syntex dependency, intended for use with Macros 1.1.
[dependencies] quote = "0.3.9"
#[macro_use] extern crate quote;
Interpolation is done with #var
:
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>, } };
Repetition is done using #(...)*
or #(...),*
very similar to macro_rules!
:
#(#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 interpolationsThe return type of quote!
is quote::Tokens
. Tokens can be interpolated into other quotes:
let t = quote! { /* ... */ }; return quote! { /* ... */ #t /* ... */ };
Call to_string()
or as_str()
on a Tokens to get a String
or &str
of Rust code.
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.
Licensed under either of
at your option.
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.