| 1 |
use crate::lib::*; |
| 2 |
|
| 3 |
use crate::de::{ |
| 4 |
Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, Unexpected, VariantAccess, |
| 5 |
Visitor, |
| 6 |
}; |
| 7 |
|
| 8 |
use crate::seed::InPlaceSeed; |
| 9 |
|
| 10 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 11 |
use crate::de::size_hint; |
| 12 |
|
| 13 |
//////////////////////////////////////////////////////////////////////////////// |
| 14 |
|
| 15 |
struct UnitVisitor; |
| 16 |
|
| 17 |
impl<'de> Visitor<'de> for UnitVisitor { |
| 18 |
type Value = (); |
| 19 |
|
| 20 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 21 |
formatter.write_str("unit") |
| 22 |
} |
| 23 |
|
| 24 |
fn visit_unit<E>(self) -> Result<Self::Value, E> |
| 25 |
where |
| 26 |
E: Error, |
| 27 |
{ |
| 28 |
Ok(()) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl<'de> Deserialize<'de> for () { |
| 33 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 34 |
where |
| 35 |
D: Deserializer<'de>, |
| 36 |
{ |
| 37 |
deserializer.deserialize_unit(UnitVisitor) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[cfg(feature = "unstable")] |
| 42 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "unstable")))] |
| 43 |
impl<'de> Deserialize<'de> for ! { |
| 44 |
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error> |
| 45 |
where |
| 46 |
D: Deserializer<'de>, |
| 47 |
{ |
| 48 |
Err(Error::custom("cannot deserialize `!`")) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
//////////////////////////////////////////////////////////////////////////////// |
| 53 |
|
| 54 |
struct BoolVisitor; |
| 55 |
|
| 56 |
impl<'de> Visitor<'de> for BoolVisitor { |
| 57 |
type Value = bool; |
| 58 |
|
| 59 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 60 |
formatter.write_str("a boolean") |
| 61 |
} |
| 62 |
|
| 63 |
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> |
| 64 |
where |
| 65 |
E: Error, |
| 66 |
{ |
| 67 |
Ok(v) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl<'de> Deserialize<'de> for bool { |
| 72 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 73 |
where |
| 74 |
D: Deserializer<'de>, |
| 75 |
{ |
| 76 |
deserializer.deserialize_bool(BoolVisitor) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
//////////////////////////////////////////////////////////////////////////////// |
| 81 |
|
| 82 |
macro_rules! impl_deserialize_num { |
| 83 |
($primitive:ident, $nonzero:ident $(cfg($($cfg:tt)*))*, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { |
| 84 |
impl_deserialize_num!($primitive, $deserialize $($method!($($val : $visit)*);)*); |
| 85 |
|
| 86 |
$(#[cfg($($cfg)*)])* |
| 87 |
impl<'de> Deserialize<'de> for num::$nonzero { |
| 88 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 89 |
where |
| 90 |
D: Deserializer<'de>, |
| 91 |
{ |
| 92 |
struct NonZeroVisitor; |
| 93 |
|
| 94 |
impl<'de> Visitor<'de> for NonZeroVisitor { |
| 95 |
type Value = num::$nonzero; |
| 96 |
|
| 97 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 98 |
formatter.write_str(concat!("a nonzero ", stringify!($primitive))) |
| 99 |
} |
| 100 |
|
| 101 |
$($($method!(nonzero $primitive $val : $visit);)*)* |
| 102 |
} |
| 103 |
|
| 104 |
deserializer.$deserialize(NonZeroVisitor) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#[cfg(not(no_core_num_saturating))] |
| 109 |
impl<'de> Deserialize<'de> for Saturating<$primitive> { |
| 110 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 111 |
where |
| 112 |
D: Deserializer<'de>, |
| 113 |
{ |
| 114 |
struct SaturatingVisitor; |
| 115 |
|
| 116 |
impl<'de> Visitor<'de> for SaturatingVisitor { |
| 117 |
type Value = Saturating<$primitive>; |
| 118 |
|
| 119 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 120 |
formatter.write_str("integer with support for saturating semantics") |
| 121 |
} |
| 122 |
|
| 123 |
$($($method!(saturating $primitive $val : $visit);)*)* |
| 124 |
} |
| 125 |
|
| 126 |
deserializer.$deserialize(SaturatingVisitor) |
| 127 |
} |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
($primitive:ident, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { |
| 132 |
impl<'de> Deserialize<'de> for $primitive { |
| 133 |
#[inline] |
| 134 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 135 |
where |
| 136 |
D: Deserializer<'de>, |
| 137 |
{ |
| 138 |
struct PrimitiveVisitor; |
| 139 |
|
| 140 |
impl<'de> Visitor<'de> for PrimitiveVisitor { |
| 141 |
type Value = $primitive; |
| 142 |
|
| 143 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 144 |
formatter.write_str(stringify!($primitive)) |
| 145 |
} |
| 146 |
|
| 147 |
$($($method!($val : $visit);)*)* |
| 148 |
} |
| 149 |
|
| 150 |
deserializer.$deserialize(PrimitiveVisitor) |
| 151 |
} |
| 152 |
} |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
macro_rules! num_self { |
| 157 |
($ty:ident : $visit:ident) => { |
| 158 |
#[inline] |
| 159 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 160 |
where |
| 161 |
E: Error, |
| 162 |
{ |
| 163 |
Ok(v) |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
(nonzero $primitive:ident $ty:ident : $visit:ident) => { |
| 168 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 169 |
where |
| 170 |
E: Error, |
| 171 |
{ |
| 172 |
if let Some(nonzero) = Self::Value::new(v) { |
| 173 |
Ok(nonzero) |
| 174 |
} else { |
| 175 |
Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) |
| 176 |
} |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
(saturating $primitive:ident $ty:ident : $visit:ident) => { |
| 181 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 182 |
where |
| 183 |
E: Error, |
| 184 |
{ |
| 185 |
Ok(Saturating(v)) |
| 186 |
} |
| 187 |
}; |
| 188 |
} |
| 189 |
|
| 190 |
macro_rules! num_as_self { |
| 191 |
($ty:ident : $visit:ident) => { |
| 192 |
#[inline] |
| 193 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 194 |
where |
| 195 |
E: Error, |
| 196 |
{ |
| 197 |
Ok(v as Self::Value) |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
(nonzero $primitive:ident $ty:ident : $visit:ident) => { |
| 202 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 203 |
where |
| 204 |
E: Error, |
| 205 |
{ |
| 206 |
if let Some(nonzero) = Self::Value::new(v as $primitive) { |
| 207 |
Ok(nonzero) |
| 208 |
} else { |
| 209 |
Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) |
| 210 |
} |
| 211 |
} |
| 212 |
}; |
| 213 |
|
| 214 |
(saturating $primitive:ident $ty:ident : $visit:ident) => { |
| 215 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 216 |
where |
| 217 |
E: Error, |
| 218 |
{ |
| 219 |
Ok(Saturating(v as $primitive)) |
| 220 |
} |
| 221 |
}; |
| 222 |
} |
| 223 |
|
| 224 |
macro_rules! num_as_copysign_self { |
| 225 |
($ty:ident : $visit:ident) => { |
| 226 |
#[inline] |
| 227 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 228 |
where |
| 229 |
E: Error, |
| 230 |
{ |
| 231 |
#[cfg(any(no_float_copysign, not(feature = "std")))] |
| 232 |
{ |
| 233 |
Ok(v as Self::Value) |
| 234 |
} |
| 235 |
|
| 236 |
#[cfg(all(not(no_float_copysign), feature = "std"))] |
| 237 |
{ |
| 238 |
// Preserve sign of NaN. The `as` produces a nondeterministic sign. |
| 239 |
let sign = if v.is_sign_positive() { 1.0 } else { -1.0 }; |
| 240 |
Ok((v as Self::Value).copysign(sign)) |
| 241 |
} |
| 242 |
} |
| 243 |
}; |
| 244 |
} |
| 245 |
|
| 246 |
macro_rules! int_to_int { |
| 247 |
($ty:ident : $visit:ident) => { |
| 248 |
#[inline] |
| 249 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 250 |
where |
| 251 |
E: Error, |
| 252 |
{ |
| 253 |
if Self::Value::min_value() as i64 <= v as i64 |
| 254 |
&& v as i64 <= Self::Value::max_value() as i64 |
| 255 |
{ |
| 256 |
Ok(v as Self::Value) |
| 257 |
} else { |
| 258 |
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) |
| 259 |
} |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
(nonzero $primitive:ident $ty:ident : $visit:ident) => { |
| 264 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 265 |
where |
| 266 |
E: Error, |
| 267 |
{ |
| 268 |
if $primitive::min_value() as i64 <= v as i64 |
| 269 |
&& v as i64 <= $primitive::max_value() as i64 |
| 270 |
{ |
| 271 |
if let Some(nonzero) = Self::Value::new(v as $primitive) { |
| 272 |
return Ok(nonzero); |
| 273 |
} |
| 274 |
} |
| 275 |
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
(saturating $primitive:ident $ty:ident : $visit:ident) => { |
| 280 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 281 |
where |
| 282 |
E: Error, |
| 283 |
{ |
| 284 |
if (v as i64) < $primitive::MIN as i64 { |
| 285 |
Ok(Saturating($primitive::MIN)) |
| 286 |
} else if ($primitive::MAX as i64) < v as i64 { |
| 287 |
Ok(Saturating($primitive::MAX)) |
| 288 |
} else { |
| 289 |
Ok(Saturating(v as $primitive)) |
| 290 |
} |
| 291 |
} |
| 292 |
}; |
| 293 |
} |
| 294 |
|
| 295 |
macro_rules! int_to_uint { |
| 296 |
($ty:ident : $visit:ident) => { |
| 297 |
#[inline] |
| 298 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 299 |
where |
| 300 |
E: Error, |
| 301 |
{ |
| 302 |
if 0 <= v && v as u64 <= Self::Value::max_value() as u64 { |
| 303 |
Ok(v as Self::Value) |
| 304 |
} else { |
| 305 |
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) |
| 306 |
} |
| 307 |
} |
| 308 |
}; |
| 309 |
|
| 310 |
(nonzero $primitive:ident $ty:ident : $visit:ident) => { |
| 311 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 312 |
where |
| 313 |
E: Error, |
| 314 |
{ |
| 315 |
if 0 < v && v as u64 <= $primitive::max_value() as u64 { |
| 316 |
if let Some(nonzero) = Self::Value::new(v as $primitive) { |
| 317 |
return Ok(nonzero); |
| 318 |
} |
| 319 |
} |
| 320 |
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
(saturating $primitive:ident $ty:ident : $visit:ident) => { |
| 325 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 326 |
where |
| 327 |
E: Error, |
| 328 |
{ |
| 329 |
if v < 0 { |
| 330 |
Ok(Saturating(0)) |
| 331 |
} else if ($primitive::MAX as u64) < v as u64 { |
| 332 |
Ok(Saturating($primitive::MAX)) |
| 333 |
} else { |
| 334 |
Ok(Saturating(v as $primitive)) |
| 335 |
} |
| 336 |
} |
| 337 |
}; |
| 338 |
} |
| 339 |
|
| 340 |
macro_rules! uint_to_self { |
| 341 |
($ty:ident : $visit:ident) => { |
| 342 |
#[inline] |
| 343 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 344 |
where |
| 345 |
E: Error, |
| 346 |
{ |
| 347 |
if v as u64 <= Self::Value::max_value() as u64 { |
| 348 |
Ok(v as Self::Value) |
| 349 |
} else { |
| 350 |
Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) |
| 351 |
} |
| 352 |
} |
| 353 |
}; |
| 354 |
|
| 355 |
(nonzero $primitive:ident $ty:ident : $visit:ident) => { |
| 356 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 357 |
where |
| 358 |
E: Error, |
| 359 |
{ |
| 360 |
if v as u64 <= $primitive::max_value() as u64 { |
| 361 |
if let Some(nonzero) = Self::Value::new(v as $primitive) { |
| 362 |
return Ok(nonzero); |
| 363 |
} |
| 364 |
} |
| 365 |
Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) |
| 366 |
} |
| 367 |
}; |
| 368 |
|
| 369 |
(saturating $primitive:ident $ty:ident : $visit:ident) => { |
| 370 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 371 |
where |
| 372 |
E: Error, |
| 373 |
{ |
| 374 |
if v as u64 <= $primitive::MAX as u64 { |
| 375 |
Ok(Saturating(v as $primitive)) |
| 376 |
} else { |
| 377 |
Ok(Saturating($primitive::MAX)) |
| 378 |
} |
| 379 |
} |
| 380 |
}; |
| 381 |
} |
| 382 |
|
| 383 |
impl_deserialize_num! { |
| 384 |
i8, NonZeroI8 cfg(not(no_num_nonzero_signed)), deserialize_i8 |
| 385 |
num_self!(i8:visit_i8); |
| 386 |
int_to_int!(i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 387 |
uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 388 |
} |
| 389 |
|
| 390 |
impl_deserialize_num! { |
| 391 |
i16, NonZeroI16 cfg(not(no_num_nonzero_signed)), deserialize_i16 |
| 392 |
num_self!(i16:visit_i16); |
| 393 |
num_as_self!(i8:visit_i8); |
| 394 |
int_to_int!(i32:visit_i32 i64:visit_i64); |
| 395 |
uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 396 |
} |
| 397 |
|
| 398 |
impl_deserialize_num! { |
| 399 |
i32, NonZeroI32 cfg(not(no_num_nonzero_signed)), deserialize_i32 |
| 400 |
num_self!(i32:visit_i32); |
| 401 |
num_as_self!(i8:visit_i8 i16:visit_i16); |
| 402 |
int_to_int!(i64:visit_i64); |
| 403 |
uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 404 |
} |
| 405 |
|
| 406 |
impl_deserialize_num! { |
| 407 |
i64, NonZeroI64 cfg(not(no_num_nonzero_signed)), deserialize_i64 |
| 408 |
num_self!(i64:visit_i64); |
| 409 |
num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32); |
| 410 |
uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 411 |
} |
| 412 |
|
| 413 |
impl_deserialize_num! { |
| 414 |
isize, NonZeroIsize cfg(not(no_num_nonzero_signed)), deserialize_i64 |
| 415 |
num_as_self!(i8:visit_i8 i16:visit_i16); |
| 416 |
int_to_int!(i32:visit_i32 i64:visit_i64); |
| 417 |
uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 418 |
} |
| 419 |
|
| 420 |
impl_deserialize_num! { |
| 421 |
u8, NonZeroU8, deserialize_u8 |
| 422 |
num_self!(u8:visit_u8); |
| 423 |
int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 424 |
uint_to_self!(u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 425 |
} |
| 426 |
|
| 427 |
impl_deserialize_num! { |
| 428 |
u16, NonZeroU16, deserialize_u16 |
| 429 |
num_self!(u16:visit_u16); |
| 430 |
num_as_self!(u8:visit_u8); |
| 431 |
int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 432 |
uint_to_self!(u32:visit_u32 u64:visit_u64); |
| 433 |
} |
| 434 |
|
| 435 |
impl_deserialize_num! { |
| 436 |
u32, NonZeroU32, deserialize_u32 |
| 437 |
num_self!(u32:visit_u32); |
| 438 |
num_as_self!(u8:visit_u8 u16:visit_u16); |
| 439 |
int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 440 |
uint_to_self!(u64:visit_u64); |
| 441 |
} |
| 442 |
|
| 443 |
impl_deserialize_num! { |
| 444 |
u64, NonZeroU64, deserialize_u64 |
| 445 |
num_self!(u64:visit_u64); |
| 446 |
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32); |
| 447 |
int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 448 |
} |
| 449 |
|
| 450 |
impl_deserialize_num! { |
| 451 |
usize, NonZeroUsize, deserialize_u64 |
| 452 |
num_as_self!(u8:visit_u8 u16:visit_u16); |
| 453 |
int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 454 |
uint_to_self!(u32:visit_u32 u64:visit_u64); |
| 455 |
} |
| 456 |
|
| 457 |
impl_deserialize_num! { |
| 458 |
f32, deserialize_f32 |
| 459 |
num_self!(f32:visit_f32); |
| 460 |
num_as_copysign_self!(f64:visit_f64); |
| 461 |
num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 462 |
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 463 |
} |
| 464 |
|
| 465 |
impl_deserialize_num! { |
| 466 |
f64, deserialize_f64 |
| 467 |
num_self!(f64:visit_f64); |
| 468 |
num_as_copysign_self!(f32:visit_f32); |
| 469 |
num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 470 |
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 471 |
} |
| 472 |
|
| 473 |
macro_rules! num_128 { |
| 474 |
($ty:ident : $visit:ident) => { |
| 475 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 476 |
where |
| 477 |
E: Error, |
| 478 |
{ |
| 479 |
if v as i128 >= Self::Value::min_value() as i128 |
| 480 |
&& v as u128 <= Self::Value::max_value() as u128 |
| 481 |
{ |
| 482 |
Ok(v as Self::Value) |
| 483 |
} else { |
| 484 |
Err(Error::invalid_value( |
| 485 |
Unexpected::Other(stringify!($ty)), |
| 486 |
&self, |
| 487 |
)) |
| 488 |
} |
| 489 |
} |
| 490 |
}; |
| 491 |
|
| 492 |
(nonzero $primitive:ident $ty:ident : $visit:ident) => { |
| 493 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 494 |
where |
| 495 |
E: Error, |
| 496 |
{ |
| 497 |
if v as i128 >= $primitive::min_value() as i128 |
| 498 |
&& v as u128 <= $primitive::max_value() as u128 |
| 499 |
{ |
| 500 |
if let Some(nonzero) = Self::Value::new(v as $primitive) { |
| 501 |
Ok(nonzero) |
| 502 |
} else { |
| 503 |
Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) |
| 504 |
} |
| 505 |
} else { |
| 506 |
Err(Error::invalid_value( |
| 507 |
Unexpected::Other(stringify!($ty)), |
| 508 |
&self, |
| 509 |
)) |
| 510 |
} |
| 511 |
} |
| 512 |
}; |
| 513 |
|
| 514 |
(saturating $primitive:ident $ty:ident : $visit:ident) => { |
| 515 |
fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> |
| 516 |
where |
| 517 |
E: Error, |
| 518 |
{ |
| 519 |
if (v as i128) < $primitive::MIN as i128 { |
| 520 |
Ok(Saturating($primitive::MIN)) |
| 521 |
} else if ($primitive::MAX as u128) < v as u128 { |
| 522 |
Ok(Saturating($primitive::MAX)) |
| 523 |
} else { |
| 524 |
Ok(Saturating(v as $primitive)) |
| 525 |
} |
| 526 |
} |
| 527 |
}; |
| 528 |
} |
| 529 |
|
| 530 |
impl_deserialize_num! { |
| 531 |
i128, NonZeroI128 cfg(not(no_num_nonzero_signed)), deserialize_i128 |
| 532 |
num_self!(i128:visit_i128); |
| 533 |
num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 534 |
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 535 |
num_128!(u128:visit_u128); |
| 536 |
} |
| 537 |
|
| 538 |
impl_deserialize_num! { |
| 539 |
u128, NonZeroU128, deserialize_u128 |
| 540 |
num_self!(u128:visit_u128); |
| 541 |
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); |
| 542 |
int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); |
| 543 |
num_128!(i128:visit_i128); |
| 544 |
} |
| 545 |
|
| 546 |
//////////////////////////////////////////////////////////////////////////////// |
| 547 |
|
| 548 |
struct CharVisitor; |
| 549 |
|
| 550 |
impl<'de> Visitor<'de> for CharVisitor { |
| 551 |
type Value = char; |
| 552 |
|
| 553 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 554 |
formatter.write_str("a character") |
| 555 |
} |
| 556 |
|
| 557 |
#[inline] |
| 558 |
fn visit_char<E>(self, v: char) -> Result<Self::Value, E> |
| 559 |
where |
| 560 |
E: Error, |
| 561 |
{ |
| 562 |
Ok(v) |
| 563 |
} |
| 564 |
|
| 565 |
#[inline] |
| 566 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 567 |
where |
| 568 |
E: Error, |
| 569 |
{ |
| 570 |
let mut iter = v.chars(); |
| 571 |
match (iter.next(), iter.next()) { |
| 572 |
(Some(c), None) => Ok(c), |
| 573 |
_ => Err(Error::invalid_value(Unexpected::Str(v), &self)), |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
impl<'de> Deserialize<'de> for char { |
| 579 |
#[inline] |
| 580 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 581 |
where |
| 582 |
D: Deserializer<'de>, |
| 583 |
{ |
| 584 |
deserializer.deserialize_char(CharVisitor) |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
//////////////////////////////////////////////////////////////////////////////// |
| 589 |
|
| 590 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 591 |
struct StringVisitor; |
| 592 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 593 |
struct StringInPlaceVisitor<'a>(&'a mut String); |
| 594 |
|
| 595 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 596 |
impl<'de> Visitor<'de> for StringVisitor { |
| 597 |
type Value = String; |
| 598 |
|
| 599 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 600 |
formatter.write_str("a string") |
| 601 |
} |
| 602 |
|
| 603 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 604 |
where |
| 605 |
E: Error, |
| 606 |
{ |
| 607 |
Ok(v.to_owned()) |
| 608 |
} |
| 609 |
|
| 610 |
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
| 611 |
where |
| 612 |
E: Error, |
| 613 |
{ |
| 614 |
Ok(v) |
| 615 |
} |
| 616 |
|
| 617 |
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 618 |
where |
| 619 |
E: Error, |
| 620 |
{ |
| 621 |
match str::from_utf8(v) { |
| 622 |
Ok(s) => Ok(s.to_owned()), |
| 623 |
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
| 628 |
where |
| 629 |
E: Error, |
| 630 |
{ |
| 631 |
match String::from_utf8(v) { |
| 632 |
Ok(s) => Ok(s), |
| 633 |
Err(e) => Err(Error::invalid_value( |
| 634 |
Unexpected::Bytes(&e.into_bytes()), |
| 635 |
&self, |
| 636 |
)), |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 642 |
impl<'a, 'de> Visitor<'de> for StringInPlaceVisitor<'a> { |
| 643 |
type Value = (); |
| 644 |
|
| 645 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 646 |
formatter.write_str("a string") |
| 647 |
} |
| 648 |
|
| 649 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 650 |
where |
| 651 |
E: Error, |
| 652 |
{ |
| 653 |
self.0.clear(); |
| 654 |
self.0.push_str(v); |
| 655 |
Ok(()) |
| 656 |
} |
| 657 |
|
| 658 |
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
| 659 |
where |
| 660 |
E: Error, |
| 661 |
{ |
| 662 |
*self.0 = v; |
| 663 |
Ok(()) |
| 664 |
} |
| 665 |
|
| 666 |
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 667 |
where |
| 668 |
E: Error, |
| 669 |
{ |
| 670 |
match str::from_utf8(v) { |
| 671 |
Ok(s) => { |
| 672 |
self.0.clear(); |
| 673 |
self.0.push_str(s); |
| 674 |
Ok(()) |
| 675 |
} |
| 676 |
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
| 681 |
where |
| 682 |
E: Error, |
| 683 |
{ |
| 684 |
match String::from_utf8(v) { |
| 685 |
Ok(s) => { |
| 686 |
*self.0 = s; |
| 687 |
Ok(()) |
| 688 |
} |
| 689 |
Err(e) => Err(Error::invalid_value( |
| 690 |
Unexpected::Bytes(&e.into_bytes()), |
| 691 |
&self, |
| 692 |
)), |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 698 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 699 |
impl<'de> Deserialize<'de> for String { |
| 700 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 701 |
where |
| 702 |
D: Deserializer<'de>, |
| 703 |
{ |
| 704 |
deserializer.deserialize_string(StringVisitor) |
| 705 |
} |
| 706 |
|
| 707 |
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> |
| 708 |
where |
| 709 |
D: Deserializer<'de>, |
| 710 |
{ |
| 711 |
deserializer.deserialize_string(StringInPlaceVisitor(place)) |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
//////////////////////////////////////////////////////////////////////////////// |
| 716 |
|
| 717 |
struct StrVisitor; |
| 718 |
|
| 719 |
impl<'a> Visitor<'a> for StrVisitor { |
| 720 |
type Value = &'a str; |
| 721 |
|
| 722 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 723 |
formatter.write_str("a borrowed string") |
| 724 |
} |
| 725 |
|
| 726 |
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> |
| 727 |
where |
| 728 |
E: Error, |
| 729 |
{ |
| 730 |
Ok(v) // so easy |
| 731 |
} |
| 732 |
|
| 733 |
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> |
| 734 |
where |
| 735 |
E: Error, |
| 736 |
{ |
| 737 |
str::from_utf8(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
impl<'de: 'a, 'a> Deserialize<'de> for &'a str { |
| 742 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 743 |
where |
| 744 |
D: Deserializer<'de>, |
| 745 |
{ |
| 746 |
deserializer.deserialize_str(StrVisitor) |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
//////////////////////////////////////////////////////////////////////////////// |
| 751 |
|
| 752 |
struct BytesVisitor; |
| 753 |
|
| 754 |
impl<'a> Visitor<'a> for BytesVisitor { |
| 755 |
type Value = &'a [u8]; |
| 756 |
|
| 757 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 758 |
formatter.write_str("a borrowed byte array") |
| 759 |
} |
| 760 |
|
| 761 |
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> |
| 762 |
where |
| 763 |
E: Error, |
| 764 |
{ |
| 765 |
Ok(v) |
| 766 |
} |
| 767 |
|
| 768 |
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> |
| 769 |
where |
| 770 |
E: Error, |
| 771 |
{ |
| 772 |
Ok(v.as_bytes()) |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] { |
| 777 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 778 |
where |
| 779 |
D: Deserializer<'de>, |
| 780 |
{ |
| 781 |
deserializer.deserialize_bytes(BytesVisitor) |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
//////////////////////////////////////////////////////////////////////////////// |
| 786 |
|
| 787 |
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
| 788 |
struct CStringVisitor; |
| 789 |
|
| 790 |
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
| 791 |
impl<'de> Visitor<'de> for CStringVisitor { |
| 792 |
type Value = CString; |
| 793 |
|
| 794 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 795 |
formatter.write_str("byte array") |
| 796 |
} |
| 797 |
|
| 798 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 799 |
where |
| 800 |
A: SeqAccess<'de>, |
| 801 |
{ |
| 802 |
let capacity = size_hint::cautious::<u8>(seq.size_hint()); |
| 803 |
let mut values = Vec::<u8>::with_capacity(capacity); |
| 804 |
|
| 805 |
while let Some(value) = tri!(seq.next_element()) { |
| 806 |
values.push(value); |
| 807 |
} |
| 808 |
|
| 809 |
CString::new(values).map_err(Error::custom) |
| 810 |
} |
| 811 |
|
| 812 |
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 813 |
where |
| 814 |
E: Error, |
| 815 |
{ |
| 816 |
CString::new(v).map_err(Error::custom) |
| 817 |
} |
| 818 |
|
| 819 |
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
| 820 |
where |
| 821 |
E: Error, |
| 822 |
{ |
| 823 |
CString::new(v).map_err(Error::custom) |
| 824 |
} |
| 825 |
|
| 826 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 827 |
where |
| 828 |
E: Error, |
| 829 |
{ |
| 830 |
CString::new(v).map_err(Error::custom) |
| 831 |
} |
| 832 |
|
| 833 |
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
| 834 |
where |
| 835 |
E: Error, |
| 836 |
{ |
| 837 |
CString::new(v).map_err(Error::custom) |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
| 842 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 843 |
impl<'de> Deserialize<'de> for CString { |
| 844 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 845 |
where |
| 846 |
D: Deserializer<'de>, |
| 847 |
{ |
| 848 |
deserializer.deserialize_byte_buf(CStringVisitor) |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
macro_rules! forwarded_impl { |
| 853 |
( |
| 854 |
$(#[$attr:meta])* |
| 855 |
($($id:ident),*), $ty:ty, $func:expr |
| 856 |
) => { |
| 857 |
$(#[$attr])* |
| 858 |
impl<'de $(, $id : Deserialize<'de>,)*> Deserialize<'de> for $ty { |
| 859 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 860 |
where |
| 861 |
D: Deserializer<'de>, |
| 862 |
{ |
| 863 |
Deserialize::deserialize(deserializer).map($func) |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
forwarded_impl! { |
| 870 |
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
| 871 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 872 |
(), Box<CStr>, CString::into_boxed_c_str |
| 873 |
} |
| 874 |
|
| 875 |
forwarded_impl! { |
| 876 |
(T), Reverse<T>, Reverse |
| 877 |
} |
| 878 |
|
| 879 |
//////////////////////////////////////////////////////////////////////////////// |
| 880 |
|
| 881 |
struct OptionVisitor<T> { |
| 882 |
marker: PhantomData<T>, |
| 883 |
} |
| 884 |
|
| 885 |
impl<'de, T> Visitor<'de> for OptionVisitor<T> |
| 886 |
where |
| 887 |
T: Deserialize<'de>, |
| 888 |
{ |
| 889 |
type Value = Option<T>; |
| 890 |
|
| 891 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 892 |
formatter.write_str("option") |
| 893 |
} |
| 894 |
|
| 895 |
#[inline] |
| 896 |
fn visit_unit<E>(self) -> Result<Self::Value, E> |
| 897 |
where |
| 898 |
E: Error, |
| 899 |
{ |
| 900 |
Ok(None) |
| 901 |
} |
| 902 |
|
| 903 |
#[inline] |
| 904 |
fn visit_none<E>(self) -> Result<Self::Value, E> |
| 905 |
where |
| 906 |
E: Error, |
| 907 |
{ |
| 908 |
Ok(None) |
| 909 |
} |
| 910 |
|
| 911 |
#[inline] |
| 912 |
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 913 |
where |
| 914 |
D: Deserializer<'de>, |
| 915 |
{ |
| 916 |
T::deserialize(deserializer).map(Some) |
| 917 |
} |
| 918 |
|
| 919 |
fn __private_visit_untagged_option<D>(self, deserializer: D) -> Result<Self::Value, ()> |
| 920 |
where |
| 921 |
D: Deserializer<'de>, |
| 922 |
{ |
| 923 |
Ok(T::deserialize(deserializer).ok()) |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
impl<'de, T> Deserialize<'de> for Option<T> |
| 928 |
where |
| 929 |
T: Deserialize<'de>, |
| 930 |
{ |
| 931 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 932 |
where |
| 933 |
D: Deserializer<'de>, |
| 934 |
{ |
| 935 |
deserializer.deserialize_option(OptionVisitor { |
| 936 |
marker: PhantomData, |
| 937 |
}) |
| 938 |
} |
| 939 |
|
| 940 |
// The Some variant's repr is opaque, so we can't play cute tricks with its |
| 941 |
// tag to have deserialize_in_place build the content in place unconditionally. |
| 942 |
// |
| 943 |
// FIXME: investigate whether branching on the old value being Some to |
| 944 |
// deserialize_in_place the value is profitable (probably data-dependent?) |
| 945 |
} |
| 946 |
|
| 947 |
//////////////////////////////////////////////////////////////////////////////// |
| 948 |
|
| 949 |
struct PhantomDataVisitor<T: ?Sized> { |
| 950 |
marker: PhantomData<T>, |
| 951 |
} |
| 952 |
|
| 953 |
impl<'de, T> Visitor<'de> for PhantomDataVisitor<T> |
| 954 |
where |
| 955 |
T: ?Sized, |
| 956 |
{ |
| 957 |
type Value = PhantomData<T>; |
| 958 |
|
| 959 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 960 |
formatter.write_str("unit") |
| 961 |
} |
| 962 |
|
| 963 |
#[inline] |
| 964 |
fn visit_unit<E>(self) -> Result<Self::Value, E> |
| 965 |
where |
| 966 |
E: Error, |
| 967 |
{ |
| 968 |
Ok(PhantomData) |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
impl<'de, T> Deserialize<'de> for PhantomData<T> |
| 973 |
where |
| 974 |
T: ?Sized, |
| 975 |
{ |
| 976 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 977 |
where |
| 978 |
D: Deserializer<'de>, |
| 979 |
{ |
| 980 |
let visitor = PhantomDataVisitor { |
| 981 |
marker: PhantomData, |
| 982 |
}; |
| 983 |
deserializer.deserialize_unit_struct("PhantomData", visitor) |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
//////////////////////////////////////////////////////////////////////////////// |
| 988 |
|
| 989 |
macro_rules! seq_impl { |
| 990 |
( |
| 991 |
$(#[$attr:meta])* |
| 992 |
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>, |
| 993 |
$access:ident, |
| 994 |
$clear:expr, |
| 995 |
$with_capacity:expr, |
| 996 |
$reserve:expr, |
| 997 |
$insert:expr |
| 998 |
) => { |
| 999 |
$(#[$attr])* |
| 1000 |
impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*> |
| 1001 |
where |
| 1002 |
T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, |
| 1003 |
$($typaram: $bound1 $(+ $bound2)*,)* |
| 1004 |
{ |
| 1005 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1006 |
where |
| 1007 |
D: Deserializer<'de>, |
| 1008 |
{ |
| 1009 |
struct SeqVisitor<T $(, $typaram)*> { |
| 1010 |
marker: PhantomData<$ty<T $(, $typaram)*>>, |
| 1011 |
} |
| 1012 |
|
| 1013 |
impl<'de, T $(, $typaram)*> Visitor<'de> for SeqVisitor<T $(, $typaram)*> |
| 1014 |
where |
| 1015 |
T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, |
| 1016 |
$($typaram: $bound1 $(+ $bound2)*,)* |
| 1017 |
{ |
| 1018 |
type Value = $ty<T $(, $typaram)*>; |
| 1019 |
|
| 1020 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1021 |
formatter.write_str("a sequence") |
| 1022 |
} |
| 1023 |
|
| 1024 |
#[inline] |
| 1025 |
fn visit_seq<A>(self, mut $access: A) -> Result<Self::Value, A::Error> |
| 1026 |
where |
| 1027 |
A: SeqAccess<'de>, |
| 1028 |
{ |
| 1029 |
let mut values = $with_capacity; |
| 1030 |
|
| 1031 |
while let Some(value) = tri!($access.next_element()) { |
| 1032 |
$insert(&mut values, value); |
| 1033 |
} |
| 1034 |
|
| 1035 |
Ok(values) |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
let visitor = SeqVisitor { marker: PhantomData }; |
| 1040 |
deserializer.deserialize_seq(visitor) |
| 1041 |
} |
| 1042 |
|
| 1043 |
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> |
| 1044 |
where |
| 1045 |
D: Deserializer<'de>, |
| 1046 |
{ |
| 1047 |
struct SeqInPlaceVisitor<'a, T: 'a $(, $typaram: 'a)*>(&'a mut $ty<T $(, $typaram)*>); |
| 1048 |
|
| 1049 |
impl<'a, 'de, T $(, $typaram)*> Visitor<'de> for SeqInPlaceVisitor<'a, T $(, $typaram)*> |
| 1050 |
where |
| 1051 |
T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, |
| 1052 |
$($typaram: $bound1 $(+ $bound2)*,)* |
| 1053 |
{ |
| 1054 |
type Value = (); |
| 1055 |
|
| 1056 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1057 |
formatter.write_str("a sequence") |
| 1058 |
} |
| 1059 |
|
| 1060 |
#[inline] |
| 1061 |
fn visit_seq<A>(mut self, mut $access: A) -> Result<Self::Value, A::Error> |
| 1062 |
where |
| 1063 |
A: SeqAccess<'de>, |
| 1064 |
{ |
| 1065 |
$clear(&mut self.0); |
| 1066 |
$reserve(&mut self.0, size_hint::cautious::<T>($access.size_hint())); |
| 1067 |
|
| 1068 |
// FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList) |
| 1069 |
while let Some(value) = tri!($access.next_element()) { |
| 1070 |
$insert(&mut self.0, value); |
| 1071 |
} |
| 1072 |
|
| 1073 |
Ok(()) |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
deserializer.deserialize_seq(SeqInPlaceVisitor(place)) |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
// Dummy impl of reserve |
| 1084 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1085 |
fn nop_reserve<T>(_seq: T, _n: usize) {} |
| 1086 |
|
| 1087 |
seq_impl!( |
| 1088 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1089 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1090 |
BinaryHeap<T: Ord>, |
| 1091 |
seq, |
| 1092 |
BinaryHeap::clear, |
| 1093 |
BinaryHeap::with_capacity(size_hint::cautious::<T>(seq.size_hint())), |
| 1094 |
BinaryHeap::reserve, |
| 1095 |
BinaryHeap::push |
| 1096 |
); |
| 1097 |
|
| 1098 |
seq_impl!( |
| 1099 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1100 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1101 |
BTreeSet<T: Eq + Ord>, |
| 1102 |
seq, |
| 1103 |
BTreeSet::clear, |
| 1104 |
BTreeSet::new(), |
| 1105 |
nop_reserve, |
| 1106 |
BTreeSet::insert |
| 1107 |
); |
| 1108 |
|
| 1109 |
seq_impl!( |
| 1110 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1111 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1112 |
LinkedList<T>, |
| 1113 |
seq, |
| 1114 |
LinkedList::clear, |
| 1115 |
LinkedList::new(), |
| 1116 |
nop_reserve, |
| 1117 |
LinkedList::push_back |
| 1118 |
); |
| 1119 |
|
| 1120 |
seq_impl!( |
| 1121 |
#[cfg(feature = "std")] |
| 1122 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1123 |
HashSet<T: Eq + Hash, S: BuildHasher + Default>, |
| 1124 |
seq, |
| 1125 |
HashSet::clear, |
| 1126 |
HashSet::with_capacity_and_hasher(size_hint::cautious::<T>(seq.size_hint()), S::default()), |
| 1127 |
HashSet::reserve, |
| 1128 |
HashSet::insert |
| 1129 |
); |
| 1130 |
|
| 1131 |
seq_impl!( |
| 1132 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1133 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1134 |
VecDeque<T>, |
| 1135 |
seq, |
| 1136 |
VecDeque::clear, |
| 1137 |
VecDeque::with_capacity(size_hint::cautious::<T>(seq.size_hint())), |
| 1138 |
VecDeque::reserve, |
| 1139 |
VecDeque::push_back |
| 1140 |
); |
| 1141 |
|
| 1142 |
//////////////////////////////////////////////////////////////////////////////// |
| 1143 |
|
| 1144 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1145 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1146 |
impl<'de, T> Deserialize<'de> for Vec<T> |
| 1147 |
where |
| 1148 |
T: Deserialize<'de>, |
| 1149 |
{ |
| 1150 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1151 |
where |
| 1152 |
D: Deserializer<'de>, |
| 1153 |
{ |
| 1154 |
struct VecVisitor<T> { |
| 1155 |
marker: PhantomData<T>, |
| 1156 |
} |
| 1157 |
|
| 1158 |
impl<'de, T> Visitor<'de> for VecVisitor<T> |
| 1159 |
where |
| 1160 |
T: Deserialize<'de>, |
| 1161 |
{ |
| 1162 |
type Value = Vec<T>; |
| 1163 |
|
| 1164 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1165 |
formatter.write_str("a sequence") |
| 1166 |
} |
| 1167 |
|
| 1168 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 1169 |
where |
| 1170 |
A: SeqAccess<'de>, |
| 1171 |
{ |
| 1172 |
let capacity = size_hint::cautious::<T>(seq.size_hint()); |
| 1173 |
let mut values = Vec::<T>::with_capacity(capacity); |
| 1174 |
|
| 1175 |
while let Some(value) = tri!(seq.next_element()) { |
| 1176 |
values.push(value); |
| 1177 |
} |
| 1178 |
|
| 1179 |
Ok(values) |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
let visitor = VecVisitor { |
| 1184 |
marker: PhantomData, |
| 1185 |
}; |
| 1186 |
deserializer.deserialize_seq(visitor) |
| 1187 |
} |
| 1188 |
|
| 1189 |
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> |
| 1190 |
where |
| 1191 |
D: Deserializer<'de>, |
| 1192 |
{ |
| 1193 |
struct VecInPlaceVisitor<'a, T: 'a>(&'a mut Vec<T>); |
| 1194 |
|
| 1195 |
impl<'a, 'de, T> Visitor<'de> for VecInPlaceVisitor<'a, T> |
| 1196 |
where |
| 1197 |
T: Deserialize<'de>, |
| 1198 |
{ |
| 1199 |
type Value = (); |
| 1200 |
|
| 1201 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1202 |
formatter.write_str("a sequence") |
| 1203 |
} |
| 1204 |
|
| 1205 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 1206 |
where |
| 1207 |
A: SeqAccess<'de>, |
| 1208 |
{ |
| 1209 |
let hint = size_hint::cautious::<T>(seq.size_hint()); |
| 1210 |
if let Some(additional) = hint.checked_sub(self.0.len()) { |
| 1211 |
self.0.reserve(additional); |
| 1212 |
} |
| 1213 |
|
| 1214 |
for i in 0..self.0.len() { |
| 1215 |
let next = { |
| 1216 |
let next_place = InPlaceSeed(&mut self.0[i]); |
| 1217 |
tri!(seq.next_element_seed(next_place)) |
| 1218 |
}; |
| 1219 |
if next.is_none() { |
| 1220 |
self.0.truncate(i); |
| 1221 |
return Ok(()); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
while let Some(value) = tri!(seq.next_element()) { |
| 1226 |
self.0.push(value); |
| 1227 |
} |
| 1228 |
|
| 1229 |
Ok(()) |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
deserializer.deserialize_seq(VecInPlaceVisitor(place)) |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
//////////////////////////////////////////////////////////////////////////////// |
| 1238 |
|
| 1239 |
struct ArrayVisitor<A> { |
| 1240 |
marker: PhantomData<A>, |
| 1241 |
} |
| 1242 |
struct ArrayInPlaceVisitor<'a, A: 'a>(&'a mut A); |
| 1243 |
|
| 1244 |
impl<A> ArrayVisitor<A> { |
| 1245 |
fn new() -> Self { |
| 1246 |
ArrayVisitor { |
| 1247 |
marker: PhantomData, |
| 1248 |
} |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> { |
| 1253 |
type Value = [T; 0]; |
| 1254 |
|
| 1255 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1256 |
formatter.write_str("an empty array") |
| 1257 |
} |
| 1258 |
|
| 1259 |
#[inline] |
| 1260 |
fn visit_seq<A>(self, _: A) -> Result<Self::Value, A::Error> |
| 1261 |
where |
| 1262 |
A: SeqAccess<'de>, |
| 1263 |
{ |
| 1264 |
Ok([]) |
| 1265 |
} |
| 1266 |
} |
| 1267 |
|
| 1268 |
// Does not require T: Deserialize<'de>. |
| 1269 |
impl<'de, T> Deserialize<'de> for [T; 0] { |
| 1270 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1271 |
where |
| 1272 |
D: Deserializer<'de>, |
| 1273 |
{ |
| 1274 |
deserializer.deserialize_tuple(0, ArrayVisitor::<[T; 0]>::new()) |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
macro_rules! array_impls { |
| 1279 |
($($len:expr => ($($n:tt)+))+) => { |
| 1280 |
$( |
| 1281 |
impl<'de, T> Visitor<'de> for ArrayVisitor<[T; $len]> |
| 1282 |
where |
| 1283 |
T: Deserialize<'de>, |
| 1284 |
{ |
| 1285 |
type Value = [T; $len]; |
| 1286 |
|
| 1287 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1288 |
formatter.write_str(concat!("an array of length ", $len)) |
| 1289 |
} |
| 1290 |
|
| 1291 |
#[inline] |
| 1292 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 1293 |
where |
| 1294 |
A: SeqAccess<'de>, |
| 1295 |
{ |
| 1296 |
Ok([$( |
| 1297 |
match tri!(seq.next_element()) { |
| 1298 |
Some(val) => val, |
| 1299 |
None => return Err(Error::invalid_length($n, &self)), |
| 1300 |
} |
| 1301 |
),+]) |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; $len]> |
| 1306 |
where |
| 1307 |
T: Deserialize<'de>, |
| 1308 |
{ |
| 1309 |
type Value = (); |
| 1310 |
|
| 1311 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1312 |
formatter.write_str(concat!("an array of length ", $len)) |
| 1313 |
} |
| 1314 |
|
| 1315 |
#[inline] |
| 1316 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 1317 |
where |
| 1318 |
A: SeqAccess<'de>, |
| 1319 |
{ |
| 1320 |
let mut fail_idx = None; |
| 1321 |
for (idx, dest) in self.0[..].iter_mut().enumerate() { |
| 1322 |
if tri!(seq.next_element_seed(InPlaceSeed(dest))).is_none() { |
| 1323 |
fail_idx = Some(idx); |
| 1324 |
break; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
if let Some(idx) = fail_idx { |
| 1328 |
return Err(Error::invalid_length(idx, &self)); |
| 1329 |
} |
| 1330 |
Ok(()) |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
impl<'de, T> Deserialize<'de> for [T; $len] |
| 1335 |
where |
| 1336 |
T: Deserialize<'de>, |
| 1337 |
{ |
| 1338 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1339 |
where |
| 1340 |
D: Deserializer<'de>, |
| 1341 |
{ |
| 1342 |
deserializer.deserialize_tuple($len, ArrayVisitor::<[T; $len]>::new()) |
| 1343 |
} |
| 1344 |
|
| 1345 |
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> |
| 1346 |
where |
| 1347 |
D: Deserializer<'de>, |
| 1348 |
{ |
| 1349 |
deserializer.deserialize_tuple($len, ArrayInPlaceVisitor(place)) |
| 1350 |
} |
| 1351 |
} |
| 1352 |
)+ |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
array_impls! { |
| 1357 |
1 => (0) |
| 1358 |
2 => (0 1) |
| 1359 |
3 => (0 1 2) |
| 1360 |
4 => (0 1 2 3) |
| 1361 |
5 => (0 1 2 3 4) |
| 1362 |
6 => (0 1 2 3 4 5) |
| 1363 |
7 => (0 1 2 3 4 5 6) |
| 1364 |
8 => (0 1 2 3 4 5 6 7) |
| 1365 |
9 => (0 1 2 3 4 5 6 7 8) |
| 1366 |
10 => (0 1 2 3 4 5 6 7 8 9) |
| 1367 |
11 => (0 1 2 3 4 5 6 7 8 9 10) |
| 1368 |
12 => (0 1 2 3 4 5 6 7 8 9 10 11) |
| 1369 |
13 => (0 1 2 3 4 5 6 7 8 9 10 11 12) |
| 1370 |
14 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13) |
| 1371 |
15 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14) |
| 1372 |
16 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) |
| 1373 |
17 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16) |
| 1374 |
18 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17) |
| 1375 |
19 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) |
| 1376 |
20 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19) |
| 1377 |
21 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) |
| 1378 |
22 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21) |
| 1379 |
23 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22) |
| 1380 |
24 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23) |
| 1381 |
25 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) |
| 1382 |
26 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25) |
| 1383 |
27 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26) |
| 1384 |
28 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27) |
| 1385 |
29 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28) |
| 1386 |
30 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29) |
| 1387 |
31 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30) |
| 1388 |
32 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) |
| 1389 |
} |
| 1390 |
|
| 1391 |
//////////////////////////////////////////////////////////////////////////////// |
| 1392 |
|
| 1393 |
macro_rules! tuple_impls { |
| 1394 |
($($len:tt => ($($n:tt $name:ident)+))+) => { |
| 1395 |
$( |
| 1396 |
impl<'de, $($name: Deserialize<'de>),+> Deserialize<'de> for ($($name,)+) { |
| 1397 |
#[inline] |
| 1398 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1399 |
where |
| 1400 |
D: Deserializer<'de>, |
| 1401 |
{ |
| 1402 |
struct TupleVisitor<$($name,)+> { |
| 1403 |
marker: PhantomData<($($name,)+)>, |
| 1404 |
} |
| 1405 |
|
| 1406 |
impl<'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleVisitor<$($name,)+> { |
| 1407 |
type Value = ($($name,)+); |
| 1408 |
|
| 1409 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1410 |
formatter.write_str(concat!("a tuple of size ", $len)) |
| 1411 |
} |
| 1412 |
|
| 1413 |
#[inline] |
| 1414 |
#[allow(non_snake_case)] |
| 1415 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 1416 |
where |
| 1417 |
A: SeqAccess<'de>, |
| 1418 |
{ |
| 1419 |
$( |
| 1420 |
let $name = match tri!(seq.next_element()) { |
| 1421 |
Some(value) => value, |
| 1422 |
None => return Err(Error::invalid_length($n, &self)), |
| 1423 |
}; |
| 1424 |
)+ |
| 1425 |
|
| 1426 |
Ok(($($name,)+)) |
| 1427 |
} |
| 1428 |
} |
| 1429 |
|
| 1430 |
deserializer.deserialize_tuple($len, TupleVisitor { marker: PhantomData }) |
| 1431 |
} |
| 1432 |
|
| 1433 |
#[inline] |
| 1434 |
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> |
| 1435 |
where |
| 1436 |
D: Deserializer<'de>, |
| 1437 |
{ |
| 1438 |
struct TupleInPlaceVisitor<'a, $($name: 'a,)+>(&'a mut ($($name,)+)); |
| 1439 |
|
| 1440 |
impl<'a, 'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleInPlaceVisitor<'a, $($name,)+> { |
| 1441 |
type Value = (); |
| 1442 |
|
| 1443 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1444 |
formatter.write_str(concat!("a tuple of size ", $len)) |
| 1445 |
} |
| 1446 |
|
| 1447 |
#[inline] |
| 1448 |
#[allow(non_snake_case)] |
| 1449 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 1450 |
where |
| 1451 |
A: SeqAccess<'de>, |
| 1452 |
{ |
| 1453 |
$( |
| 1454 |
if tri!(seq.next_element_seed(InPlaceSeed(&mut (self.0).$n))).is_none() { |
| 1455 |
return Err(Error::invalid_length($n, &self)); |
| 1456 |
} |
| 1457 |
)+ |
| 1458 |
|
| 1459 |
Ok(()) |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
deserializer.deserialize_tuple($len, TupleInPlaceVisitor(place)) |
| 1464 |
} |
| 1465 |
} |
| 1466 |
)+ |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
tuple_impls! { |
| 1471 |
1 => (0 T0) |
| 1472 |
2 => (0 T0 1 T1) |
| 1473 |
3 => (0 T0 1 T1 2 T2) |
| 1474 |
4 => (0 T0 1 T1 2 T2 3 T3) |
| 1475 |
5 => (0 T0 1 T1 2 T2 3 T3 4 T4) |
| 1476 |
6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5) |
| 1477 |
7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6) |
| 1478 |
8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7) |
| 1479 |
9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8) |
| 1480 |
10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9) |
| 1481 |
11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10) |
| 1482 |
12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11) |
| 1483 |
13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12) |
| 1484 |
14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13) |
| 1485 |
15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14) |
| 1486 |
16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15) |
| 1487 |
} |
| 1488 |
|
| 1489 |
//////////////////////////////////////////////////////////////////////////////// |
| 1490 |
|
| 1491 |
macro_rules! map_impl { |
| 1492 |
( |
| 1493 |
$(#[$attr:meta])* |
| 1494 |
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>, |
| 1495 |
$access:ident, |
| 1496 |
$with_capacity:expr, |
| 1497 |
) => { |
| 1498 |
$(#[$attr])* |
| 1499 |
impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*> |
| 1500 |
where |
| 1501 |
K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, |
| 1502 |
V: Deserialize<'de>, |
| 1503 |
$($typaram: $bound1 $(+ $bound2)*),* |
| 1504 |
{ |
| 1505 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1506 |
where |
| 1507 |
D: Deserializer<'de>, |
| 1508 |
{ |
| 1509 |
struct MapVisitor<K, V $(, $typaram)*> { |
| 1510 |
marker: PhantomData<$ty<K, V $(, $typaram)*>>, |
| 1511 |
} |
| 1512 |
|
| 1513 |
impl<'de, K, V $(, $typaram)*> Visitor<'de> for MapVisitor<K, V $(, $typaram)*> |
| 1514 |
where |
| 1515 |
K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, |
| 1516 |
V: Deserialize<'de>, |
| 1517 |
$($typaram: $bound1 $(+ $bound2)*),* |
| 1518 |
{ |
| 1519 |
type Value = $ty<K, V $(, $typaram)*>; |
| 1520 |
|
| 1521 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1522 |
formatter.write_str("a map") |
| 1523 |
} |
| 1524 |
|
| 1525 |
#[inline] |
| 1526 |
fn visit_map<A>(self, mut $access: A) -> Result<Self::Value, A::Error> |
| 1527 |
where |
| 1528 |
A: MapAccess<'de>, |
| 1529 |
{ |
| 1530 |
let mut values = $with_capacity; |
| 1531 |
|
| 1532 |
while let Some((key, value)) = tri!($access.next_entry()) { |
| 1533 |
values.insert(key, value); |
| 1534 |
} |
| 1535 |
|
| 1536 |
Ok(values) |
| 1537 |
} |
| 1538 |
} |
| 1539 |
|
| 1540 |
let visitor = MapVisitor { marker: PhantomData }; |
| 1541 |
deserializer.deserialize_map(visitor) |
| 1542 |
} |
| 1543 |
} |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
map_impl! { |
| 1548 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1549 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1550 |
BTreeMap<K: Ord, V>, |
| 1551 |
map, |
| 1552 |
BTreeMap::new(), |
| 1553 |
} |
| 1554 |
|
| 1555 |
map_impl! { |
| 1556 |
#[cfg(feature = "std")] |
| 1557 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1558 |
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>, |
| 1559 |
map, |
| 1560 |
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default()), |
| 1561 |
} |
| 1562 |
|
| 1563 |
//////////////////////////////////////////////////////////////////////////////// |
| 1564 |
|
| 1565 |
macro_rules! parse_ip_impl { |
| 1566 |
( |
| 1567 |
$(#[$attr:meta])* |
| 1568 |
$ty:ty, $expecting:expr, $size:tt |
| 1569 |
) => { |
| 1570 |
$(#[$attr])* |
| 1571 |
impl<'de> Deserialize<'de> for $ty { |
| 1572 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1573 |
where |
| 1574 |
D: Deserializer<'de>, |
| 1575 |
{ |
| 1576 |
if deserializer.is_human_readable() { |
| 1577 |
deserializer.deserialize_str(FromStrVisitor::new($expecting)) |
| 1578 |
} else { |
| 1579 |
<[u8; $size]>::deserialize(deserializer).map(<$ty>::from) |
| 1580 |
} |
| 1581 |
} |
| 1582 |
} |
| 1583 |
}; |
| 1584 |
} |
| 1585 |
|
| 1586 |
#[cfg(feature = "std")] |
| 1587 |
macro_rules! variant_identifier { |
| 1588 |
( |
| 1589 |
$name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*) |
| 1590 |
$expecting_message:expr, |
| 1591 |
$variants_name:ident |
| 1592 |
) => { |
| 1593 |
enum $name_kind { |
| 1594 |
$($variant),* |
| 1595 |
} |
| 1596 |
|
| 1597 |
static $variants_name: &[&str] = &[$(stringify!($variant)),*]; |
| 1598 |
|
| 1599 |
impl<'de> Deserialize<'de> for $name_kind { |
| 1600 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1601 |
where |
| 1602 |
D: Deserializer<'de>, |
| 1603 |
{ |
| 1604 |
struct KindVisitor; |
| 1605 |
|
| 1606 |
impl<'de> Visitor<'de> for KindVisitor { |
| 1607 |
type Value = $name_kind; |
| 1608 |
|
| 1609 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1610 |
formatter.write_str($expecting_message) |
| 1611 |
} |
| 1612 |
|
| 1613 |
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
| 1614 |
where |
| 1615 |
E: Error, |
| 1616 |
{ |
| 1617 |
match value { |
| 1618 |
$( |
| 1619 |
$index => Ok($name_kind :: $variant), |
| 1620 |
)* |
| 1621 |
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),), |
| 1622 |
} |
| 1623 |
} |
| 1624 |
|
| 1625 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 1626 |
where |
| 1627 |
E: Error, |
| 1628 |
{ |
| 1629 |
match value { |
| 1630 |
$( |
| 1631 |
stringify!($variant) => Ok($name_kind :: $variant), |
| 1632 |
)* |
| 1633 |
_ => Err(Error::unknown_variant(value, $variants_name)), |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 1638 |
where |
| 1639 |
E: Error, |
| 1640 |
{ |
| 1641 |
match value { |
| 1642 |
$( |
| 1643 |
$bytes => Ok($name_kind :: $variant), |
| 1644 |
)* |
| 1645 |
_ => { |
| 1646 |
match str::from_utf8(value) { |
| 1647 |
Ok(value) => Err(Error::unknown_variant(value, $variants_name)), |
| 1648 |
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)), |
| 1649 |
} |
| 1650 |
} |
| 1651 |
} |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
deserializer.deserialize_identifier(KindVisitor) |
| 1656 |
} |
| 1657 |
} |
| 1658 |
} |
| 1659 |
} |
| 1660 |
|
| 1661 |
#[cfg(feature = "std")] |
| 1662 |
macro_rules! deserialize_enum { |
| 1663 |
( |
| 1664 |
$name:ident $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*) |
| 1665 |
$expecting_message:expr, |
| 1666 |
$deserializer:expr |
| 1667 |
) => { |
| 1668 |
variant_identifier! { |
| 1669 |
$name_kind ($($variant; $bytes; $index),*) |
| 1670 |
$expecting_message, |
| 1671 |
VARIANTS |
| 1672 |
} |
| 1673 |
|
| 1674 |
struct EnumVisitor; |
| 1675 |
impl<'de> Visitor<'de> for EnumVisitor { |
| 1676 |
type Value = $name; |
| 1677 |
|
| 1678 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1679 |
formatter.write_str(concat!("a ", stringify!($name))) |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 1684 |
where |
| 1685 |
A: EnumAccess<'de>, |
| 1686 |
{ |
| 1687 |
match tri!(data.variant()) { |
| 1688 |
$( |
| 1689 |
($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant), |
| 1690 |
)* |
| 1691 |
} |
| 1692 |
} |
| 1693 |
} |
| 1694 |
$deserializer.deserialize_enum(stringify!($name), VARIANTS, EnumVisitor) |
| 1695 |
} |
| 1696 |
} |
| 1697 |
|
| 1698 |
#[cfg(feature = "std")] |
| 1699 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1700 |
impl<'de> Deserialize<'de> for net::IpAddr { |
| 1701 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1702 |
where |
| 1703 |
D: Deserializer<'de>, |
| 1704 |
{ |
| 1705 |
if deserializer.is_human_readable() { |
| 1706 |
deserializer.deserialize_str(FromStrVisitor::new("IP address")) |
| 1707 |
} else { |
| 1708 |
use crate::lib::net::IpAddr; |
| 1709 |
deserialize_enum! { |
| 1710 |
IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) |
| 1711 |
"`V4` or `V6`", |
| 1712 |
deserializer |
| 1713 |
} |
| 1714 |
} |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
parse_ip_impl! { |
| 1719 |
#[cfg(feature = "std")] |
| 1720 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1721 |
net::Ipv4Addr, "IPv4 address", 4 |
| 1722 |
} |
| 1723 |
|
| 1724 |
parse_ip_impl! { |
| 1725 |
#[cfg(feature = "std")] |
| 1726 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1727 |
net::Ipv6Addr, "IPv6 address", 16 |
| 1728 |
} |
| 1729 |
|
| 1730 |
macro_rules! parse_socket_impl { |
| 1731 |
( |
| 1732 |
$(#[$attr:meta])* |
| 1733 |
$ty:ty, $expecting:tt, |
| 1734 |
$new:expr, |
| 1735 |
) => { |
| 1736 |
$(#[$attr])* |
| 1737 |
impl<'de> Deserialize<'de> for $ty { |
| 1738 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1739 |
where |
| 1740 |
D: Deserializer<'de>, |
| 1741 |
{ |
| 1742 |
if deserializer.is_human_readable() { |
| 1743 |
deserializer.deserialize_str(FromStrVisitor::new($expecting)) |
| 1744 |
} else { |
| 1745 |
<(_, u16)>::deserialize(deserializer).map($new) |
| 1746 |
} |
| 1747 |
} |
| 1748 |
} |
| 1749 |
}; |
| 1750 |
} |
| 1751 |
|
| 1752 |
#[cfg(feature = "std")] |
| 1753 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1754 |
impl<'de> Deserialize<'de> for net::SocketAddr { |
| 1755 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1756 |
where |
| 1757 |
D: Deserializer<'de>, |
| 1758 |
{ |
| 1759 |
if deserializer.is_human_readable() { |
| 1760 |
deserializer.deserialize_str(FromStrVisitor::new("socket address")) |
| 1761 |
} else { |
| 1762 |
use crate::lib::net::SocketAddr; |
| 1763 |
deserialize_enum! { |
| 1764 |
SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) |
| 1765 |
"`V4` or `V6`", |
| 1766 |
deserializer |
| 1767 |
} |
| 1768 |
} |
| 1769 |
} |
| 1770 |
} |
| 1771 |
|
| 1772 |
parse_socket_impl! { |
| 1773 |
#[cfg(feature = "std")] |
| 1774 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1775 |
net::SocketAddrV4, "IPv4 socket address", |
| 1776 |
|(ip, port)| net::SocketAddrV4::new(ip, port), |
| 1777 |
} |
| 1778 |
|
| 1779 |
parse_socket_impl! { |
| 1780 |
#[cfg(feature = "std")] |
| 1781 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1782 |
net::SocketAddrV6, "IPv6 socket address", |
| 1783 |
|(ip, port)| net::SocketAddrV6::new(ip, port, 0, 0), |
| 1784 |
} |
| 1785 |
|
| 1786 |
//////////////////////////////////////////////////////////////////////////////// |
| 1787 |
|
| 1788 |
#[cfg(feature = "std")] |
| 1789 |
struct PathVisitor; |
| 1790 |
|
| 1791 |
#[cfg(feature = "std")] |
| 1792 |
impl<'a> Visitor<'a> for PathVisitor { |
| 1793 |
type Value = &'a Path; |
| 1794 |
|
| 1795 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1796 |
formatter.write_str("a borrowed path") |
| 1797 |
} |
| 1798 |
|
| 1799 |
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> |
| 1800 |
where |
| 1801 |
E: Error, |
| 1802 |
{ |
| 1803 |
Ok(v.as_ref()) |
| 1804 |
} |
| 1805 |
|
| 1806 |
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> |
| 1807 |
where |
| 1808 |
E: Error, |
| 1809 |
{ |
| 1810 |
str::from_utf8(v) |
| 1811 |
.map(AsRef::as_ref) |
| 1812 |
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) |
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
#[cfg(feature = "std")] |
| 1817 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1818 |
impl<'de: 'a, 'a> Deserialize<'de> for &'a Path { |
| 1819 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1820 |
where |
| 1821 |
D: Deserializer<'de>, |
| 1822 |
{ |
| 1823 |
deserializer.deserialize_str(PathVisitor) |
| 1824 |
} |
| 1825 |
} |
| 1826 |
|
| 1827 |
#[cfg(feature = "std")] |
| 1828 |
struct PathBufVisitor; |
| 1829 |
|
| 1830 |
#[cfg(feature = "std")] |
| 1831 |
impl<'de> Visitor<'de> for PathBufVisitor { |
| 1832 |
type Value = PathBuf; |
| 1833 |
|
| 1834 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1835 |
formatter.write_str("path string") |
| 1836 |
} |
| 1837 |
|
| 1838 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 1839 |
where |
| 1840 |
E: Error, |
| 1841 |
{ |
| 1842 |
Ok(From::from(v)) |
| 1843 |
} |
| 1844 |
|
| 1845 |
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
| 1846 |
where |
| 1847 |
E: Error, |
| 1848 |
{ |
| 1849 |
Ok(From::from(v)) |
| 1850 |
} |
| 1851 |
|
| 1852 |
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 1853 |
where |
| 1854 |
E: Error, |
| 1855 |
{ |
| 1856 |
str::from_utf8(v) |
| 1857 |
.map(From::from) |
| 1858 |
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) |
| 1859 |
} |
| 1860 |
|
| 1861 |
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
| 1862 |
where |
| 1863 |
E: Error, |
| 1864 |
{ |
| 1865 |
String::from_utf8(v) |
| 1866 |
.map(From::from) |
| 1867 |
.map_err(|e| Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self)) |
| 1868 |
} |
| 1869 |
} |
| 1870 |
|
| 1871 |
#[cfg(feature = "std")] |
| 1872 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1873 |
impl<'de> Deserialize<'de> for PathBuf { |
| 1874 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1875 |
where |
| 1876 |
D: Deserializer<'de>, |
| 1877 |
{ |
| 1878 |
deserializer.deserialize_string(PathBufVisitor) |
| 1879 |
} |
| 1880 |
} |
| 1881 |
|
| 1882 |
forwarded_impl! { |
| 1883 |
#[cfg(feature = "std")] |
| 1884 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 1885 |
(), Box<Path>, PathBuf::into_boxed_path |
| 1886 |
} |
| 1887 |
|
| 1888 |
//////////////////////////////////////////////////////////////////////////////// |
| 1889 |
|
| 1890 |
// If this were outside of the serde crate, it would just use: |
| 1891 |
// |
| 1892 |
// #[derive(Deserialize)] |
| 1893 |
// #[serde(variant_identifier)] |
| 1894 |
#[cfg(all(feature = "std", any(unix, windows)))] |
| 1895 |
variant_identifier! { |
| 1896 |
OsStringKind (Unix; b"Unix"; 0, Windows; b"Windows"; 1) |
| 1897 |
"`Unix` or `Windows`", |
| 1898 |
OSSTR_VARIANTS |
| 1899 |
} |
| 1900 |
|
| 1901 |
#[cfg(all(feature = "std", any(unix, windows)))] |
| 1902 |
struct OsStringVisitor; |
| 1903 |
|
| 1904 |
#[cfg(all(feature = "std", any(unix, windows)))] |
| 1905 |
impl<'de> Visitor<'de> for OsStringVisitor { |
| 1906 |
type Value = OsString; |
| 1907 |
|
| 1908 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1909 |
formatter.write_str("os string") |
| 1910 |
} |
| 1911 |
|
| 1912 |
#[cfg(unix)] |
| 1913 |
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 1914 |
where |
| 1915 |
A: EnumAccess<'de>, |
| 1916 |
{ |
| 1917 |
use std::os::unix::ffi::OsStringExt; |
| 1918 |
|
| 1919 |
match tri!(data.variant()) { |
| 1920 |
(OsStringKind::Unix, v) => v.newtype_variant().map(OsString::from_vec), |
| 1921 |
(OsStringKind::Windows, _) => Err(Error::custom( |
| 1922 |
"cannot deserialize Windows OS string on Unix", |
| 1923 |
)), |
| 1924 |
} |
| 1925 |
} |
| 1926 |
|
| 1927 |
#[cfg(windows)] |
| 1928 |
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 1929 |
where |
| 1930 |
A: EnumAccess<'de>, |
| 1931 |
{ |
| 1932 |
use std::os::windows::ffi::OsStringExt; |
| 1933 |
|
| 1934 |
match tri!(data.variant()) { |
| 1935 |
(OsStringKind::Windows, v) => v |
| 1936 |
.newtype_variant::<Vec<u16>>() |
| 1937 |
.map(|vec| OsString::from_wide(&vec)), |
| 1938 |
(OsStringKind::Unix, _) => Err(Error::custom( |
| 1939 |
"cannot deserialize Unix OS string on Windows", |
| 1940 |
)), |
| 1941 |
} |
| 1942 |
} |
| 1943 |
} |
| 1944 |
|
| 1945 |
#[cfg(all(feature = "std", any(unix, windows)))] |
| 1946 |
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] |
| 1947 |
impl<'de> Deserialize<'de> for OsString { |
| 1948 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1949 |
where |
| 1950 |
D: Deserializer<'de>, |
| 1951 |
{ |
| 1952 |
deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor) |
| 1953 |
} |
| 1954 |
} |
| 1955 |
|
| 1956 |
//////////////////////////////////////////////////////////////////////////////// |
| 1957 |
|
| 1958 |
forwarded_impl! { |
| 1959 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1960 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1961 |
(T), Box<T>, Box::new |
| 1962 |
} |
| 1963 |
|
| 1964 |
forwarded_impl! { |
| 1965 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1966 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1967 |
(T), Box<[T]>, Vec::into_boxed_slice |
| 1968 |
} |
| 1969 |
|
| 1970 |
forwarded_impl! { |
| 1971 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1972 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1973 |
(), Box<str>, String::into_boxed_str |
| 1974 |
} |
| 1975 |
|
| 1976 |
forwarded_impl! { |
| 1977 |
#[cfg(all(feature = "std", any(unix, windows)))] |
| 1978 |
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] |
| 1979 |
(), Box<OsStr>, OsString::into_boxed_os_str |
| 1980 |
} |
| 1981 |
|
| 1982 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 1983 |
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "alloc"))))] |
| 1984 |
impl<'de, 'a, T> Deserialize<'de> for Cow<'a, T> |
| 1985 |
where |
| 1986 |
T: ?Sized + ToOwned, |
| 1987 |
T::Owned: Deserialize<'de>, |
| 1988 |
{ |
| 1989 |
#[inline] |
| 1990 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1991 |
where |
| 1992 |
D: Deserializer<'de>, |
| 1993 |
{ |
| 1994 |
T::Owned::deserialize(deserializer).map(Cow::Owned) |
| 1995 |
} |
| 1996 |
} |
| 1997 |
|
| 1998 |
//////////////////////////////////////////////////////////////////////////////// |
| 1999 |
|
| 2000 |
/// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting |
| 2001 |
/// `Weak<T>` has a reference count of 0 and cannot be upgraded. |
| 2002 |
/// |
| 2003 |
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc |
| 2004 |
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] |
| 2005 |
#[cfg_attr( |
| 2006 |
doc_cfg, |
| 2007 |
doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))) |
| 2008 |
)] |
| 2009 |
impl<'de, T> Deserialize<'de> for RcWeak<T> |
| 2010 |
where |
| 2011 |
T: Deserialize<'de>, |
| 2012 |
{ |
| 2013 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2014 |
where |
| 2015 |
D: Deserializer<'de>, |
| 2016 |
{ |
| 2017 |
tri!(Option::<T>::deserialize(deserializer)); |
| 2018 |
Ok(RcWeak::new()) |
| 2019 |
} |
| 2020 |
} |
| 2021 |
|
| 2022 |
/// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting |
| 2023 |
/// `Weak<T>` has a reference count of 0 and cannot be upgraded. |
| 2024 |
/// |
| 2025 |
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc |
| 2026 |
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] |
| 2027 |
#[cfg_attr( |
| 2028 |
doc_cfg, |
| 2029 |
doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))) |
| 2030 |
)] |
| 2031 |
impl<'de, T> Deserialize<'de> for ArcWeak<T> |
| 2032 |
where |
| 2033 |
T: Deserialize<'de>, |
| 2034 |
{ |
| 2035 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2036 |
where |
| 2037 |
D: Deserializer<'de>, |
| 2038 |
{ |
| 2039 |
tri!(Option::<T>::deserialize(deserializer)); |
| 2040 |
Ok(ArcWeak::new()) |
| 2041 |
} |
| 2042 |
} |
| 2043 |
|
| 2044 |
//////////////////////////////////////////////////////////////////////////////// |
| 2045 |
|
| 2046 |
macro_rules! box_forwarded_impl { |
| 2047 |
( |
| 2048 |
$(#[$attr:meta])* |
| 2049 |
$t:ident |
| 2050 |
) => { |
| 2051 |
$(#[$attr])* |
| 2052 |
impl<'de, T> Deserialize<'de> for $t<T> |
| 2053 |
where |
| 2054 |
T: ?Sized, |
| 2055 |
Box<T>: Deserialize<'de>, |
| 2056 |
{ |
| 2057 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2058 |
where |
| 2059 |
D: Deserializer<'de>, |
| 2060 |
{ |
| 2061 |
Box::deserialize(deserializer).map(Into::into) |
| 2062 |
} |
| 2063 |
} |
| 2064 |
}; |
| 2065 |
} |
| 2066 |
|
| 2067 |
box_forwarded_impl! { |
| 2068 |
/// This impl requires the [`"rc"`] Cargo feature of Serde. |
| 2069 |
/// |
| 2070 |
/// Deserializing a data structure containing `Rc` will not attempt to |
| 2071 |
/// deduplicate `Rc` references to the same data. Every deserialized `Rc` |
| 2072 |
/// will end up with a strong count of 1. |
| 2073 |
/// |
| 2074 |
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc |
| 2075 |
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] |
| 2076 |
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))] |
| 2077 |
Rc |
| 2078 |
} |
| 2079 |
|
| 2080 |
box_forwarded_impl! { |
| 2081 |
/// This impl requires the [`"rc"`] Cargo feature of Serde. |
| 2082 |
/// |
| 2083 |
/// Deserializing a data structure containing `Arc` will not attempt to |
| 2084 |
/// deduplicate `Arc` references to the same data. Every deserialized `Arc` |
| 2085 |
/// will end up with a strong count of 1. |
| 2086 |
/// |
| 2087 |
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc |
| 2088 |
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] |
| 2089 |
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))] |
| 2090 |
Arc |
| 2091 |
} |
| 2092 |
|
| 2093 |
//////////////////////////////////////////////////////////////////////////////// |
| 2094 |
|
| 2095 |
impl<'de, T> Deserialize<'de> for Cell<T> |
| 2096 |
where |
| 2097 |
T: Deserialize<'de> + Copy, |
| 2098 |
{ |
| 2099 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2100 |
where |
| 2101 |
D: Deserializer<'de>, |
| 2102 |
{ |
| 2103 |
T::deserialize(deserializer).map(Cell::new) |
| 2104 |
} |
| 2105 |
} |
| 2106 |
|
| 2107 |
forwarded_impl! { |
| 2108 |
(T), RefCell<T>, RefCell::new |
| 2109 |
} |
| 2110 |
|
| 2111 |
forwarded_impl! { |
| 2112 |
#[cfg(feature = "std")] |
| 2113 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 2114 |
(T), Mutex<T>, Mutex::new |
| 2115 |
} |
| 2116 |
|
| 2117 |
forwarded_impl! { |
| 2118 |
#[cfg(feature = "std")] |
| 2119 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 2120 |
(T), RwLock<T>, RwLock::new |
| 2121 |
} |
| 2122 |
|
| 2123 |
//////////////////////////////////////////////////////////////////////////////// |
| 2124 |
|
| 2125 |
// This is a cleaned-up version of the impl generated by: |
| 2126 |
// |
| 2127 |
// #[derive(Deserialize)] |
| 2128 |
// #[serde(deny_unknown_fields)] |
| 2129 |
// struct Duration { |
| 2130 |
// secs: u64, |
| 2131 |
// nanos: u32, |
| 2132 |
// } |
| 2133 |
impl<'de> Deserialize<'de> for Duration { |
| 2134 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2135 |
where |
| 2136 |
D: Deserializer<'de>, |
| 2137 |
{ |
| 2138 |
// If this were outside of the serde crate, it would just use: |
| 2139 |
// |
| 2140 |
// #[derive(Deserialize)] |
| 2141 |
// #[serde(field_identifier, rename_all = "lowercase")] |
| 2142 |
enum Field { |
| 2143 |
Secs, |
| 2144 |
Nanos, |
| 2145 |
} |
| 2146 |
|
| 2147 |
impl<'de> Deserialize<'de> for Field { |
| 2148 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2149 |
where |
| 2150 |
D: Deserializer<'de>, |
| 2151 |
{ |
| 2152 |
struct FieldVisitor; |
| 2153 |
|
| 2154 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 2155 |
type Value = Field; |
| 2156 |
|
| 2157 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2158 |
formatter.write_str("`secs` or `nanos`") |
| 2159 |
} |
| 2160 |
|
| 2161 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 2162 |
where |
| 2163 |
E: Error, |
| 2164 |
{ |
| 2165 |
match value { |
| 2166 |
"secs" => Ok(Field::Secs), |
| 2167 |
"nanos" => Ok(Field::Nanos), |
| 2168 |
_ => Err(Error::unknown_field(value, FIELDS)), |
| 2169 |
} |
| 2170 |
} |
| 2171 |
|
| 2172 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 2173 |
where |
| 2174 |
E: Error, |
| 2175 |
{ |
| 2176 |
match value { |
| 2177 |
b"secs" => Ok(Field::Secs), |
| 2178 |
b"nanos" => Ok(Field::Nanos), |
| 2179 |
_ => { |
| 2180 |
let value = crate::__private::from_utf8_lossy(value); |
| 2181 |
Err(Error::unknown_field(&*value, FIELDS)) |
| 2182 |
} |
| 2183 |
} |
| 2184 |
} |
| 2185 |
} |
| 2186 |
|
| 2187 |
deserializer.deserialize_identifier(FieldVisitor) |
| 2188 |
} |
| 2189 |
} |
| 2190 |
|
| 2191 |
fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E> |
| 2192 |
where |
| 2193 |
E: Error, |
| 2194 |
{ |
| 2195 |
static NANOS_PER_SEC: u32 = 1_000_000_000; |
| 2196 |
match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { |
| 2197 |
Some(_) => Ok(()), |
| 2198 |
None => Err(E::custom("overflow deserializing Duration")), |
| 2199 |
} |
| 2200 |
} |
| 2201 |
|
| 2202 |
struct DurationVisitor; |
| 2203 |
|
| 2204 |
impl<'de> Visitor<'de> for DurationVisitor { |
| 2205 |
type Value = Duration; |
| 2206 |
|
| 2207 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2208 |
formatter.write_str("struct Duration") |
| 2209 |
} |
| 2210 |
|
| 2211 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 2212 |
where |
| 2213 |
A: SeqAccess<'de>, |
| 2214 |
{ |
| 2215 |
let secs: u64 = match tri!(seq.next_element()) { |
| 2216 |
Some(value) => value, |
| 2217 |
None => { |
| 2218 |
return Err(Error::invalid_length(0, &self)); |
| 2219 |
} |
| 2220 |
}; |
| 2221 |
let nanos: u32 = match tri!(seq.next_element()) { |
| 2222 |
Some(value) => value, |
| 2223 |
None => { |
| 2224 |
return Err(Error::invalid_length(1, &self)); |
| 2225 |
} |
| 2226 |
}; |
| 2227 |
tri!(check_overflow(secs, nanos)); |
| 2228 |
Ok(Duration::new(secs, nanos)) |
| 2229 |
} |
| 2230 |
|
| 2231 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 2232 |
where |
| 2233 |
A: MapAccess<'de>, |
| 2234 |
{ |
| 2235 |
let mut secs: Option<u64> = None; |
| 2236 |
let mut nanos: Option<u32> = None; |
| 2237 |
while let Some(key) = tri!(map.next_key()) { |
| 2238 |
match key { |
| 2239 |
Field::Secs => { |
| 2240 |
if secs.is_some() { |
| 2241 |
return Err(<A::Error as Error>::duplicate_field("secs")); |
| 2242 |
} |
| 2243 |
secs = Some(tri!(map.next_value())); |
| 2244 |
} |
| 2245 |
Field::Nanos => { |
| 2246 |
if nanos.is_some() { |
| 2247 |
return Err(<A::Error as Error>::duplicate_field("nanos")); |
| 2248 |
} |
| 2249 |
nanos = Some(tri!(map.next_value())); |
| 2250 |
} |
| 2251 |
} |
| 2252 |
} |
| 2253 |
let secs = match secs { |
| 2254 |
Some(secs) => secs, |
| 2255 |
None => return Err(<A::Error as Error>::missing_field("secs")), |
| 2256 |
}; |
| 2257 |
let nanos = match nanos { |
| 2258 |
Some(nanos) => nanos, |
| 2259 |
None => return Err(<A::Error as Error>::missing_field("nanos")), |
| 2260 |
}; |
| 2261 |
tri!(check_overflow(secs, nanos)); |
| 2262 |
Ok(Duration::new(secs, nanos)) |
| 2263 |
} |
| 2264 |
} |
| 2265 |
|
| 2266 |
const FIELDS: &[&str] = &["secs", "nanos"]; |
| 2267 |
deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor) |
| 2268 |
} |
| 2269 |
} |
| 2270 |
|
| 2271 |
//////////////////////////////////////////////////////////////////////////////// |
| 2272 |
|
| 2273 |
#[cfg(feature = "std")] |
| 2274 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 2275 |
impl<'de> Deserialize<'de> for SystemTime { |
| 2276 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2277 |
where |
| 2278 |
D: Deserializer<'de>, |
| 2279 |
{ |
| 2280 |
// Reuse duration |
| 2281 |
enum Field { |
| 2282 |
Secs, |
| 2283 |
Nanos, |
| 2284 |
} |
| 2285 |
|
| 2286 |
impl<'de> Deserialize<'de> for Field { |
| 2287 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2288 |
where |
| 2289 |
D: Deserializer<'de>, |
| 2290 |
{ |
| 2291 |
struct FieldVisitor; |
| 2292 |
|
| 2293 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 2294 |
type Value = Field; |
| 2295 |
|
| 2296 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2297 |
formatter.write_str("`secs_since_epoch` or `nanos_since_epoch`") |
| 2298 |
} |
| 2299 |
|
| 2300 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 2301 |
where |
| 2302 |
E: Error, |
| 2303 |
{ |
| 2304 |
match value { |
| 2305 |
"secs_since_epoch" => Ok(Field::Secs), |
| 2306 |
"nanos_since_epoch" => Ok(Field::Nanos), |
| 2307 |
_ => Err(Error::unknown_field(value, FIELDS)), |
| 2308 |
} |
| 2309 |
} |
| 2310 |
|
| 2311 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 2312 |
where |
| 2313 |
E: Error, |
| 2314 |
{ |
| 2315 |
match value { |
| 2316 |
b"secs_since_epoch" => Ok(Field::Secs), |
| 2317 |
b"nanos_since_epoch" => Ok(Field::Nanos), |
| 2318 |
_ => { |
| 2319 |
let value = String::from_utf8_lossy(value); |
| 2320 |
Err(Error::unknown_field(&value, FIELDS)) |
| 2321 |
} |
| 2322 |
} |
| 2323 |
} |
| 2324 |
} |
| 2325 |
|
| 2326 |
deserializer.deserialize_identifier(FieldVisitor) |
| 2327 |
} |
| 2328 |
} |
| 2329 |
|
| 2330 |
fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E> |
| 2331 |
where |
| 2332 |
E: Error, |
| 2333 |
{ |
| 2334 |
static NANOS_PER_SEC: u32 = 1_000_000_000; |
| 2335 |
match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { |
| 2336 |
Some(_) => Ok(()), |
| 2337 |
None => Err(E::custom("overflow deserializing SystemTime epoch offset")), |
| 2338 |
} |
| 2339 |
} |
| 2340 |
|
| 2341 |
struct DurationVisitor; |
| 2342 |
|
| 2343 |
impl<'de> Visitor<'de> for DurationVisitor { |
| 2344 |
type Value = Duration; |
| 2345 |
|
| 2346 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2347 |
formatter.write_str("struct SystemTime") |
| 2348 |
} |
| 2349 |
|
| 2350 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 2351 |
where |
| 2352 |
A: SeqAccess<'de>, |
| 2353 |
{ |
| 2354 |
let secs: u64 = match tri!(seq.next_element()) { |
| 2355 |
Some(value) => value, |
| 2356 |
None => { |
| 2357 |
return Err(Error::invalid_length(0, &self)); |
| 2358 |
} |
| 2359 |
}; |
| 2360 |
let nanos: u32 = match tri!(seq.next_element()) { |
| 2361 |
Some(value) => value, |
| 2362 |
None => { |
| 2363 |
return Err(Error::invalid_length(1, &self)); |
| 2364 |
} |
| 2365 |
}; |
| 2366 |
tri!(check_overflow(secs, nanos)); |
| 2367 |
Ok(Duration::new(secs, nanos)) |
| 2368 |
} |
| 2369 |
|
| 2370 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 2371 |
where |
| 2372 |
A: MapAccess<'de>, |
| 2373 |
{ |
| 2374 |
let mut secs: Option<u64> = None; |
| 2375 |
let mut nanos: Option<u32> = None; |
| 2376 |
while let Some(key) = tri!(map.next_key()) { |
| 2377 |
match key { |
| 2378 |
Field::Secs => { |
| 2379 |
if secs.is_some() { |
| 2380 |
return Err(<A::Error as Error>::duplicate_field( |
| 2381 |
"secs_since_epoch", |
| 2382 |
)); |
| 2383 |
} |
| 2384 |
secs = Some(tri!(map.next_value())); |
| 2385 |
} |
| 2386 |
Field::Nanos => { |
| 2387 |
if nanos.is_some() { |
| 2388 |
return Err(<A::Error as Error>::duplicate_field( |
| 2389 |
"nanos_since_epoch", |
| 2390 |
)); |
| 2391 |
} |
| 2392 |
nanos = Some(tri!(map.next_value())); |
| 2393 |
} |
| 2394 |
} |
| 2395 |
} |
| 2396 |
let secs = match secs { |
| 2397 |
Some(secs) => secs, |
| 2398 |
None => return Err(<A::Error as Error>::missing_field("secs_since_epoch")), |
| 2399 |
}; |
| 2400 |
let nanos = match nanos { |
| 2401 |
Some(nanos) => nanos, |
| 2402 |
None => return Err(<A::Error as Error>::missing_field("nanos_since_epoch")), |
| 2403 |
}; |
| 2404 |
tri!(check_overflow(secs, nanos)); |
| 2405 |
Ok(Duration::new(secs, nanos)) |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
const FIELDS: &[&str] = &["secs_since_epoch", "nanos_since_epoch"]; |
| 2410 |
let duration = tri!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor)); |
| 2411 |
#[cfg(not(no_systemtime_checked_add))] |
| 2412 |
let ret = UNIX_EPOCH |
| 2413 |
.checked_add(duration) |
| 2414 |
.ok_or_else(|| D::Error::custom("overflow deserializing SystemTime")); |
| 2415 |
#[cfg(no_systemtime_checked_add)] |
| 2416 |
let ret = Ok(UNIX_EPOCH + duration); |
| 2417 |
ret |
| 2418 |
} |
| 2419 |
} |
| 2420 |
|
| 2421 |
//////////////////////////////////////////////////////////////////////////////// |
| 2422 |
|
| 2423 |
// Similar to: |
| 2424 |
// |
| 2425 |
// #[derive(Deserialize)] |
| 2426 |
// #[serde(deny_unknown_fields)] |
| 2427 |
// struct Range<Idx> { |
| 2428 |
// start: Idx, |
| 2429 |
// end: Idx, |
| 2430 |
// } |
| 2431 |
impl<'de, Idx> Deserialize<'de> for Range<Idx> |
| 2432 |
where |
| 2433 |
Idx: Deserialize<'de>, |
| 2434 |
{ |
| 2435 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2436 |
where |
| 2437 |
D: Deserializer<'de>, |
| 2438 |
{ |
| 2439 |
let (start, end) = tri!(deserializer.deserialize_struct( |
| 2440 |
"Range", |
| 2441 |
range::FIELDS, |
| 2442 |
range::RangeVisitor { |
| 2443 |
expecting: "struct Range", |
| 2444 |
phantom: PhantomData, |
| 2445 |
}, |
| 2446 |
)); |
| 2447 |
Ok(start..end) |
| 2448 |
} |
| 2449 |
} |
| 2450 |
|
| 2451 |
impl<'de, Idx> Deserialize<'de> for RangeInclusive<Idx> |
| 2452 |
where |
| 2453 |
Idx: Deserialize<'de>, |
| 2454 |
{ |
| 2455 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2456 |
where |
| 2457 |
D: Deserializer<'de>, |
| 2458 |
{ |
| 2459 |
let (start, end) = tri!(deserializer.deserialize_struct( |
| 2460 |
"RangeInclusive", |
| 2461 |
range::FIELDS, |
| 2462 |
range::RangeVisitor { |
| 2463 |
expecting: "struct RangeInclusive", |
| 2464 |
phantom: PhantomData, |
| 2465 |
}, |
| 2466 |
)); |
| 2467 |
Ok(RangeInclusive::new(start, end)) |
| 2468 |
} |
| 2469 |
} |
| 2470 |
|
| 2471 |
mod range { |
| 2472 |
use crate::lib::*; |
| 2473 |
|
| 2474 |
use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; |
| 2475 |
|
| 2476 |
pub const FIELDS: &[&str] = &["start", "end"]; |
| 2477 |
|
| 2478 |
// If this were outside of the serde crate, it would just use: |
| 2479 |
// |
| 2480 |
// #[derive(Deserialize)] |
| 2481 |
// #[serde(field_identifier, rename_all = "lowercase")] |
| 2482 |
enum Field { |
| 2483 |
Start, |
| 2484 |
End, |
| 2485 |
} |
| 2486 |
|
| 2487 |
impl<'de> Deserialize<'de> for Field { |
| 2488 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2489 |
where |
| 2490 |
D: Deserializer<'de>, |
| 2491 |
{ |
| 2492 |
struct FieldVisitor; |
| 2493 |
|
| 2494 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 2495 |
type Value = Field; |
| 2496 |
|
| 2497 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2498 |
formatter.write_str("`start` or `end`") |
| 2499 |
} |
| 2500 |
|
| 2501 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 2502 |
where |
| 2503 |
E: Error, |
| 2504 |
{ |
| 2505 |
match value { |
| 2506 |
"start" => Ok(Field::Start), |
| 2507 |
"end" => Ok(Field::End), |
| 2508 |
_ => Err(Error::unknown_field(value, FIELDS)), |
| 2509 |
} |
| 2510 |
} |
| 2511 |
|
| 2512 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 2513 |
where |
| 2514 |
E: Error, |
| 2515 |
{ |
| 2516 |
match value { |
| 2517 |
b"start" => Ok(Field::Start), |
| 2518 |
b"end" => Ok(Field::End), |
| 2519 |
_ => { |
| 2520 |
let value = crate::__private::from_utf8_lossy(value); |
| 2521 |
Err(Error::unknown_field(&*value, FIELDS)) |
| 2522 |
} |
| 2523 |
} |
| 2524 |
} |
| 2525 |
} |
| 2526 |
|
| 2527 |
deserializer.deserialize_identifier(FieldVisitor) |
| 2528 |
} |
| 2529 |
} |
| 2530 |
|
| 2531 |
pub struct RangeVisitor<Idx> { |
| 2532 |
pub expecting: &'static str, |
| 2533 |
pub phantom: PhantomData<Idx>, |
| 2534 |
} |
| 2535 |
|
| 2536 |
impl<'de, Idx> Visitor<'de> for RangeVisitor<Idx> |
| 2537 |
where |
| 2538 |
Idx: Deserialize<'de>, |
| 2539 |
{ |
| 2540 |
type Value = (Idx, Idx); |
| 2541 |
|
| 2542 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2543 |
formatter.write_str(self.expecting) |
| 2544 |
} |
| 2545 |
|
| 2546 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 2547 |
where |
| 2548 |
A: SeqAccess<'de>, |
| 2549 |
{ |
| 2550 |
let start: Idx = match tri!(seq.next_element()) { |
| 2551 |
Some(value) => value, |
| 2552 |
None => { |
| 2553 |
return Err(Error::invalid_length(0, &self)); |
| 2554 |
} |
| 2555 |
}; |
| 2556 |
let end: Idx = match tri!(seq.next_element()) { |
| 2557 |
Some(value) => value, |
| 2558 |
None => { |
| 2559 |
return Err(Error::invalid_length(1, &self)); |
| 2560 |
} |
| 2561 |
}; |
| 2562 |
Ok((start, end)) |
| 2563 |
} |
| 2564 |
|
| 2565 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 2566 |
where |
| 2567 |
A: MapAccess<'de>, |
| 2568 |
{ |
| 2569 |
let mut start: Option<Idx> = None; |
| 2570 |
let mut end: Option<Idx> = None; |
| 2571 |
while let Some(key) = tri!(map.next_key()) { |
| 2572 |
match key { |
| 2573 |
Field::Start => { |
| 2574 |
if start.is_some() { |
| 2575 |
return Err(<A::Error as Error>::duplicate_field("start")); |
| 2576 |
} |
| 2577 |
start = Some(tri!(map.next_value())); |
| 2578 |
} |
| 2579 |
Field::End => { |
| 2580 |
if end.is_some() { |
| 2581 |
return Err(<A::Error as Error>::duplicate_field("end")); |
| 2582 |
} |
| 2583 |
end = Some(tri!(map.next_value())); |
| 2584 |
} |
| 2585 |
} |
| 2586 |
} |
| 2587 |
let start = match start { |
| 2588 |
Some(start) => start, |
| 2589 |
None => return Err(<A::Error as Error>::missing_field("start")), |
| 2590 |
}; |
| 2591 |
let end = match end { |
| 2592 |
Some(end) => end, |
| 2593 |
None => return Err(<A::Error as Error>::missing_field("end")), |
| 2594 |
}; |
| 2595 |
Ok((start, end)) |
| 2596 |
} |
| 2597 |
} |
| 2598 |
} |
| 2599 |
|
| 2600 |
//////////////////////////////////////////////////////////////////////////////// |
| 2601 |
|
| 2602 |
// Similar to: |
| 2603 |
// |
| 2604 |
// #[derive(Deserialize)] |
| 2605 |
// #[serde(deny_unknown_fields)] |
| 2606 |
// struct RangeFrom<Idx> { |
| 2607 |
// start: Idx, |
| 2608 |
// } |
| 2609 |
impl<'de, Idx> Deserialize<'de> for RangeFrom<Idx> |
| 2610 |
where |
| 2611 |
Idx: Deserialize<'de>, |
| 2612 |
{ |
| 2613 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2614 |
where |
| 2615 |
D: Deserializer<'de>, |
| 2616 |
{ |
| 2617 |
let start = tri!(deserializer.deserialize_struct( |
| 2618 |
"RangeFrom", |
| 2619 |
range_from::FIELDS, |
| 2620 |
range_from::RangeFromVisitor { |
| 2621 |
expecting: "struct RangeFrom", |
| 2622 |
phantom: PhantomData, |
| 2623 |
}, |
| 2624 |
)); |
| 2625 |
Ok(start..) |
| 2626 |
} |
| 2627 |
} |
| 2628 |
|
| 2629 |
mod range_from { |
| 2630 |
use crate::lib::*; |
| 2631 |
|
| 2632 |
use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; |
| 2633 |
|
| 2634 |
pub const FIELDS: &[&str] = &["start"]; |
| 2635 |
|
| 2636 |
// If this were outside of the serde crate, it would just use: |
| 2637 |
// |
| 2638 |
// #[derive(Deserialize)] |
| 2639 |
// #[serde(field_identifier, rename_all = "lowercase")] |
| 2640 |
enum Field { |
| 2641 |
Start, |
| 2642 |
} |
| 2643 |
|
| 2644 |
impl<'de> Deserialize<'de> for Field { |
| 2645 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2646 |
where |
| 2647 |
D: Deserializer<'de>, |
| 2648 |
{ |
| 2649 |
struct FieldVisitor; |
| 2650 |
|
| 2651 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 2652 |
type Value = Field; |
| 2653 |
|
| 2654 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2655 |
formatter.write_str("`start`") |
| 2656 |
} |
| 2657 |
|
| 2658 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 2659 |
where |
| 2660 |
E: Error, |
| 2661 |
{ |
| 2662 |
match value { |
| 2663 |
"start" => Ok(Field::Start), |
| 2664 |
_ => Err(Error::unknown_field(value, FIELDS)), |
| 2665 |
} |
| 2666 |
} |
| 2667 |
|
| 2668 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 2669 |
where |
| 2670 |
E: Error, |
| 2671 |
{ |
| 2672 |
match value { |
| 2673 |
b"start" => Ok(Field::Start), |
| 2674 |
_ => { |
| 2675 |
let value = crate::__private::from_utf8_lossy(value); |
| 2676 |
Err(Error::unknown_field(&*value, FIELDS)) |
| 2677 |
} |
| 2678 |
} |
| 2679 |
} |
| 2680 |
} |
| 2681 |
|
| 2682 |
deserializer.deserialize_identifier(FieldVisitor) |
| 2683 |
} |
| 2684 |
} |
| 2685 |
|
| 2686 |
pub struct RangeFromVisitor<Idx> { |
| 2687 |
pub expecting: &'static str, |
| 2688 |
pub phantom: PhantomData<Idx>, |
| 2689 |
} |
| 2690 |
|
| 2691 |
impl<'de, Idx> Visitor<'de> for RangeFromVisitor<Idx> |
| 2692 |
where |
| 2693 |
Idx: Deserialize<'de>, |
| 2694 |
{ |
| 2695 |
type Value = Idx; |
| 2696 |
|
| 2697 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2698 |
formatter.write_str(self.expecting) |
| 2699 |
} |
| 2700 |
|
| 2701 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 2702 |
where |
| 2703 |
A: SeqAccess<'de>, |
| 2704 |
{ |
| 2705 |
let start: Idx = match tri!(seq.next_element()) { |
| 2706 |
Some(value) => value, |
| 2707 |
None => { |
| 2708 |
return Err(Error::invalid_length(0, &self)); |
| 2709 |
} |
| 2710 |
}; |
| 2711 |
Ok(start) |
| 2712 |
} |
| 2713 |
|
| 2714 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 2715 |
where |
| 2716 |
A: MapAccess<'de>, |
| 2717 |
{ |
| 2718 |
let mut start: Option<Idx> = None; |
| 2719 |
while let Some(key) = tri!(map.next_key()) { |
| 2720 |
match key { |
| 2721 |
Field::Start => { |
| 2722 |
if start.is_some() { |
| 2723 |
return Err(<A::Error as Error>::duplicate_field("start")); |
| 2724 |
} |
| 2725 |
start = Some(tri!(map.next_value())); |
| 2726 |
} |
| 2727 |
} |
| 2728 |
} |
| 2729 |
let start = match start { |
| 2730 |
Some(start) => start, |
| 2731 |
None => return Err(<A::Error as Error>::missing_field("start")), |
| 2732 |
}; |
| 2733 |
Ok(start) |
| 2734 |
} |
| 2735 |
} |
| 2736 |
} |
| 2737 |
|
| 2738 |
//////////////////////////////////////////////////////////////////////////////// |
| 2739 |
|
| 2740 |
// Similar to: |
| 2741 |
// |
| 2742 |
// #[derive(Deserialize)] |
| 2743 |
// #[serde(deny_unknown_fields)] |
| 2744 |
// struct RangeTo<Idx> { |
| 2745 |
// end: Idx, |
| 2746 |
// } |
| 2747 |
impl<'de, Idx> Deserialize<'de> for RangeTo<Idx> |
| 2748 |
where |
| 2749 |
Idx: Deserialize<'de>, |
| 2750 |
{ |
| 2751 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2752 |
where |
| 2753 |
D: Deserializer<'de>, |
| 2754 |
{ |
| 2755 |
let end = tri!(deserializer.deserialize_struct( |
| 2756 |
"RangeTo", |
| 2757 |
range_to::FIELDS, |
| 2758 |
range_to::RangeToVisitor { |
| 2759 |
expecting: "struct RangeTo", |
| 2760 |
phantom: PhantomData, |
| 2761 |
}, |
| 2762 |
)); |
| 2763 |
Ok(..end) |
| 2764 |
} |
| 2765 |
} |
| 2766 |
|
| 2767 |
mod range_to { |
| 2768 |
use crate::lib::*; |
| 2769 |
|
| 2770 |
use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; |
| 2771 |
|
| 2772 |
pub const FIELDS: &[&str] = &["end"]; |
| 2773 |
|
| 2774 |
// If this were outside of the serde crate, it would just use: |
| 2775 |
// |
| 2776 |
// #[derive(Deserialize)] |
| 2777 |
// #[serde(field_identifier, rename_all = "lowercase")] |
| 2778 |
enum Field { |
| 2779 |
End, |
| 2780 |
} |
| 2781 |
|
| 2782 |
impl<'de> Deserialize<'de> for Field { |
| 2783 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2784 |
where |
| 2785 |
D: Deserializer<'de>, |
| 2786 |
{ |
| 2787 |
struct FieldVisitor; |
| 2788 |
|
| 2789 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 2790 |
type Value = Field; |
| 2791 |
|
| 2792 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2793 |
formatter.write_str("`end`") |
| 2794 |
} |
| 2795 |
|
| 2796 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 2797 |
where |
| 2798 |
E: Error, |
| 2799 |
{ |
| 2800 |
match value { |
| 2801 |
"end" => Ok(Field::End), |
| 2802 |
_ => Err(Error::unknown_field(value, FIELDS)), |
| 2803 |
} |
| 2804 |
} |
| 2805 |
|
| 2806 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 2807 |
where |
| 2808 |
E: Error, |
| 2809 |
{ |
| 2810 |
match value { |
| 2811 |
b"end" => Ok(Field::End), |
| 2812 |
_ => { |
| 2813 |
let value = crate::__private::from_utf8_lossy(value); |
| 2814 |
Err(Error::unknown_field(&*value, FIELDS)) |
| 2815 |
} |
| 2816 |
} |
| 2817 |
} |
| 2818 |
} |
| 2819 |
|
| 2820 |
deserializer.deserialize_identifier(FieldVisitor) |
| 2821 |
} |
| 2822 |
} |
| 2823 |
|
| 2824 |
pub struct RangeToVisitor<Idx> { |
| 2825 |
pub expecting: &'static str, |
| 2826 |
pub phantom: PhantomData<Idx>, |
| 2827 |
} |
| 2828 |
|
| 2829 |
impl<'de, Idx> Visitor<'de> for RangeToVisitor<Idx> |
| 2830 |
where |
| 2831 |
Idx: Deserialize<'de>, |
| 2832 |
{ |
| 2833 |
type Value = Idx; |
| 2834 |
|
| 2835 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2836 |
formatter.write_str(self.expecting) |
| 2837 |
} |
| 2838 |
|
| 2839 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 2840 |
where |
| 2841 |
A: SeqAccess<'de>, |
| 2842 |
{ |
| 2843 |
let end: Idx = match tri!(seq.next_element()) { |
| 2844 |
Some(value) => value, |
| 2845 |
None => { |
| 2846 |
return Err(Error::invalid_length(0, &self)); |
| 2847 |
} |
| 2848 |
}; |
| 2849 |
Ok(end) |
| 2850 |
} |
| 2851 |
|
| 2852 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 2853 |
where |
| 2854 |
A: MapAccess<'de>, |
| 2855 |
{ |
| 2856 |
let mut end: Option<Idx> = None; |
| 2857 |
while let Some(key) = tri!(map.next_key()) { |
| 2858 |
match key { |
| 2859 |
Field::End => { |
| 2860 |
if end.is_some() { |
| 2861 |
return Err(<A::Error as Error>::duplicate_field("end")); |
| 2862 |
} |
| 2863 |
end = Some(tri!(map.next_value())); |
| 2864 |
} |
| 2865 |
} |
| 2866 |
} |
| 2867 |
let end = match end { |
| 2868 |
Some(end) => end, |
| 2869 |
None => return Err(<A::Error as Error>::missing_field("end")), |
| 2870 |
}; |
| 2871 |
Ok(end) |
| 2872 |
} |
| 2873 |
} |
| 2874 |
} |
| 2875 |
|
| 2876 |
//////////////////////////////////////////////////////////////////////////////// |
| 2877 |
|
| 2878 |
impl<'de, T> Deserialize<'de> for Bound<T> |
| 2879 |
where |
| 2880 |
T: Deserialize<'de>, |
| 2881 |
{ |
| 2882 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2883 |
where |
| 2884 |
D: Deserializer<'de>, |
| 2885 |
{ |
| 2886 |
enum Field { |
| 2887 |
Unbounded, |
| 2888 |
Included, |
| 2889 |
Excluded, |
| 2890 |
} |
| 2891 |
|
| 2892 |
impl<'de> Deserialize<'de> for Field { |
| 2893 |
#[inline] |
| 2894 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2895 |
where |
| 2896 |
D: Deserializer<'de>, |
| 2897 |
{ |
| 2898 |
struct FieldVisitor; |
| 2899 |
|
| 2900 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 2901 |
type Value = Field; |
| 2902 |
|
| 2903 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2904 |
formatter.write_str("`Unbounded`, `Included` or `Excluded`") |
| 2905 |
} |
| 2906 |
|
| 2907 |
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
| 2908 |
where |
| 2909 |
E: Error, |
| 2910 |
{ |
| 2911 |
match value { |
| 2912 |
0 => Ok(Field::Unbounded), |
| 2913 |
1 => Ok(Field::Included), |
| 2914 |
2 => Ok(Field::Excluded), |
| 2915 |
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)), |
| 2916 |
} |
| 2917 |
} |
| 2918 |
|
| 2919 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 2920 |
where |
| 2921 |
E: Error, |
| 2922 |
{ |
| 2923 |
match value { |
| 2924 |
"Unbounded" => Ok(Field::Unbounded), |
| 2925 |
"Included" => Ok(Field::Included), |
| 2926 |
"Excluded" => Ok(Field::Excluded), |
| 2927 |
_ => Err(Error::unknown_variant(value, VARIANTS)), |
| 2928 |
} |
| 2929 |
} |
| 2930 |
|
| 2931 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 2932 |
where |
| 2933 |
E: Error, |
| 2934 |
{ |
| 2935 |
match value { |
| 2936 |
b"Unbounded" => Ok(Field::Unbounded), |
| 2937 |
b"Included" => Ok(Field::Included), |
| 2938 |
b"Excluded" => Ok(Field::Excluded), |
| 2939 |
_ => match str::from_utf8(value) { |
| 2940 |
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), |
| 2941 |
Err(_) => { |
| 2942 |
Err(Error::invalid_value(Unexpected::Bytes(value), &self)) |
| 2943 |
} |
| 2944 |
}, |
| 2945 |
} |
| 2946 |
} |
| 2947 |
} |
| 2948 |
|
| 2949 |
deserializer.deserialize_identifier(FieldVisitor) |
| 2950 |
} |
| 2951 |
} |
| 2952 |
|
| 2953 |
struct BoundVisitor<T>(PhantomData<Bound<T>>); |
| 2954 |
|
| 2955 |
impl<'de, T> Visitor<'de> for BoundVisitor<T> |
| 2956 |
where |
| 2957 |
T: Deserialize<'de>, |
| 2958 |
{ |
| 2959 |
type Value = Bound<T>; |
| 2960 |
|
| 2961 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2962 |
formatter.write_str("enum Bound") |
| 2963 |
} |
| 2964 |
|
| 2965 |
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 2966 |
where |
| 2967 |
A: EnumAccess<'de>, |
| 2968 |
{ |
| 2969 |
match tri!(data.variant()) { |
| 2970 |
(Field::Unbounded, v) => v.unit_variant().map(|()| Bound::Unbounded), |
| 2971 |
(Field::Included, v) => v.newtype_variant().map(Bound::Included), |
| 2972 |
(Field::Excluded, v) => v.newtype_variant().map(Bound::Excluded), |
| 2973 |
} |
| 2974 |
} |
| 2975 |
} |
| 2976 |
|
| 2977 |
const VARIANTS: &[&str] = &["Unbounded", "Included", "Excluded"]; |
| 2978 |
|
| 2979 |
deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData)) |
| 2980 |
} |
| 2981 |
} |
| 2982 |
|
| 2983 |
//////////////////////////////////////////////////////////////////////////////// |
| 2984 |
|
| 2985 |
impl<'de, T, E> Deserialize<'de> for Result<T, E> |
| 2986 |
where |
| 2987 |
T: Deserialize<'de>, |
| 2988 |
E: Deserialize<'de>, |
| 2989 |
{ |
| 2990 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2991 |
where |
| 2992 |
D: Deserializer<'de>, |
| 2993 |
{ |
| 2994 |
// If this were outside of the serde crate, it would just use: |
| 2995 |
// |
| 2996 |
// #[derive(Deserialize)] |
| 2997 |
// #[serde(variant_identifier)] |
| 2998 |
enum Field { |
| 2999 |
Ok, |
| 3000 |
Err, |
| 3001 |
} |
| 3002 |
|
| 3003 |
impl<'de> Deserialize<'de> for Field { |
| 3004 |
#[inline] |
| 3005 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 3006 |
where |
| 3007 |
D: Deserializer<'de>, |
| 3008 |
{ |
| 3009 |
struct FieldVisitor; |
| 3010 |
|
| 3011 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 3012 |
type Value = Field; |
| 3013 |
|
| 3014 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 3015 |
formatter.write_str("`Ok` or `Err`") |
| 3016 |
} |
| 3017 |
|
| 3018 |
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
| 3019 |
where |
| 3020 |
E: Error, |
| 3021 |
{ |
| 3022 |
match value { |
| 3023 |
0 => Ok(Field::Ok), |
| 3024 |
1 => Ok(Field::Err), |
| 3025 |
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)), |
| 3026 |
} |
| 3027 |
} |
| 3028 |
|
| 3029 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 3030 |
where |
| 3031 |
E: Error, |
| 3032 |
{ |
| 3033 |
match value { |
| 3034 |
"Ok" => Ok(Field::Ok), |
| 3035 |
"Err" => Ok(Field::Err), |
| 3036 |
_ => Err(Error::unknown_variant(value, VARIANTS)), |
| 3037 |
} |
| 3038 |
} |
| 3039 |
|
| 3040 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 3041 |
where |
| 3042 |
E: Error, |
| 3043 |
{ |
| 3044 |
match value { |
| 3045 |
b"Ok" => Ok(Field::Ok), |
| 3046 |
b"Err" => Ok(Field::Err), |
| 3047 |
_ => match str::from_utf8(value) { |
| 3048 |
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), |
| 3049 |
Err(_) => { |
| 3050 |
Err(Error::invalid_value(Unexpected::Bytes(value), &self)) |
| 3051 |
} |
| 3052 |
}, |
| 3053 |
} |
| 3054 |
} |
| 3055 |
} |
| 3056 |
|
| 3057 |
deserializer.deserialize_identifier(FieldVisitor) |
| 3058 |
} |
| 3059 |
} |
| 3060 |
|
| 3061 |
struct ResultVisitor<T, E>(PhantomData<Result<T, E>>); |
| 3062 |
|
| 3063 |
impl<'de, T, E> Visitor<'de> for ResultVisitor<T, E> |
| 3064 |
where |
| 3065 |
T: Deserialize<'de>, |
| 3066 |
E: Deserialize<'de>, |
| 3067 |
{ |
| 3068 |
type Value = Result<T, E>; |
| 3069 |
|
| 3070 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 3071 |
formatter.write_str("enum Result") |
| 3072 |
} |
| 3073 |
|
| 3074 |
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 3075 |
where |
| 3076 |
A: EnumAccess<'de>, |
| 3077 |
{ |
| 3078 |
match tri!(data.variant()) { |
| 3079 |
(Field::Ok, v) => v.newtype_variant().map(Ok), |
| 3080 |
(Field::Err, v) => v.newtype_variant().map(Err), |
| 3081 |
} |
| 3082 |
} |
| 3083 |
} |
| 3084 |
|
| 3085 |
const VARIANTS: &[&str] = &["Ok", "Err"]; |
| 3086 |
|
| 3087 |
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData)) |
| 3088 |
} |
| 3089 |
} |
| 3090 |
|
| 3091 |
//////////////////////////////////////////////////////////////////////////////// |
| 3092 |
|
| 3093 |
impl<'de, T> Deserialize<'de> for Wrapping<T> |
| 3094 |
where |
| 3095 |
T: Deserialize<'de>, |
| 3096 |
{ |
| 3097 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 3098 |
where |
| 3099 |
D: Deserializer<'de>, |
| 3100 |
{ |
| 3101 |
Deserialize::deserialize(deserializer).map(Wrapping) |
| 3102 |
} |
| 3103 |
} |
| 3104 |
|
| 3105 |
#[cfg(all(feature = "std", not(no_std_atomic)))] |
| 3106 |
macro_rules! atomic_impl { |
| 3107 |
($($ty:ident $size:expr)*) => { |
| 3108 |
$( |
| 3109 |
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))] |
| 3110 |
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", target_has_atomic = $size))))] |
| 3111 |
impl<'de> Deserialize<'de> for $ty { |
| 3112 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 3113 |
where |
| 3114 |
D: Deserializer<'de>, |
| 3115 |
{ |
| 3116 |
Deserialize::deserialize(deserializer).map(Self::new) |
| 3117 |
} |
| 3118 |
} |
| 3119 |
)* |
| 3120 |
}; |
| 3121 |
} |
| 3122 |
|
| 3123 |
#[cfg(all(feature = "std", not(no_std_atomic)))] |
| 3124 |
atomic_impl! { |
| 3125 |
AtomicBool "8" |
| 3126 |
AtomicI8 "8" |
| 3127 |
AtomicI16 "16" |
| 3128 |
AtomicI32 "32" |
| 3129 |
AtomicIsize "ptr" |
| 3130 |
AtomicU8 "8" |
| 3131 |
AtomicU16 "16" |
| 3132 |
AtomicU32 "32" |
| 3133 |
AtomicUsize "ptr" |
| 3134 |
} |
| 3135 |
|
| 3136 |
#[cfg(all(feature = "std", not(no_std_atomic64)))] |
| 3137 |
atomic_impl! { |
| 3138 |
AtomicI64 "64" |
| 3139 |
AtomicU64 "64" |
| 3140 |
} |
| 3141 |
|
| 3142 |
#[cfg(feature = "std")] |
| 3143 |
struct FromStrVisitor<T> { |
| 3144 |
expecting: &'static str, |
| 3145 |
ty: PhantomData<T>, |
| 3146 |
} |
| 3147 |
|
| 3148 |
#[cfg(feature = "std")] |
| 3149 |
impl<T> FromStrVisitor<T> { |
| 3150 |
fn new(expecting: &'static str) -> Self { |
| 3151 |
FromStrVisitor { |
| 3152 |
expecting, |
| 3153 |
ty: PhantomData, |
| 3154 |
} |
| 3155 |
} |
| 3156 |
} |
| 3157 |
|
| 3158 |
#[cfg(feature = "std")] |
| 3159 |
impl<'de, T> Visitor<'de> for FromStrVisitor<T> |
| 3160 |
where |
| 3161 |
T: str::FromStr, |
| 3162 |
T::Err: fmt::Display, |
| 3163 |
{ |
| 3164 |
type Value = T; |
| 3165 |
|
| 3166 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 3167 |
formatter.write_str(self.expecting) |
| 3168 |
} |
| 3169 |
|
| 3170 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 3171 |
where |
| 3172 |
E: Error, |
| 3173 |
{ |
| 3174 |
s.parse().map_err(Error::custom) |
| 3175 |
} |
| 3176 |
} |