From dba1377d1f2af31705a13a4165101108ecd5db87 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 18 Aug 2016 13:42:38 -0400 Subject: [PATCH] Set RUST_MIN_STACK if unset --- serde_codegen/src/env.rs | 36 +++++++++++++++++++++++ serde_codegen/src/lib.rs | 62 ++++++++++++++++++++++++---------------- 2 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 serde_codegen/src/env.rs diff --git a/serde_codegen/src/env.rs b/serde_codegen/src/env.rs new file mode 100644 index 00000000..36272550 --- /dev/null +++ b/serde_codegen/src/env.rs @@ -0,0 +1,36 @@ +use std::env; +use std::ffi::OsStr; +use std::ops::Drop; + +pub fn set_if_unset(k: K, v: V) -> TmpEnv + where K: AsRef, + V: AsRef, +{ + match env::var(&k) { + Ok(_) => TmpEnv::WasAlreadySet, + Err(_) => { + env::set_var(&k, v); + TmpEnv::WasNotSet { k: k } + } + } +} + +#[must_use] +pub enum TmpEnv + where K: AsRef, +{ + WasAlreadySet, + WasNotSet { + k: K, + } +} + +impl Drop for TmpEnv + where K: AsRef, +{ + fn drop(&mut self) { + if let TmpEnv::WasNotSet { ref k } = *self { + env::remove_var(k); + } + } +} diff --git a/serde_codegen/src/lib.rs b/serde_codegen/src/lib.rs index 5cc90efa..3c72f43a 100644 --- a/serde_codegen/src/lib.rs +++ b/serde_codegen/src/lib.rs @@ -35,48 +35,62 @@ include!(concat!(env!("OUT_DIR"), "/lib.rs")); #[cfg(not(feature = "with-syntex"))] include!("lib.rs.in"); +#[cfg(feature = "with-syntex")] +mod env; + #[cfg(feature = "with-syntex")] pub fn expand(src: S, dst: D) -> Result<(), syntex::Error> where S: AsRef, D: AsRef, { - use syntax::{ast, fold}; + let src = src.as_ref().to_owned(); + let dst = dst.as_ref().to_owned(); - /// Strip the serde attributes from the crate. - #[cfg(feature = "with-syntex")] - fn strip_attributes(krate: ast::Crate) -> ast::Crate { - /// Helper folder that strips the serde attributes after the extensions have been expanded. - struct StripAttributeFolder; + let expand_thread = move || { + use syntax::{ast, fold}; - impl fold::Folder for StripAttributeFolder { - fn fold_attribute(&mut self, attr: ast::Attribute) -> Option { - match attr.node.value.node { - ast::MetaItemKind::List(ref n, _) if n == &"serde" => { return None; } - _ => {} + /// Strip the serde attributes from the crate. + #[cfg(feature = "with-syntex")] + fn strip_attributes(krate: ast::Crate) -> ast::Crate { + /// Helper folder that strips the serde attributes after the extensions have been expanded. + struct StripAttributeFolder; + + impl fold::Folder for StripAttributeFolder { + fn fold_attribute(&mut self, attr: ast::Attribute) -> Option { + match attr.node.value.node { + ast::MetaItemKind::List(ref n, _) if n == &"serde" => { return None; } + _ => {} + } + + Some(attr) } - Some(attr) + fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { + fold::noop_fold_mac(mac, self) + } } - fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { - fold::noop_fold_mac(mac, self) - } + fold::Folder::fold_crate(&mut StripAttributeFolder, krate) } - fold::Folder::fold_crate(&mut StripAttributeFolder, krate) - } + let mut reg = syntex::Registry::new(); - let mut reg = syntex::Registry::new(); + reg.add_attr("feature(custom_derive)"); + reg.add_attr("feature(custom_attribute)"); - reg.add_attr("feature(custom_derive)"); - reg.add_attr("feature(custom_attribute)"); + reg.add_decorator("derive_Serialize", ser::expand_derive_serialize); + reg.add_decorator("derive_Deserialize", de::expand_derive_deserialize); - reg.add_decorator("derive_Serialize", ser::expand_derive_serialize); - reg.add_decorator("derive_Deserialize", de::expand_derive_deserialize); + reg.add_post_expansion_pass(strip_attributes); - reg.add_post_expansion_pass(strip_attributes); + reg.expand("", src, dst) + }; - reg.expand("", src.as_ref(), dst.as_ref()) + // 16 MB stack unless otherwise specified + let _tmp_env = env::set_if_unset("RUST_MIN_STACK", "16777216"); + + use std::thread; + thread::spawn(expand_thread).join().unwrap() } #[cfg(not(feature = "with-syntex"))]