David Tolnay | 3c3c7d1 | 2019-05-08 14:54:12 -0700 | [diff] [blame] | 1 | extern crate proc_macro2; |
| 2 | extern crate ref_cast; |
| 3 | |
| 4 | mod gen; |
| 5 | |
| 6 | use self::proc_macro2::{Ident, Literal, TokenStream}; |
| 7 | use self::ref_cast::RefCast; |
| 8 | use std::fmt::{self, Debug}; |
| 9 | use std::ops::Deref; |
| 10 | use syn::punctuated::Punctuated; |
| 11 | |
| 12 | #[derive(RefCast)] |
| 13 | #[repr(transparent)] |
| 14 | pub struct Lite<T: ?Sized> { |
| 15 | value: T, |
| 16 | } |
| 17 | |
| 18 | #[allow(non_snake_case)] |
| 19 | pub fn Lite<T: ?Sized>(value: &T) -> &Lite<T> { |
| 20 | Lite::ref_cast(value) |
| 21 | } |
| 22 | |
| 23 | impl<T: ?Sized> Deref for Lite<T> { |
| 24 | type Target = T; |
| 25 | |
| 26 | fn deref(&self) -> &Self::Target { |
| 27 | &self.value |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl Debug for Lite<bool> { |
| 32 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 33 | write!(formatter, "{}", self.value) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Debug for Lite<u32> { |
| 38 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 39 | write!(formatter, "{}", self.value) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Debug for Lite<usize> { |
| 44 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 45 | write!(formatter, "{}", self.value) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl Debug for Lite<String> { |
| 50 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 51 | write!(formatter, "{:?}", self.value) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Debug for Lite<Ident> { |
| 56 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 57 | write!(formatter, "{:?}", self.value.to_string()) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl Debug for Lite<Literal> { |
| 62 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 63 | write!(formatter, "{}", self.value) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl Debug for Lite<TokenStream> { |
| 68 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 69 | write!(formatter, "`{}`", self.value) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl<T> Debug for Lite<Box<T>> |
| 74 | where |
| 75 | Lite<T>: Debug, |
| 76 | { |
| 77 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 78 | Debug::fmt(Lite(&*self.value), formatter) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl<T> Debug for Lite<Vec<T>> |
| 83 | where |
| 84 | Lite<T>: Debug, |
| 85 | { |
| 86 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 87 | formatter |
| 88 | .debug_list() |
| 89 | .entries(self.value.iter().map(Lite)) |
| 90 | .finish() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl<T, P> Debug for Lite<Punctuated<T, P>> |
| 95 | where |
| 96 | Lite<T>: Debug, |
| 97 | { |
| 98 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 99 | formatter |
| 100 | .debug_list() |
| 101 | .entries(self.value.iter().map(Lite)) |
| 102 | .finish() |
| 103 | } |
| 104 | } |