1 use quote::quote; 2 3 #[allow(unused_macros)] 4 macro_rules! benchmark { 5 (|$ident:ident| $quote:expr) => { 6 mod timer; 7 8 use proc_macro::TokenStream; 9 use proc_macro2::Ident; 10 11 #[proc_macro] 12 pub fn run_quote_benchmark(input: TokenStream) -> TokenStream { 13 let input = proc_macro2::TokenStream::from(input); 14 let span = input.into_iter().next().unwrap().span(); 15 let $ident = Ident::new("Response", span); 16 timer::time("macro", || proc_macro::TokenStream::from($quote)); 17 TokenStream::new() 18 } 19 }; 20 } 21 22 #[allow(unused_imports)] 23 use benchmark; 24 25 crate::benchmark! { 26 |ident| quote! { 27 impl<'de> _serde::Deserialize<'de> for #ident { 28 fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<Self, __D::Error> 29 where 30 __D: _serde::Deserializer<'de>, 31 { 32 #[allow(non_camel_case_types)] 33 enum __Field { 34 __field0, 35 __field1, 36 __ignore, 37 } 38 struct __FieldVisitor; 39 impl<'de> _serde::de::Visitor<'de> for __FieldVisitor { 40 type Value = __Field; 41 fn expecting( 42 &self, 43 __formatter: &mut _serde::export::Formatter, 44 ) -> _serde::export::fmt::Result { 45 _serde::export::Formatter::write_str(__formatter, "field identifier") 46 } 47 fn visit_u64<__E>(self, __value: u64) -> _serde::export::Result<Self::Value, __E> 48 where 49 __E: _serde::de::Error, 50 { 51 match __value { 52 0u64 => _serde::export::Ok(__Field::__field0), 53 1u64 => _serde::export::Ok(__Field::__field1), 54 _ => _serde::export::Err(_serde::de::Error::invalid_value( 55 _serde::de::Unexpected::Unsigned(__value), 56 &"field index 0 <= i < 2", 57 )), 58 } 59 } 60 fn visit_str<__E>(self, __value: &str) -> _serde::export::Result<Self::Value, __E> 61 where 62 __E: _serde::de::Error, 63 { 64 match __value { 65 "id" => _serde::export::Ok(__Field::__field0), 66 "s" => _serde::export::Ok(__Field::__field1), 67 _ => _serde::export::Ok(__Field::__ignore), 68 } 69 } 70 fn visit_bytes<__E>( 71 self, 72 __value: &[u8], 73 ) -> _serde::export::Result<Self::Value, __E> 74 where 75 __E: _serde::de::Error, 76 { 77 match __value { 78 b"id" => _serde::export::Ok(__Field::__field0), 79 b"s" => _serde::export::Ok(__Field::__field1), 80 _ => _serde::export::Ok(__Field::__ignore), 81 } 82 } 83 } 84 impl<'de> _serde::Deserialize<'de> for __Field { 85 #[inline] 86 fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<Self, __D::Error> 87 where 88 __D: _serde::Deserializer<'de>, 89 { 90 _serde::Deserializer::deserialize_identifier(__deserializer, __FieldVisitor) 91 } 92 } 93 struct __Visitor<'de> { 94 marker: _serde::export::PhantomData<#ident>, 95 lifetime: _serde::export::PhantomData<&'de ()>, 96 } 97 impl<'de> _serde::de::Visitor<'de> for __Visitor<'de> { 98 type Value = #ident; 99 fn expecting( 100 &self, 101 __formatter: &mut _serde::export::Formatter, 102 ) -> _serde::export::fmt::Result { 103 _serde::export::Formatter::write_str(__formatter, "struct") 104 } 105 #[inline] 106 fn visit_seq<__A>( 107 self, 108 mut __seq: __A, 109 ) -> _serde::export::Result<Self::Value, __A::Error> 110 where 111 __A: _serde::de::SeqAccess<'de>, 112 { 113 let __field0 = 114 match try!(_serde::de::SeqAccess::next_element::<u64>(&mut __seq)) { 115 _serde::export::Some(__value) => __value, 116 _serde::export::None => { 117 return _serde::export::Err(_serde::de::Error::invalid_length( 118 0usize, 119 &"struct with 2 elements", 120 )); 121 } 122 }; 123 let __field1 = 124 match try!(_serde::de::SeqAccess::next_element::<String>(&mut __seq)) { 125 _serde::export::Some(__value) => __value, 126 _serde::export::None => { 127 return _serde::export::Err(_serde::de::Error::invalid_length( 128 1usize, 129 &"struct with 2 elements", 130 )); 131 } 132 }; 133 _serde::export::Ok(#ident { 134 id: __field0, 135 s: __field1, 136 }) 137 } 138 #[inline] 139 fn visit_map<__A>( 140 self, 141 mut __map: __A, 142 ) -> _serde::export::Result<Self::Value, __A::Error> 143 where 144 __A: _serde::de::MapAccess<'de>, 145 { 146 let mut __field0: _serde::export::Option<u64> = _serde::export::None; 147 let mut __field1: _serde::export::Option<String> = _serde::export::None; 148 while let _serde::export::Some(__key) = 149 try!(_serde::de::MapAccess::next_key::<__Field>(&mut __map)) 150 { 151 match __key { 152 __Field::__field0 => { 153 if _serde::export::Option::is_some(&__field0) { 154 return _serde::export::Err( 155 <__A::Error as _serde::de::Error>::duplicate_field("id"), 156 ); 157 } 158 __field0 = _serde::export::Some( 159 try!(_serde::de::MapAccess::next_value::<u64>(&mut __map)), 160 ); 161 } 162 __Field::__field1 => { 163 if _serde::export::Option::is_some(&__field1) { 164 return _serde::export::Err( 165 <__A::Error as _serde::de::Error>::duplicate_field("s"), 166 ); 167 } 168 __field1 = _serde::export::Some( 169 try!(_serde::de::MapAccess::next_value::<String>(&mut __map)), 170 ); 171 } 172 _ => { 173 let _ = try!(_serde::de::MapAccess::next_value::< 174 _serde::de::IgnoredAny, 175 >(&mut __map)); 176 } 177 } 178 } 179 let __field0 = match __field0 { 180 _serde::export::Some(__field0) => __field0, 181 _serde::export::None => try!(_serde::private::de::missing_field("id")), 182 }; 183 let __field1 = match __field1 { 184 _serde::export::Some(__field1) => __field1, 185 _serde::export::None => try!(_serde::private::de::missing_field("s")), 186 }; 187 _serde::export::Ok(#ident { 188 id: __field0, 189 s: __field1, 190 }) 191 } 192 } 193 const FIELDS: &'static [&'static str] = &["id", "s"]; 194 _serde::Deserializer::deserialize_struct( 195 __deserializer, 196 stringify!(#ident), 197 FIELDS, 198 __Visitor { 199 marker: _serde::export::PhantomData::<#ident>, 200 lifetime: _serde::export::PhantomData, 201 }, 202 ) 203 } 204 } 205 } 206 } 207