]> git.proxmox.com Git - rustc.git/blob - src/vendor/serde_json/src/number.rs
New upstream version 1.26.0+dfsg1
[rustc.git] / src / vendor / serde_json / src / number.rs
1 // Copyright 2017 Serde Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 use error::Error;
10 use serde::de::{self, Visitor, Unexpected};
11 use serde::{Serialize, Serializer, Deserialize, Deserializer};
12 use std::fmt::{self, Debug, Display};
13 use std::i64;
14 use std::str::FromStr;
15
16 #[cfg(not(feature = "arbitrary_precision"))]
17 use num_traits::{NumCast};
18
19 #[cfg(feature = "arbitrary_precision")]
20 use dtoa;
21 #[cfg(feature = "arbitrary_precision")]
22 use itoa;
23 #[cfg(feature = "arbitrary_precision")]
24 use serde::de::{IntoDeserializer, MapAccess};
25
26 #[cfg(feature = "arbitrary_precision")]
27 use error::{ErrorCode};
28
29 #[cfg(feature = "arbitrary_precision")]
30 /// Not public API. Should be pub(crate).
31 #[doc(hidden)]
32 pub const SERDE_STRUCT_FIELD_NAME: &'static str = "$__serde_private_number";
33
34 #[cfg(feature = "arbitrary_precision")]
35 /// Not public API. Should be pub(crate).
36 #[doc(hidden)]
37 pub const SERDE_STRUCT_NAME: &'static str = "$__serde_private_Number";
38
39 /// Represents a JSON number, whether integer or floating point.
40 #[derive(Clone, PartialEq)]
41 pub struct Number {
42 n: N,
43 }
44
45 #[cfg(not(feature = "arbitrary_precision"))]
46 #[derive(Copy, Clone, PartialEq)]
47 enum N {
48 PosInt(u64),
49 /// Always less than zero.
50 NegInt(i64),
51 /// Always finite.
52 Float(f64),
53 }
54
55 #[cfg(feature = "arbitrary_precision")]
56 type N = String;
57
58 impl Number {
59 /// Returns true if the `Number` is an integer between `i64::MIN` and
60 /// `i64::MAX`.
61 ///
62 /// For any Number on which `is_i64` returns true, `as_i64` is guaranteed to
63 /// return the integer value.
64 ///
65 /// ```rust
66 /// # #[macro_use]
67 /// # extern crate serde_json;
68 /// #
69 /// # use std::i64;
70 /// #
71 /// # fn main() {
72 /// let big = i64::MAX as u64 + 10;
73 /// let v = json!({ "a": 64, "b": big, "c": 256.0 });
74 ///
75 /// assert!(v["a"].is_i64());
76 ///
77 /// // Greater than i64::MAX.
78 /// assert!(!v["b"].is_i64());
79 ///
80 /// // Numbers with a decimal point are not considered integers.
81 /// assert!(!v["c"].is_i64());
82 /// # }
83 /// ```
84 #[inline]
85 pub fn is_i64(&self) -> bool {
86 #[cfg(not(feature = "arbitrary_precision"))]
87 match self.n {
88 N::PosInt(v) => v <= i64::max_value() as u64,
89 N::NegInt(_) => true,
90 N::Float(_) => false,
91 }
92 #[cfg(feature = "arbitrary_precision")]
93 self.as_i64().is_some()
94 }
95
96 /// Returns true if the `Number` is an integer between zero and `u64::MAX`.
97 ///
98 /// For any Number on which `is_u64` returns true, `as_u64` is guaranteed to
99 /// return the integer value.
100 ///
101 /// ```rust
102 /// # #[macro_use]
103 /// # extern crate serde_json;
104 /// #
105 /// # fn main() {
106 /// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
107 ///
108 /// assert!(v["a"].is_u64());
109 ///
110 /// // Negative integer.
111 /// assert!(!v["b"].is_u64());
112 ///
113 /// // Numbers with a decimal point are not considered integers.
114 /// assert!(!v["c"].is_u64());
115 /// # }
116 /// ```
117 #[inline]
118 pub fn is_u64(&self) -> bool {
119 #[cfg(not(feature = "arbitrary_precision"))]
120 match self.n {
121 N::PosInt(_) => true,
122 N::NegInt(_) | N::Float(_) => false,
123 }
124 #[cfg(feature = "arbitrary_precision")]
125 self.as_u64().is_some()
126 }
127
128 /// Returns true if the `Number` can be represented by f64.
129 ///
130 /// For any Number on which `is_f64` returns true, `as_f64` is guaranteed to
131 /// return the floating point value.
132 ///
133 /// Currently this function returns true if and only if both `is_i64` and
134 /// `is_u64` return false but this is not a guarantee in the future.
135 ///
136 /// ```rust
137 /// # #[macro_use]
138 /// # extern crate serde_json;
139 /// #
140 /// # fn main() {
141 /// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
142 ///
143 /// assert!(v["a"].is_f64());
144 ///
145 /// // Integers.
146 /// assert!(!v["b"].is_f64());
147 /// assert!(!v["c"].is_f64());
148 /// # }
149 /// ```
150 #[inline]
151 pub fn is_f64(&self) -> bool {
152 #[cfg(not(feature = "arbitrary_precision"))]
153 match self.n {
154 N::Float(_) => true,
155 N::PosInt(_) | N::NegInt(_) => false,
156 }
157 #[cfg(feature = "arbitrary_precision")]
158 {
159 for c in self.n.chars() {
160 if c == '.' || c == 'e' || c == 'E' {
161 return self.n.parse::<f64>().ok()
162 .map_or(false, |f| f.is_finite());
163 }
164 }
165 false
166 }
167 }
168
169 /// If the `Number` is an integer, represent it as i64 if possible. Returns
170 /// None otherwise.
171 ///
172 /// ```rust
173 /// # #[macro_use]
174 /// # extern crate serde_json;
175 /// #
176 /// # use std::i64;
177 /// #
178 /// # fn main() {
179 /// let big = i64::MAX as u64 + 10;
180 /// let v = json!({ "a": 64, "b": big, "c": 256.0 });
181 ///
182 /// assert_eq!(v["a"].as_i64(), Some(64));
183 /// assert_eq!(v["b"].as_i64(), None);
184 /// assert_eq!(v["c"].as_i64(), None);
185 /// # }
186 /// ```
187 #[inline]
188 pub fn as_i64(&self) -> Option<i64> {
189 #[cfg(not(feature = "arbitrary_precision"))]
190 match self.n {
191 N::PosInt(n) => NumCast::from(n),
192 N::NegInt(n) => Some(n),
193 N::Float(_) => None,
194 }
195 #[cfg(feature = "arbitrary_precision")]
196 self.n.parse().ok()
197 }
198
199 /// If the `Number` is an integer, represent it as u64 if possible. Returns
200 /// None otherwise.
201 ///
202 /// ```rust
203 /// # #[macro_use]
204 /// # extern crate serde_json;
205 /// #
206 /// # fn main() {
207 /// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
208 ///
209 /// assert_eq!(v["a"].as_u64(), Some(64));
210 /// assert_eq!(v["b"].as_u64(), None);
211 /// assert_eq!(v["c"].as_u64(), None);
212 /// # }
213 /// ```
214 #[inline]
215 pub fn as_u64(&self) -> Option<u64> {
216 #[cfg(not(feature = "arbitrary_precision"))]
217 match self.n {
218 N::PosInt(n) => Some(n),
219 N::NegInt(n) => NumCast::from(n),
220 N::Float(_) => None,
221 }
222 #[cfg(feature = "arbitrary_precision")]
223 self.n.parse().ok()
224 }
225
226 /// Represents the number as f64 if possible. Returns None otherwise.
227 ///
228 /// ```rust
229 /// # #[macro_use]
230 /// # extern crate serde_json;
231 /// #
232 /// # fn main() {
233 /// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
234 ///
235 /// assert_eq!(v["a"].as_f64(), Some(256.0));
236 /// assert_eq!(v["b"].as_f64(), Some(64.0));
237 /// assert_eq!(v["c"].as_f64(), Some(-64.0));
238 /// # }
239 /// ```
240 #[inline]
241 pub fn as_f64(&self) -> Option<f64> {
242 #[cfg(not(feature = "arbitrary_precision"))]
243 match self.n {
244 N::PosInt(n) => NumCast::from(n),
245 N::NegInt(n) => NumCast::from(n),
246 N::Float(n) => Some(n),
247 }
248 #[cfg(feature = "arbitrary_precision")]
249 self.n.parse().ok()
250 }
251
252 /// Converts a finite `f64` to a `Number`. Infinite or NaN values are not JSON
253 /// numbers.
254 ///
255 /// ```rust
256 /// # use std::f64;
257 /// #
258 /// # use serde_json::Number;
259 /// #
260 /// assert!(Number::from_f64(256.0).is_some());
261 ///
262 /// assert!(Number::from_f64(f64::NAN).is_none());
263 /// ```
264 #[inline]
265 pub fn from_f64(f: f64) -> Option<Number> {
266 if f.is_finite() {
267 let n = {
268 #[cfg(not(feature = "arbitrary_precision"))]
269 { N::Float(f) }
270 #[cfg(feature = "arbitrary_precision")]
271 {
272 let mut buf = Vec::new();
273 dtoa::write(&mut buf, f).unwrap();
274 String::from_utf8(buf).unwrap()
275 }
276 };
277 Some(Number { n: n })
278 } else {
279 None
280 }
281 }
282
283 #[cfg(feature = "arbitrary_precision")]
284 /// Not public API. Only tests use this.
285 #[doc(hidden)]
286 #[inline]
287 pub fn from_string_unchecked(n: String) -> Self {
288 Number { n: n }
289 }
290 }
291
292 impl fmt::Display for Number {
293 #[cfg(not(feature = "arbitrary_precision"))]
294 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
295 match self.n {
296 N::PosInt(u) => Display::fmt(&u, formatter),
297 N::NegInt(i) => Display::fmt(&i, formatter),
298 N::Float(f) => Display::fmt(&f, formatter),
299 }
300 }
301
302 #[cfg(feature = "arbitrary_precision")]
303 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
304 Display::fmt(&self.n, formatter)
305 }
306 }
307
308 impl Debug for Number {
309 #[cfg(not(feature = "arbitrary_precision"))]
310 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
311 let mut debug = formatter.debug_tuple("Number");
312 match self.n {
313 N::PosInt(i) => {
314 debug.field(&i);
315 }
316 N::NegInt(i) => {
317 debug.field(&i);
318 }
319 N::Float(f) => {
320 debug.field(&f);
321 }
322 }
323 debug.finish()
324 }
325
326 #[cfg(feature = "arbitrary_precision")]
327 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
328 write!(formatter, "Number({})", &self.n)
329 }
330 }
331
332 impl Serialize for Number {
333 #[cfg(not(feature = "arbitrary_precision"))]
334 #[inline]
335 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
336 where
337 S: Serializer,
338 {
339 match self.n {
340 N::PosInt(u) => serializer.serialize_u64(u),
341 N::NegInt(i) => serializer.serialize_i64(i),
342 N::Float(f) => serializer.serialize_f64(f),
343 }
344 }
345
346 #[cfg(feature = "arbitrary_precision")]
347 #[inline]
348 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
349 where
350 S: Serializer,
351 {
352 use serde::ser::SerializeStruct;
353
354 let mut s = serializer.serialize_struct(SERDE_STRUCT_NAME, 1)?;
355 s.serialize_field(SERDE_STRUCT_FIELD_NAME, &self.n)?;
356 s.end()
357 }
358 }
359
360 impl<'de> Deserialize<'de> for Number {
361 #[inline]
362 fn deserialize<D>(deserializer: D) -> Result<Number, D::Error>
363 where
364 D: Deserializer<'de>,
365 {
366 struct NumberVisitor;
367
368 impl<'de> Visitor<'de> for NumberVisitor {
369 type Value = Number;
370
371 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
372 formatter.write_str("a JSON number")
373 }
374
375 #[inline]
376 fn visit_i64<E>(self, value: i64) -> Result<Number, E> {
377 Ok(value.into())
378 }
379
380 #[inline]
381 fn visit_u64<E>(self, value: u64) -> Result<Number, E> {
382 Ok(value.into())
383 }
384
385 #[inline]
386 fn visit_f64<E>(self, value: f64) -> Result<Number, E>
387 where
388 E: de::Error,
389 {
390 Number::from_f64(value).ok_or_else(|| de::Error::custom("not a JSON number"))
391 }
392
393 #[cfg(feature = "arbitrary_precision")]
394 #[inline]
395 fn visit_map<V>(self, mut visitor: V) -> Result<Number, V::Error>
396 where
397 V: de::MapAccess<'de>
398 {
399 let value = visitor.next_key::<NumberKey>()?;
400 if value.is_none() {
401 return Err(de::Error::invalid_type(Unexpected::Map, &self));
402 }
403 let v: NumberFromString = visitor.next_value()?;
404 Ok(v.value)
405 }
406 }
407
408 deserializer.deserialize_any(NumberVisitor)
409 }
410 }
411
412 #[cfg(feature = "arbitrary_precision")]
413 struct NumberKey;
414
415 #[cfg(feature = "arbitrary_precision")]
416 impl<'de> de::Deserialize<'de> for NumberKey {
417 fn deserialize<D>(deserializer: D) -> Result<NumberKey, D::Error>
418 where
419 D: de::Deserializer<'de>
420 {
421 struct FieldVisitor;
422
423 impl<'de> de::Visitor<'de> for FieldVisitor {
424 type Value = ();
425
426 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
427 formatter.write_str("a valid number field")
428 }
429
430 fn visit_str<E>(self, s: &str) -> Result<(), E>
431 where E: de::Error
432 {
433 if s == SERDE_STRUCT_FIELD_NAME {
434 Ok(())
435 } else {
436 Err(de::Error::custom("expected field with custom name"))
437 }
438 }
439 }
440
441 deserializer.deserialize_identifier(FieldVisitor)?;
442 Ok(NumberKey)
443 }
444 }
445
446 #[cfg(feature = "arbitrary_precision")]
447 pub struct NumberFromString {
448 pub value: Number,
449 }
450
451 #[cfg(feature = "arbitrary_precision")]
452 impl<'de> de::Deserialize<'de> for NumberFromString {
453 fn deserialize<D>(deserializer: D) -> Result<NumberFromString, D::Error>
454 where
455 D: de::Deserializer<'de>
456 {
457 struct Visitor;
458
459 impl<'de> de::Visitor<'de> for Visitor {
460 type Value = NumberFromString;
461
462 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
463 formatter.write_str("string containing a number")
464 }
465
466 fn visit_str<E>(self, s: &str) -> Result<NumberFromString, E>
467 where E: de::Error,
468 {
469 let n = try!(s.parse().map_err(de::Error::custom));
470 Ok(NumberFromString { value: n })
471 }
472 }
473
474 deserializer.deserialize_str(Visitor)
475 }
476 }
477
478 #[cfg(feature = "arbitrary_precision")]
479 fn invalid_number() -> Error {
480 Error::syntax(ErrorCode::InvalidNumber, 0, 0)
481 }
482
483 macro_rules! deserialize_any {
484 (@expand [$($num_string:tt)*]) => {
485 #[cfg(not(feature = "arbitrary_precision"))]
486 #[inline]
487 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
488 where
489 V: Visitor<'de>,
490 {
491 match self.n {
492 N::PosInt(u) => visitor.visit_u64(u),
493 N::NegInt(i) => visitor.visit_i64(i),
494 N::Float(f) => visitor.visit_f64(f),
495 }
496 }
497
498 #[cfg(feature = "arbitrary_precision")]
499 #[inline]
500 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
501 where V: Visitor<'de>
502 {
503 if let Some(u) = self.as_u64() {
504 return visitor.visit_u64(u);
505 } else if let Some(i) = self.as_i64() {
506 return visitor.visit_i64(i);
507 } else if let Some(f) = self.as_f64() {
508 if f.to_string() == self.n {
509 return visitor.visit_f64(f);
510 }
511 }
512
513 visitor.visit_map(NumberDeserializer {
514 number: Some(self.$($num_string)*),
515 })
516 }
517 };
518
519 (owned) => {
520 deserialize_any!(@expand [n]);
521 };
522
523 (ref) => {
524 deserialize_any!(@expand [n.clone()]);
525 };
526 }
527
528 macro_rules! deserialize_number {
529 ($deserialize:ident => $visit:ident) => {
530 #[cfg(not(feature = "arbitrary_precision"))]
531 fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
532 where
533 V: Visitor<'de>,
534 {
535 self.deserialize_any(visitor)
536 }
537
538 #[cfg(feature = "arbitrary_precision")]
539 fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
540 where
541 V: de::Visitor<'de>,
542 {
543 visitor.$visit(self.n.parse().map_err(|_| invalid_number())?)
544 }
545 }
546 }
547
548 impl<'de> Deserializer<'de> for Number {
549 type Error = Error;
550
551 deserialize_any!(owned);
552
553 deserialize_number!(deserialize_i8 => visit_i8);
554 deserialize_number!(deserialize_i16 => visit_i16);
555 deserialize_number!(deserialize_i32 => visit_i32);
556 deserialize_number!(deserialize_i64 => visit_i64);
557 deserialize_number!(deserialize_u8 => visit_u8);
558 deserialize_number!(deserialize_u16 => visit_u16);
559 deserialize_number!(deserialize_u32 => visit_u32);
560 deserialize_number!(deserialize_u64 => visit_u64);
561 deserialize_number!(deserialize_f32 => visit_f32);
562 deserialize_number!(deserialize_f64 => visit_f64);
563
564 forward_to_deserialize_any! {
565 bool char str string bytes byte_buf option unit unit_struct
566 newtype_struct seq tuple tuple_struct map struct enum identifier
567 ignored_any
568 }
569 }
570
571 impl<'de, 'a> Deserializer<'de> for &'a Number {
572 type Error = Error;
573
574 deserialize_any!(ref);
575
576 deserialize_number!(deserialize_i8 => visit_i8);
577 deserialize_number!(deserialize_i16 => visit_i16);
578 deserialize_number!(deserialize_i32 => visit_i32);
579 deserialize_number!(deserialize_i64 => visit_i64);
580 deserialize_number!(deserialize_u8 => visit_u8);
581 deserialize_number!(deserialize_u16 => visit_u16);
582 deserialize_number!(deserialize_u32 => visit_u32);
583 deserialize_number!(deserialize_u64 => visit_u64);
584 deserialize_number!(deserialize_f32 => visit_f32);
585 deserialize_number!(deserialize_f64 => visit_f64);
586
587 forward_to_deserialize_any! {
588 bool char str string bytes byte_buf option unit unit_struct
589 newtype_struct seq tuple tuple_struct map struct enum identifier
590 ignored_any
591 }
592 }
593
594 #[cfg(feature = "arbitrary_precision")]
595 // Not public API. Should be pub(crate).
596 #[doc(hidden)]
597 pub struct NumberDeserializer {
598 pub number: Option<String>,
599 }
600
601 #[cfg(feature = "arbitrary_precision")]
602 impl<'de> MapAccess<'de> for NumberDeserializer {
603 type Error = Error;
604
605 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
606 where
607 K: de::DeserializeSeed<'de>,
608 {
609 if self.number.is_none() {
610 return Ok(None)
611 }
612 seed.deserialize(NumberFieldDeserializer).map(Some)
613 }
614
615 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
616 where
617 V: de::DeserializeSeed<'de>,
618 {
619 seed.deserialize(self.number.take().unwrap().into_deserializer())
620 }
621 }
622
623 #[cfg(feature = "arbitrary_precision")]
624 struct NumberFieldDeserializer;
625
626 #[cfg(feature = "arbitrary_precision")]
627 impl<'de> Deserializer<'de> for NumberFieldDeserializer {
628 type Error = Error;
629
630 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
631 where
632 V: de::Visitor<'de>,
633 {
634 visitor.visit_borrowed_str(SERDE_STRUCT_FIELD_NAME)
635 }
636
637 forward_to_deserialize_any! {
638 bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
639 bytes byte_buf map struct option unit newtype_struct
640 ignored_any unit_struct tuple_struct tuple enum identifier
641 }
642 }
643
644 impl FromStr for Number {
645 type Err = Error;
646
647 fn from_str(s: &str) -> Result<Self, Self::Err> {
648 super::de::Deserializer::from_str(s).parse_any_signed_number().map(|n| n.into())
649 }
650 }
651
652 impl From<super::de::Number> for Number {
653 fn from(value: super::de::Number) -> Self {
654 let n = match value {
655 super::de::Number::F64(f) => {
656 #[cfg(not(feature = "arbitrary_precision"))]
657 { N::Float(f) }
658 #[cfg(feature = "arbitrary_precision")]
659 { f.to_string() }
660 },
661 super::de::Number::U64(u) => {
662 #[cfg(not(feature = "arbitrary_precision"))]
663 { N::PosInt(u) }
664 #[cfg(feature = "arbitrary_precision")]
665 { u.to_string() }
666 },
667 super::de::Number::I64(i) => {
668 #[cfg(not(feature = "arbitrary_precision"))]
669 { N::NegInt(i) }
670 #[cfg(feature = "arbitrary_precision")]
671 { i.to_string() }
672 },
673 #[cfg(feature = "arbitrary_precision")]
674 super::de::Number::String(s) => s,
675 };
676 Number { n: n }
677 }
678 }
679
680 macro_rules! impl_from_unsigned {
681 (
682 $($ty:ty),*
683 ) => {
684 $(
685 impl From<$ty> for Number {
686 #[inline]
687 fn from(u: $ty) -> Self {
688 let n = {
689 #[cfg(not(feature = "arbitrary_precision"))]
690 { N::PosInt(u as u64) }
691 #[cfg(feature = "arbitrary_precision")]
692 {
693 let mut buf = Vec::new();
694 itoa::write(&mut buf, u).unwrap();
695 String::from_utf8(buf).unwrap()
696 }
697 };
698 Number { n: n }
699 }
700 }
701 )*
702 };
703 }
704
705 macro_rules! impl_from_signed {
706 (
707 $($ty:ty),*
708 ) => {
709 $(
710 impl From<$ty> for Number {
711 #[inline]
712 fn from(i: $ty) -> Self {
713 let n = {
714 #[cfg(not(feature = "arbitrary_precision"))]
715 {
716 if i < 0 {
717 N::NegInt(i as i64)
718 } else {
719 N::PosInt(i as u64)
720 }
721 }
722 #[cfg(feature = "arbitrary_precision")]
723 {
724 let mut buf = Vec::new();
725 itoa::write(&mut buf, i).unwrap();
726 String::from_utf8(buf).unwrap()
727 }
728 };
729 Number { n: n }
730 }
731 }
732 )*
733 };
734 }
735
736 impl_from_unsigned!(u8, u16, u32, u64, usize);
737 impl_from_signed!(i8, i16, i32, i64, isize);
738
739 impl Number {
740 #[cfg(not(feature = "arbitrary_precision"))]
741 // Not public API. Should be pub(crate).
742 #[doc(hidden)]
743 pub fn unexpected(&self) -> Unexpected {
744 match self.n {
745 N::PosInt(u) => Unexpected::Unsigned(u),
746 N::NegInt(i) => Unexpected::Signed(i),
747 N::Float(f) => Unexpected::Float(f),
748 }
749 }
750
751 #[cfg(feature = "arbitrary_precision")]
752 // Not public API. Should be pub(crate).
753 #[doc(hidden)]
754 pub fn unexpected(&self) -> Unexpected {
755 Unexpected::Other("number")
756 }
757 }