Move TokensOrDefault out of root
diff --git a/src/data.rs b/src/data.rs
index fff1c84..fe20fe2 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -318,9 +318,12 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
+
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
+ use print::TokensOrDefault;
+
impl ToTokens for Variant {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(&self.attrs);
diff --git a/src/derive.rs b/src/derive.rs
index a346950..5c1bb17 100644
--- a/src/derive.rs
+++ b/src/derive.rs
@@ -211,10 +211,13 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
- use attr::FilterAttrs;
+
use proc_macro2::TokenStream;
use quote::ToTokens;
+ use attr::FilterAttrs;
+ use print::TokensOrDefault;
+
impl ToTokens for DeriveInput {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attr in self.attrs.outer() {
diff --git a/src/expr.rs b/src/expr.rs
index 27b9eb5..ab3ba9a 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -2997,11 +2997,15 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
- #[cfg(feature = "full")]
- use attr::FilterAttrs;
+
use proc_macro2::{Literal, TokenStream};
use quote::{ToTokens, TokenStreamExt};
+ #[cfg(feature = "full")]
+ use attr::FilterAttrs;
+ #[cfg(feature = "full")]
+ use print::TokensOrDefault;
+
// If the given expression is a bare `ExprStruct`, wraps it in parenthesis
// before appending it to `TokenStream`.
#[cfg(feature = "full")]
diff --git a/src/generics.rs b/src/generics.rs
index 8903408..af23301 100644
--- a/src/generics.rs
+++ b/src/generics.rs
@@ -863,10 +863,13 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
- use attr::FilterAttrs;
+
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
+ use attr::FilterAttrs;
+ use print::TokensOrDefault;
+
impl ToTokens for Generics {
fn to_tokens(&self, tokens: &mut TokenStream) {
if self.params.is_empty() {
diff --git a/src/item.rs b/src/item.rs
index d95019d..849594e 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -1977,10 +1977,13 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
- use attr::FilterAttrs;
+
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
+ use attr::FilterAttrs;
+ use print::TokensOrDefault;
+
impl ToTokens for ItemExternCrate {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
diff --git a/src/lib.rs b/src/lib.rs
index 2387af7..799f1c7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -582,6 +582,12 @@
mod span;
+#[cfg(all(
+ any(feature = "full", feature = "derive"),
+ feature = "printing"
+))]
+mod print;
+
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "parsing")]
@@ -807,25 +813,3 @@
};
};
}
-
-#[cfg(all(
- any(feature = "full", feature = "derive"),
- feature = "printing"
-))]
-struct TokensOrDefault<'a, T: 'a>(&'a Option<T>);
-
-#[cfg(all(
- any(feature = "full", feature = "derive"),
- feature = "printing"
-))]
-impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T>
-where
- T: quote::ToTokens + Default,
-{
- fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
- match *self.0 {
- Some(ref t) => t.to_tokens(tokens),
- None => T::default().to_tokens(tokens),
- }
- }
-}
diff --git a/src/path.rs b/src/path.rs
index 2dafac8..e3ae9a7 100644
--- a/src/path.rs
+++ b/src/path.rs
@@ -458,9 +458,12 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
+
use proc_macro2::TokenStream;
use quote::ToTokens;
+ use print::TokensOrDefault;
+
impl ToTokens for Path {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.leading_colon.to_tokens(tokens);
diff --git a/src/print.rs b/src/print.rs
new file mode 100644
index 0000000..90570a0
--- /dev/null
+++ b/src/print.rs
@@ -0,0 +1,16 @@
+use proc_macro2::TokenStream;
+use quote::ToTokens;
+
+pub struct TokensOrDefault<'a, T: 'a>(pub &'a Option<T>);
+
+impl<'a, T> ToTokens for TokensOrDefault<'a, T>
+where
+ T: ToTokens + Default,
+{
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ match *self.0 {
+ Some(ref t) => t.to_tokens(tokens),
+ None => T::default().to_tokens(tokens),
+ }
+ }
+}
diff --git a/src/ty.rs b/src/ty.rs
index 2e909dd..94258e1 100644
--- a/src/ty.rs
+++ b/src/ty.rs
@@ -757,9 +757,12 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
+
use proc_macro2::TokenStream;
use quote::ToTokens;
+ use print::TokensOrDefault;
+
impl ToTokens for TypeSlice {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.bracket_token.surround(tokens, |tokens| {