xref: /third_party/rust/crates/syn/tests/debug/mod.rs (revision fad3a1d3)
1#![allow(
2    clippy::no_effect_underscore_binding,
3    clippy::too_many_lines,
4    clippy::used_underscore_binding
5)]
6
7#[rustfmt::skip]
8mod gen;
9
10use proc_macro2::{Ident, Literal, TokenStream};
11use ref_cast::RefCast;
12use std::fmt::{self, Debug};
13use std::ops::Deref;
14use syn::punctuated::Punctuated;
15
16#[derive(RefCast)]
17#[repr(transparent)]
18pub struct Lite<T: ?Sized> {
19    value: T,
20}
21
22#[allow(non_snake_case)]
23pub fn Lite<T: ?Sized>(value: &T) -> &Lite<T> {
24    Lite::ref_cast(value)
25}
26
27impl<T: ?Sized> Deref for Lite<T> {
28    type Target = T;
29
30    fn deref(&self) -> &Self::Target {
31        &self.value
32    }
33}
34
35impl Debug for Lite<bool> {
36    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
37        write!(formatter, "{}", self.value)
38    }
39}
40
41impl Debug for Lite<u32> {
42    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
43        write!(formatter, "{}", self.value)
44    }
45}
46
47impl Debug for Lite<usize> {
48    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
49        write!(formatter, "{}", self.value)
50    }
51}
52
53impl Debug for Lite<String> {
54    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
55        write!(formatter, "{:?}", self.value)
56    }
57}
58
59impl Debug for Lite<Ident> {
60    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
61        write!(formatter, "{:?}", self.value.to_string())
62    }
63}
64
65impl Debug for Lite<Literal> {
66    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
67        write!(formatter, "{}", self.value)
68    }
69}
70
71impl Debug for Lite<TokenStream> {
72    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
73        let string = self.value.to_string();
74        if string.len() <= 80 {
75            write!(formatter, "TokenStream(`{}`)", self.value)
76        } else {
77            formatter
78                .debug_tuple("TokenStream")
79                .field(&format_args!("`{}`", string))
80                .finish()
81        }
82    }
83}
84
85impl<'a, T> Debug for Lite<&'a T>
86where
87    Lite<T>: Debug,
88{
89    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
90        Debug::fmt(Lite(self.value), formatter)
91    }
92}
93
94impl<T> Debug for Lite<Box<T>>
95where
96    Lite<T>: Debug,
97{
98    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
99        Debug::fmt(Lite(&*self.value), formatter)
100    }
101}
102
103impl<T> Debug for Lite<Vec<T>>
104where
105    Lite<T>: Debug,
106{
107    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
108        formatter
109            .debug_list()
110            .entries(self.value.iter().map(Lite))
111            .finish()
112    }
113}
114
115impl<T, P> Debug for Lite<Punctuated<T, P>>
116where
117    Lite<T>: Debug,
118    Lite<P>: Debug,
119{
120    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
121        let mut list = formatter.debug_list();
122        for pair in self.pairs() {
123            let (node, punct) = pair.into_tuple();
124            list.entry(Lite(node));
125            list.entries(punct.map(Lite));
126        }
127        list.finish()
128    }
129}
130
131struct Present;
132
133impl Debug for Present {
134    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
135        formatter.write_str("Some")
136    }
137}
138
139struct Option {
140    present: bool,
141}
142
143impl Debug for Option {
144    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
145        formatter.write_str(if self.present { "Some" } else { "None" })
146    }
147}
148