Merge pull request #19 from SimonSapin/hex

Add a Hex wrapper for integer type, with a ToTokens impl using `{:X}`.
tree: 5b5e55bdd7d166b8bbfefabedc8ce3bb8166150a
  1. src/
  2. tests/
  3. .gitignore
  4. .travis.yml
  5. Cargo.toml
  6. LICENSE-APACHE
  7. LICENSE-MIT
  8. README.md
README.md

Rust Quasi-Quoting

Build Status Latest Version Rust Documentation

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 interpolations

The 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.

License

Licensed under either of

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.