From 19d31605410678e6f76a6971ee0ee88377337d72 Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Thu, 22 Feb 2018 05:04:04 -0500 Subject: [PATCH] Add a feature for linking to proc_macro. This feature is enabled by default for backwards compatibility, but it can be disabled in order to break the runtime dependency on the dynamic library libproc_macro in the rustc toolchain. --- .travis.yml | 1 + Cargo.toml | 8 +++++++- src/lib.rs | 4 ++++ src/tokens.rs | 2 ++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 38ae811..d55e0b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,3 +10,4 @@ rust: script: - cargo test + - cargo test --no-default-features diff --git a/Cargo.toml b/Cargo.toml index 8631ddb..a8bb101 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,10 @@ keywords = ["syn"] include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] [dependencies] -proc-macro2 = "0.2" +proc-macro2 = { version = "0.2.3", default-features = false } + +[features] +default = ["proc-macro"] +# Disabling the proc-macro feature removes the dynamic library dependency on +# libproc_macro in the rustc compiler. +proc-macro = ["proc-macro2/proc-macro"] diff --git a/src/lib.rs b/src/lib.rs index 7d17434..2620a6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,6 +94,7 @@ #![doc(html_root_url = "https://docs.rs/quote/0.4.2")] extern crate proc_macro2; +#[cfg(feature = "proc-macro")] extern crate proc_macro; mod tokens; @@ -169,16 +170,19 @@ pub mod __rt { /// # Example /// /// ``` +/// # #[cfg(feature = "proc-macro")] /// extern crate proc_macro; /// /// #[macro_use] /// extern crate quote; /// +/// # #[cfg(feature = "proc-macro")] /// use proc_macro::TokenStream; /// /// # const IGNORE_TOKENS: &'static str = stringify! { /// #[proc_macro_derive(HeapSize)] /// # }; +/// # #[cfg(feature = "proc-macro")] /// pub fn derive_heap_size(input: TokenStream) -> TokenStream { /// // Parse the input and figure out what implementation to generate... /// # const IGNORE_TOKENS: &'static str = stringify! { diff --git a/src/tokens.rs b/src/tokens.rs index 8fa2e44..a58e49b 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -2,6 +2,7 @@ use super::ToTokens; use std::fmt::{self, Debug, Display}; use std::hash::{Hash, Hasher}; +#[cfg(feature = "proc-macro")] use proc_macro; use proc_macro2::{TokenStream, TokenTree}; @@ -108,6 +109,7 @@ impl From for TokenStream { } } +#[cfg(feature = "proc-macro")] impl From for proc_macro::TokenStream { fn from(tokens: Tokens) -> proc_macro::TokenStream { TokenStream::from(tokens).into()