• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 mod gen;
2 
3 use proc_macro2::{Ident, Literal, TokenStream};
4 use ref_cast::RefCast;
5 use std::fmt::{self, Debug};
6 use std::ops::Deref;
7 use syn::punctuated::Punctuated;
8 
9 #[derive(RefCast)]
10 #[repr(transparent)]
11 pub struct Lite<T: ?Sized> {
12     value: T,
13 }
14 
15 #[allow(non_snake_case)]
Lite<T: ?Sized>(value: &T) -> &Lite<T>16 pub fn Lite<T: ?Sized>(value: &T) -> &Lite<T> {
17     Lite::ref_cast(value)
18 }
19 
20 impl<T: ?Sized> Deref for Lite<T> {
21     type Target = T;
22 
deref(&self) -> &Self::Target23     fn deref(&self) -> &Self::Target {
24         &self.value
25     }
26 }
27 
28 impl Debug for Lite<bool> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result29     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
30         write!(formatter, "{}", self.value)
31     }
32 }
33 
34 impl Debug for Lite<u32> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result35     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
36         write!(formatter, "{}", self.value)
37     }
38 }
39 
40 impl Debug for Lite<usize> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result41     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
42         write!(formatter, "{}", self.value)
43     }
44 }
45 
46 impl Debug for Lite<String> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result47     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
48         write!(formatter, "{:?}", self.value)
49     }
50 }
51 
52 impl Debug for Lite<Ident> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result53     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
54         write!(formatter, "{:?}", self.value.to_string())
55     }
56 }
57 
58 impl Debug for Lite<Literal> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result59     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
60         write!(formatter, "{}", self.value)
61     }
62 }
63 
64 impl Debug for Lite<TokenStream> {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result65     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
66         write!(formatter, "`{}`", self.value)
67     }
68 }
69 
70 impl<'a, T> Debug for Lite<&'a T>
71 where
72     Lite<T>: Debug,
73 {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result74     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
75         Debug::fmt(Lite(&*self.value), formatter)
76     }
77 }
78 
79 impl<T> Debug for Lite<Box<T>>
80 where
81     Lite<T>: Debug,
82 {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result83     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
84         Debug::fmt(Lite(&*self.value), formatter)
85     }
86 }
87 
88 impl<T> Debug for Lite<Vec<T>>
89 where
90     Lite<T>: Debug,
91 {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result92     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
93         formatter
94             .debug_list()
95             .entries(self.value.iter().map(Lite))
96             .finish()
97     }
98 }
99 
100 impl<T, P> Debug for Lite<Punctuated<T, P>>
101 where
102     Lite<T>: Debug,
103 {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result104     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
105         formatter
106             .debug_list()
107             .entries(self.value.iter().map(Lite))
108             .finish()
109     }
110 }
111