commit | 6c9f5b6426c4302281c541059ce4f739427bf98c | [log] [tgz] |
---|---|---|
author | David Tolnay <dtolnay@gmail.com> | Tue Sep 13 15:19:22 2016 -0700 |
committer | David Tolnay <dtolnay@gmail.com> | Tue Sep 13 15:19:22 2016 -0700 |
tree | 3dc9ede1949d7df9fe5fb490cae486971cb50507 | |
parent | 0fc2a1674458a38e4b560c2711fadf0b9c02ac9f [diff] |
Generalize macros 1.1 example
Parse Rust structs and enums without a Syntex dependency, intended for use with Macros 1.1.
Designed for fast compile time.
syn
(from scratch including all dependencies): 6 secondssyntex
/quasi
/aster
stack: 60+ seconds[dependencies] syn = "0.5"
extern crate syn; let raw = " #[derive(Debug, Clone, Eq, PartialEq)] pub struct Item { pub ident: Ident, pub vis: Visibility, pub attrs: Vec<Attribute>, pub generics: Generics, pub body: Body, } "; let ast = syn::parse_item(raw).unwrap();
// lib.rs #![crate_type = "rustc-macro"] #![feature(rustc_macro, rustc_macro_lib)] extern crate rustc_macro; #[macro_use] extern crate quote; extern crate syn; use rustc_macro::TokenStream; #[rustc_macro_derive(SpecialItem)] pub fn special_item(input: TokenStream) -> TokenStream { let source = input.to_string(); // Parse a string of items to an AST let ast = syn::parse_item(&source).unwrap(); // Build the output, possibly using quasi-quotation let expanded = quote! { // ... }; // Parse this back to a token stream and return it expanded.to_string().parse().unwrap() }
# Cargo.toml # ... [dependencies] syn = "0.5" quote = "0.1" [lib] rustc-macro = true
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.