Debian Codegraph @

src/trait_handlers/debug/common.rs

raw symbols SCIP
1 use quote::quote;
2 use syn::{DeriveInput, Path, Type};
3
4 #[inline]
5 pub(crate) fn create_debug_map_builder() -> proc_macro2::TokenStream {
6 quote!(
7 #[allow(non_camel_case_types)] // We're using __ to help avoid clashes.
8 struct Educe__RawString(&'static str);
9
10 impl ::core::fmt::Debug for Educe__RawString {
11 #[inline]
12 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
13 f.write_str(self.0)
14 }
15 }
16
17 let mut builder = f.debug_map();
18 )
19 }
20
21 #[inline]
22 pub(crate) fn create_format_arg(
23 ast: &DeriveInput,
24 field_ty: &Type,
25 format_method: &Path,
26 field_expr: proc_macro2::TokenStream,
27 ) -> proc_macro2::TokenStream {
28 let ty_ident = &ast.ident;
29
30 // We use the complete original generics, not filtered by field,
31 // and include a PhantomData<Self> in our wrapper struct to use the generics.
32 //
33 // This avoids having to try to calculate the right *subset* of the generics
34 // relevant for this field, which is nontrivial and maybe impossible.
35 let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
36
37 quote!(
38 let arg = {
39 #[allow(non_camel_case_types)] // We're using __ to help avoid clashes.
40 struct Educe__DebugField<V, M>(V, ::core::marker::PhantomData<M>);
41
42 impl #impl_generics ::core::fmt::Debug
43 for Educe__DebugField<&#field_ty, #ty_ident #ty_generics>
44 #where_clause
45 {
46 #[inline]
47 fn fmt(&self, educe__f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
48 #format_method(self.0, educe__f)
49 }
50 }
51
52 Educe__DebugField(#field_expr, ::core::marker::PhantomData::<Self>)
53 };
54 )
55 }