Skip to content

Commit

Permalink
hide helper traits from calling code
Browse files Browse the repository at this point in the history
Makes a couple of changes to avoid helper traits from appearing anywhere in the
caller code.

It changes the name of the `Display` trait method to reduce the chance that an
inherent method conflicts with the type in question, causing undesirable
results. It also wraps the helper trait and declarations into an anonymous const
context, meaning the helper traits aren't visible to the calling context.
  • Loading branch information
mystor committed Nov 9, 2019
1 parent eb369ed commit ab1260c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
48 changes: 21 additions & 27 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,46 @@ use quote::{format_ident, quote};
use syn::{Data, DataEnum, DataStruct, DeriveInput, Error, Fields, Result};

pub fn derive(input: &DeriveInput) -> Result<TokenStream> {
match &input.data {
let impls = match &input.data {
Data::Struct(data) => impl_struct(input, data),
Data::Enum(data) => impl_enum(input, data),
Data::Union(_) => Err(Error::new_spanned(input, "Unions are not supported")),
}
}?;

let helpers = specialization();
Ok(quote! {
const _: () = {
#helpers
#impls
};
})
}

#[cfg(feature = "std")]
fn specialization() -> TokenStream {
quote! {
trait DisplayToDisplayDoc {
fn get_display(&self) -> Self;
fn __displaydoc_display(&self) -> &Self;
}

impl<T: core::fmt::Display> DisplayToDisplayDoc for &T {
fn get_display(&self) -> Self {
impl<T: core::fmt::Display> DisplayToDisplayDoc for T {
fn __displaydoc_display(&self) -> &Self {
self
}
}

trait PathToDisplayDoc {
fn get_display(&self) -> std::path::Display<'_>;
fn __displaydoc_display(&self) -> std::path::Display<'_>;
}

impl PathToDisplayDoc for std::path::Path {
fn get_display(&self) -> std::path::Display<'_> {
fn __displaydoc_display(&self) -> std::path::Display<'_> {
self.display()
}
}

impl PathToDisplayDoc for std::path::PathBuf {
fn get_display(&self) -> std::path::Display<'_> {
fn __displaydoc_display(&self) -> std::path::Display<'_> {
self.display()
}
}
Expand Down Expand Up @@ -74,13 +82,7 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
}
});

let needed_traits = specialization();

Ok(quote! {
#needed_traits

#display
})
Ok(quote! { #display })
}

fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
Expand All @@ -93,7 +95,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
.map(|variant| attr::display(&variant.attrs))
.collect::<Result<Vec<_>>>()?;

let display = if displays.iter().any(Option::is_some) {
if displays.iter().any(Option::is_some) {
let arms = data
.variants
.iter()
Expand All @@ -115,7 +117,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
})
})
.collect::<Result<Vec<_>>>()?;
Some(quote! {
Ok(quote! {
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
#[allow(unused_variables)]
Expand All @@ -126,14 +128,6 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
}
})
} else {
return Err(Error::new_spanned(input, "Missing doc comments"));
};

let needed_traits = specialization();

Ok(quote! {
#needed_traits

#display
})
Err(Error::new_spanned(input, "Missing doc comments"))
}
}
39 changes: 28 additions & 11 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ use proc_macro2::TokenStream;
use quote::quote_spanned;
use syn::{Ident, LitStr};

#[cfg(feature = "std")]
const IS_STD: bool = true;
#[cfg(not(feature = "std"))]
const IS_STD: bool = false;

macro_rules! peek_next {
($read:ident) => {
match $read.chars().next() {
Expand Down Expand Up @@ -49,8 +44,8 @@ impl Display {

let next = peek_next!(read);

let arg = if IS_STD && next == '}' {
quote_spanned!(span=> , (&#ident).get_display())
let arg = if cfg!(feature = "std") && next == '}' {
quote_spanned!(span=> , #ident.__displaydoc_display())
} else {
quote_spanned!(span=> , #ident)
};
Expand Down Expand Up @@ -120,11 +115,33 @@ mod tests {
assert(
"{v} {v:?} {0} {0:?}",
"{} {:?} {} {:?}",
", ( & v ) . get_display ( ) , v , ( & _0 ) . get_display ( ) , _0",
", v . __displaydoc_display ( ) , v , _0 . __displaydoc_display ( ) , _0",
);
assert(
"error {var}",
"error {}",
", var . __displaydoc_display ( )",
);

assert(
"The path {0}",
"The path {}",
", _0 . __displaydoc_display ( )",
);
assert("The path {0:?}", "The path {:?}", ", _0");
}

#[test]
#[cfg_attr(feature = "std", ignore)]
fn test_nostd_expand() {
assert(
"{v} {v:?} {0} {0:?}",
"{} {:?} {} {:?}",
", v , v , _0 , _0",
);
assert("error {var}", "error {}", ", ( & var ) . get_display ( )");
assert("error {var}", "error {}", ", var");

// assert("The path {0.display()}", "The path {}", "0.display()");
// assert("The path {0.display():?}", "The path {:?}", "0.display()");
assert("The path {0}", "The path {}", ", _0");
assert("The path {0:?}", "The path {:?}", ", _0");
}
}

0 comments on commit ab1260c

Please sign in to comment.