| 1 |
#![allow( |
| 2 |
clippy::no_effect_underscore_binding, |
| 3 |
clippy::too_many_lines, |
| 4 |
clippy::used_underscore_binding |
| 5 |
)] |
| 6 |
|
| 7 |
#[rustfmt::skip] |
| 8 |
mod gen; |
| 9 |
|
| 10 |
use proc_macro2::{Ident, Literal, TokenStream}; |
| 11 |
use ref_cast::RefCast; |
| 12 |
use std::fmt::{self, Debug}; |
| 13 |
use std::ops::Deref; |
| 14 |
use syn::punctuated::Punctuated; |
| 15 |
|
| 16 |
#[derive(RefCast)] |
| 17 |
#[repr(transparent)] |
| 18 |
pub struct Lite<T: ?Sized> { |
| 19 |
value: T, |
| 20 |
} |
| 21 |
|
| 22 |
#[allow(non_snake_case)] |
| 23 |
pub fn Lite<T: ?Sized>(value: &T) -> &Lite<T> { |
| 24 |
Lite::ref_cast(value) |
| 25 |
} |
| 26 |
|
| 27 |
impl<T: ?Sized> Deref for Lite<T> { |
| 28 |
type Target = T; |
| 29 |
|
| 30 |
fn deref(&self) -> &Self::Target { |
| 31 |
&self.value |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl Debug for Lite<bool> { |
| 36 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 37 |
write!(formatter, "{}", self.value) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl Debug for Lite<u32> { |
| 42 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 43 |
write!(formatter, "{}", self.value) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl Debug for Lite<usize> { |
| 48 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 49 |
write!(formatter, "{}", self.value) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl Debug for Lite<String> { |
| 54 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 55 |
write!(formatter, "{:?}", self.value) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl Debug for Lite<Ident> { |
| 60 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 61 |
write!(formatter, "{:?}", self.value.to_string()) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl Debug for Lite<Literal> { |
| 66 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 67 |
write!(formatter, "{}", self.value) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl 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 |
|
| 85 |
impl<T> Debug for Lite<&T> |
| 86 |
where |
| 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 |
|
| 94 |
impl<T> Debug for Lite<Box<T>> |
| 95 |
where |
| 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 |
|
| 103 |
impl<T> Debug for Lite<Vec<T>> |
| 104 |
where |
| 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 |
|
| 115 |
impl<T, P> Debug for Lite<Punctuated<T, P>> |
| 116 |
where |
| 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 |
|
| 131 |
struct Present; |
| 132 |
|
| 133 |
impl Debug for Present { |
| 134 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 135 |
formatter.write_str("Some") |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
struct Option { |
| 140 |
present: bool, |
| 141 |
} |
| 142 |
|
| 143 |
impl 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 |
} |