use core::{
cmp::{Eq, Ordering},
hash::{Hash, Hasher},
};
use serde::{de::Visitor, Serialize, Serializer};
/// A wrapper for any numeric primitive type in Rust. /// /// Some varints of the `Number` enum are enabled by features: /// - `Number::I128` and `Number::U128` by the `integer128` feature /// /// To ensure that feature unification does not break `match`ing over `Number`, /// the `Number` enum is non-exhaustive. /// /// <details> /// <summary>Exhaustively matching on <code>Number</code> in tests</summary> /// /// If you want to ensure that you exhaustively handle every variant, you can /// match on the hidden `Number::__NonExhaustive(x)` variant by using the /// `x.never() -> !` method. /// /// <div class="warning"> /// Matching on this variant means that your code may break when RON is /// upgraded or when feature unification enables further variants in the /// <code>Number</code> enum than your code expects. /// </div> /// /// It is your responsibility to only *ever* match on `Number::__NonExhaustive` /// inside tests, e.g. by using `#[cfg(test)]` on the particular match arm, to /// ensure that only your tests break (e.g. in CI) when your code is not /// exhaustively matching on every variant, e.g. after a version upgrade or /// feature unification. /// </details> #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Hash, Ord)] #[cfg_attr(doc, non_exhaustive)] pubenum Number {
I8(i8),
I16(i16),
I32(i32),
I64(i64), #[cfg(feature = "integer128")]
I128(i128),
U8(u8),
U16(u16),
U32(u32),
U64(u64), #[cfg(feature = "integer128")]
U128(u128),
F32(F32),
F64(F64), #[cfg(not(doc))] #[allow(private_interfaces)]
__NonExhaustive(private::Never),
}
/// Partial equality comparison /// #[doc = concat!( "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", "floating values use [`", stringify!($float), "::total_cmp`] for a total ", "order comparison.",
)] /// /// See the [`Ord`] implementation. impl PartialEq for $ty { fn eq(&self, other: &Self) -> bool { self.cmp(other).is_eq()
}
}
/// Equality comparison /// #[doc = concat!( "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", "floating values use [`", stringify!($float), "::total_cmp`] for a total ", "order comparison.",
)] /// /// See the [`Ord`] implementation. impl Eq for $ty {}
/// Partial ordering comparison /// #[doc = concat!( "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", "floating values use [`", stringify!($float), "::total_cmp`] for a total ", "order comparison.",
)] /// /// See the [`Ord`] implementation. impl PartialOrd for $ty { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// Ordering comparison /// #[doc = concat!( "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", "floating values use [`", stringify!($float), "::total_cmp`] for a total ", "order comparison.",
)] /// /// ``` #[doc = concat!("use ron::value::", stringify!($ty), ";")] #[doc = concat!( "assert!(", stringify!($ty), "::new(", stringify!($float), "::NAN) > ",
stringify!($ty), "::new(", stringify!($float), "::INFINITY));",
)] #[doc = concat!( "assert!(", stringify!($ty), "::new(-", stringify!($float), "::NAN) < ",
stringify!($ty), "::new(", stringify!($float), "::NEG_INFINITY));",
)] #[doc = concat!( "assert!(", stringify!($ty), "::new(", stringify!($float), "::NAN) == ",
stringify!($ty), "::new(", stringify!($float), "::NAN));",
)] /// ``` impl Ord for $ty { fn cmp(&self, other: &Self) -> Ordering { self.0.total_cmp(&other.0)
}
}
};
}
float_ty! { F32(f32) }
float_ty! { F64(f64) }
impl Number { /// Construct a new number. pubfn new(v: impl Into<Number>) -> Self {
v.into()
}
/// Returns the [`f64`] representation of the [`Number`] regardless of /// whether the number is stored as a float or integer. /// /// # Example /// /// ``` /// # use ron::value::Number; /// let i = Number::new(5); /// let f = Number::new(2.0); /// assert_eq!(i.into_f64(), 5.0); /// assert_eq!(f.into_f64(), 2.0); /// ``` #[must_use] pubfn into_f64(self) -> f64 { #[allow(clippy::cast_precision_loss)] matchself { Self::I8(v) => f64::from(v), Self::I16(v) => f64::from(v), Self::I32(v) => f64::from(v), Self::I64(v) => v as f64, #[cfg(feature = "integer128")] Self::I128(v) => v as f64, Self::U8(v) => f64::from(v), Self::U16(v) => f64::from(v), Self::U32(v) => f64::from(v), Self::U64(v) => v as f64, #[cfg(feature = "integer128")] Self::U128(v) => v as f64, Self::F32(v) => f64::from(v.get()), Self::F64(v) => v.get(), #[cfg(not(doc))] Self::__NonExhaustive(never) => never.never(),
}
}
}
macro_rules! number_from_impl {
(Number::$variant:ident($wrap:ident($ty:ty))) => { impl From<$ty> for Number { fn from(v: $ty) -> Number {
Number::$variant($wrap(v))
}
}
};
(Number::$variant:ident($ty:ty)) => { impl From<$ty> for Number { fn from(v: $ty) -> Number {
Number::$variant(v)
}
}
};
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.