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