1 use lib::*; 2 3 use de::{ 4 Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor, 5 }; 6 7 /// An efficient way of discarding data from a deserializer. 8 /// 9 /// Think of this like `serde_json::Value` in that it can be deserialized from 10 /// any type, except that it does not store any information about the data that 11 /// gets deserialized. 12 /// 13 /// ```edition2018 14 /// use std::fmt; 15 /// use std::marker::PhantomData; 16 /// 17 /// use serde::de::{ 18 /// self, Deserialize, DeserializeSeed, Deserializer, IgnoredAny, SeqAccess, Visitor, 19 /// }; 20 /// 21 /// /// A seed that can be used to deserialize only the `n`th element of a sequence 22 /// /// while efficiently discarding elements of any type before or after index `n`. 23 /// /// 24 /// /// For example to deserialize only the element at index 3: 25 /// /// 26 /// /// ``` 27 /// /// NthElement::new(3).deserialize(deserializer) 28 /// /// ``` 29 /// pub struct NthElement<T> { 30 /// n: usize, 31 /// marker: PhantomData<T>, 32 /// } 33 /// 34 /// impl<T> NthElement<T> { 35 /// pub fn new(n: usize) -> Self { 36 /// NthElement { 37 /// n: n, 38 /// marker: PhantomData, 39 /// } 40 /// } 41 /// } 42 /// 43 /// impl<'de, T> Visitor<'de> for NthElement<T> 44 /// where 45 /// T: Deserialize<'de>, 46 /// { 47 /// type Value = T; 48 /// 49 /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 50 /// write!( 51 /// formatter, 52 /// "a sequence in which we care about element {}", 53 /// self.n 54 /// ) 55 /// } 56 /// 57 /// fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> 58 /// where 59 /// A: SeqAccess<'de>, 60 /// { 61 /// // Skip over the first `n` elements. 62 /// for i in 0..self.n { 63 /// // It is an error if the sequence ends before we get to element `n`. 64 /// if seq.next_element::<IgnoredAny>()?.is_none() { 65 /// return Err(de::Error::invalid_length(i, &self)); 66 /// } 67 /// } 68 /// 69 /// // Deserialize the one we care about. 70 /// let nth = match seq.next_element()? { 71 /// Some(nth) => nth, 72 /// None => { 73 /// return Err(de::Error::invalid_length(self.n, &self)); 74 /// } 75 /// }; 76 /// 77 /// // Skip over any remaining elements in the sequence after `n`. 78 /// while let Some(IgnoredAny) = seq.next_element()? { 79 /// // ignore 80 /// } 81 /// 82 /// Ok(nth) 83 /// } 84 /// } 85 /// 86 /// impl<'de, T> DeserializeSeed<'de> for NthElement<T> 87 /// where 88 /// T: Deserialize<'de>, 89 /// { 90 /// type Value = T; 91 /// 92 /// fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> 93 /// where 94 /// D: Deserializer<'de>, 95 /// { 96 /// deserializer.deserialize_seq(self) 97 /// } 98 /// } 99 /// 100 /// # fn example<'de, D>(deserializer: D) -> Result<(), D::Error> 101 /// # where 102 /// # D: Deserializer<'de>, 103 /// # { 104 /// // Deserialize only the sequence element at index 3 from this deserializer. 105 /// // The element at index 3 is required to be a string. Elements before and 106 /// // after index 3 are allowed to be of any type. 107 /// let s: String = NthElement::new(3).deserialize(deserializer)?; 108 /// # Ok(()) 109 /// # } 110 /// ``` 111 #[derive(Copy, Clone, Debug, Default)] 112 pub struct IgnoredAny; 113 114 impl<'de> Visitor<'de> for IgnoredAny { 115 type Value = IgnoredAny; 116 expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result117 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 118 formatter.write_str("anything at all") 119 } 120 121 #[inline] visit_bool<E>(self, x: bool) -> Result<Self::Value, E>122 fn visit_bool<E>(self, x: bool) -> Result<Self::Value, E> { 123 let _ = x; 124 Ok(IgnoredAny) 125 } 126 127 #[inline] visit_i64<E>(self, x: i64) -> Result<Self::Value, E>128 fn visit_i64<E>(self, x: i64) -> Result<Self::Value, E> { 129 let _ = x; 130 Ok(IgnoredAny) 131 } 132 133 serde_if_integer128! { 134 #[inline] 135 fn visit_i128<E>(self, x: i128) -> Result<Self::Value, E> { 136 let _ = x; 137 Ok(IgnoredAny) 138 } 139 } 140 141 #[inline] visit_u64<E>(self, x: u64) -> Result<Self::Value, E>142 fn visit_u64<E>(self, x: u64) -> Result<Self::Value, E> { 143 let _ = x; 144 Ok(IgnoredAny) 145 } 146 147 serde_if_integer128! { 148 #[inline] 149 fn visit_u128<E>(self, x: u128) -> Result<Self::Value, E> { 150 let _ = x; 151 Ok(IgnoredAny) 152 } 153 } 154 155 #[inline] visit_f64<E>(self, x: f64) -> Result<Self::Value, E>156 fn visit_f64<E>(self, x: f64) -> Result<Self::Value, E> { 157 let _ = x; 158 Ok(IgnoredAny) 159 } 160 161 #[inline] visit_str<E>(self, s: &str) -> Result<Self::Value, E> where E: Error,162 fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> 163 where 164 E: Error, 165 { 166 let _ = s; 167 Ok(IgnoredAny) 168 } 169 170 #[inline] visit_none<E>(self) -> Result<Self::Value, E>171 fn visit_none<E>(self) -> Result<Self::Value, E> { 172 Ok(IgnoredAny) 173 } 174 175 #[inline] visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,176 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> 177 where 178 D: Deserializer<'de>, 179 { 180 IgnoredAny::deserialize(deserializer) 181 } 182 183 #[inline] visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,184 fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> 185 where 186 D: Deserializer<'de>, 187 { 188 IgnoredAny::deserialize(deserializer) 189 } 190 191 #[inline] visit_unit<E>(self) -> Result<Self::Value, E>192 fn visit_unit<E>(self) -> Result<Self::Value, E> { 193 Ok(IgnoredAny) 194 } 195 196 #[inline] visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> where A: SeqAccess<'de>,197 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> 198 where 199 A: SeqAccess<'de>, 200 { 201 while let Some(IgnoredAny) = try!(seq.next_element()) { 202 // Gobble 203 } 204 Ok(IgnoredAny) 205 } 206 207 #[inline] visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: MapAccess<'de>,208 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> 209 where 210 A: MapAccess<'de>, 211 { 212 while let Some((IgnoredAny, IgnoredAny)) = try!(map.next_entry()) { 213 // Gobble 214 } 215 Ok(IgnoredAny) 216 } 217 218 #[inline] visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E> where E: Error,219 fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E> 220 where 221 E: Error, 222 { 223 let _ = bytes; 224 Ok(IgnoredAny) 225 } 226 visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> where A: EnumAccess<'de>,227 fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> 228 where 229 A: EnumAccess<'de>, 230 { 231 data.variant::<IgnoredAny>()?.1.newtype_variant() 232 } 233 } 234 235 impl<'de> Deserialize<'de> for IgnoredAny { 236 #[inline] deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error> where D: Deserializer<'de>,237 fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error> 238 where 239 D: Deserializer<'de>, 240 { 241 deserializer.deserialize_ignored_any(IgnoredAny) 242 } 243 } 244