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