Merge pull request #530 from serde-rs/derive

Macros 1.1
This commit is contained in:
David Tolnay
2016-09-01 21:59:32 -07:00
committed by GitHub
4 changed files with 105 additions and 37 deletions
+25 -9
View File
@@ -36,14 +36,7 @@ include!(concat!(env!("OUT_DIR"), "/lib.rs"));
include!("lib.rs.in"); include!("lib.rs.in");
#[cfg(feature = "with-syntex")] #[cfg(feature = "with-syntex")]
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error> fn syntex_registry() -> syntex::Registry {
where S: AsRef<Path>,
D: AsRef<Path>,
{
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
let expand_thread = move || {
use syntax::{ast, fold}; use syntax::{ast, fold};
/// Strip the serde attributes from the crate. /// Strip the serde attributes from the crate.
@@ -80,7 +73,30 @@ pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
reg.add_post_expansion_pass(strip_attributes); reg.add_post_expansion_pass(strip_attributes);
reg.expand("", src, dst) reg
}
#[cfg(feature = "with-syntex")]
pub fn expand_str(src: &str) -> Result<String, syntex::Error> {
let src = src.to_owned();
let expand_thread = move || {
syntex_registry().expand_str("", "", &src)
};
syntex::with_extra_stack(expand_thread)
}
#[cfg(feature = "with-syntex")]
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
where S: AsRef<Path>,
D: AsRef<Path>,
{
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
let expand_thread = move || {
syntex_registry().expand("", src, dst)
}; };
syntex::with_extra_stack(expand_thread) syntex::with_extra_stack(expand_thread)
+23
View File
@@ -0,0 +1,23 @@
[package]
name = "serde_derive"
version = "0.8.5"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://serde.rs/codegen.html"
keywords = ["serde", "serialization"]
include = ["Cargo.toml", "src/**/*.rs"]
[lib]
name = "serde_derive"
rustc-macro = true
[dependencies]
serde_codegen = { version = "=0.8.5", path = "../serde_codegen" }
[dev-dependencies]
fnv = "1.0"
serde = { version = "0.8.5", path = "../serde" }
serde_test = { version = "0.8.5", path = "../serde_test" }
+21
View File
@@ -0,0 +1,21 @@
#![feature(rustc_macro, rustc_macro_lib)]
#![cfg(not(test))]
extern crate rustc_macro;
extern crate serde_codegen;
use rustc_macro::TokenStream;
#[rustc_macro_derive(Serialize)]
pub fn derive_serialize(input: TokenStream) -> TokenStream {
let item = format!("#[derive(Serialize)]\n{}", input);
let expanded = serde_codegen::expand_str(&item).unwrap();
expanded.parse().unwrap()
}
#[rustc_macro_derive(Deserialize)]
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
let item = format!("#[derive(Deserialize)]\n{}", input);
let expanded = serde_codegen::expand_str(&item).unwrap();
expanded.parse().unwrap()
}
+8
View File
@@ -0,0 +1,8 @@
#![feature(test, rustc_macro, rustc_attrs)]
#[macro_use]
extern crate serde_derive;
extern crate test;
include!("../../testing/tests/test.rs.in");