| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{ |
| 2 | cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#}, |
| 3 | hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::{Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#}, |
| 4 | }; |
| 5 | |
| 6 | use serderust-analyzer cargo serde 1.0.228 crate/::{derust-analyzer cargo serde_core 1.0.228 de/::Visitorrust-analyzer cargo serde_core 1.0.228 de/Visitor#, Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, Serializerrust-analyzer cargo serde_core 1.0.228 ser/Serializer#}; |
| 7 | |
| 8 | /// A wrapper for any numeric primitive type in Rust. |
| 9 | /// |
| 10 | /// Some varints of the `Number` enum are enabled by features: |
| 11 | /// - `Number::I128` and `Number::U128` by the `integer128` feature |
| 12 | /// |
| 13 | /// To ensure that feature unification does not break `match`ing over `Number`, |
| 14 | /// the `Number` enum is non-exhaustive. |
| 15 | /// |
| 16 | /// <details> |
| 17 | /// <summary>Exhaustively matching on <code>Number</code> in tests</summary> |
| 18 | /// |
| 19 | /// If you want to ensure that you exhaustively handle every variant, you can |
| 20 | /// match on the hidden `Number::__NonExhaustive(x)` variant by using the |
| 21 | /// `x.never() -> !` method. |
| 22 | /// |
| 23 | /// <div class="warning"> |
| 24 | /// Matching on this variant means that your code may break when RON is |
| 25 | /// upgraded or when feature unification enables further variants in the |
| 26 | /// <code>Number</code> enum than your code expects. |
| 27 | /// </div> |
| 28 | /// |
| 29 | /// It is your responsibility to only *ever* match on `Number::__NonExhaustive` |
| 30 | /// inside tests, e.g. by using `#[cfg(test)]` on the particular match arm, to |
| 31 | /// ensure that only your tests break (e.g. in CI) when your code is not |
| 32 | /// exhaustively matching on every variant, e.g. after a version upgrade or |
| 33 | /// feature unification. |
| 34 | /// </details> |
| 35 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 36 | #[cfg_attr(doc, non_exhaustive)] |
| 37 | pub enum Numberrust-analyzer cargo ron 0.12.0 value/number/Number# { |
| 38 | I8rust-analyzer cargo ron 0.12.0 value/number/Number#I8#(i8), |
| 39 | I16rust-analyzer cargo ron 0.12.0 value/number/Number#I16#(i16), |
| 40 | I32rust-analyzer cargo ron 0.12.0 value/number/Number#I32#(i32), |
| 41 | I64rust-analyzer cargo ron 0.12.0 value/number/Number#I64#(i64), |
| 42 | #[cfg(feature = "integer128")] |
| 43 | I128rust-analyzer cargo ron 0.12.0 value/number/Number#I128#(i128), |
| 44 | U8rust-analyzer cargo ron 0.12.0 value/number/Number#U8#(u8), |
| 45 | U16rust-analyzer cargo ron 0.12.0 value/number/Number#U16#(u16), |
| 46 | U32rust-analyzer cargo ron 0.12.0 value/number/Number#U32#(u32), |
| 47 | U64rust-analyzer cargo ron 0.12.0 value/number/Number#U64#(u64), |
| 48 | #[cfg(feature = "integer128")] |
| 49 | U128rust-analyzer cargo ron 0.12.0 value/number/Number#U128#(u128), |
| 50 | F32rust-analyzer cargo ron 0.12.0 value/number/Number#F32#(F32rust-analyzer cargo ron 0.12.0 value/number/F32#), |
| 51 | F64rust-analyzer cargo ron 0.12.0 value/number/Number#F64#(F64rust-analyzer cargo ron 0.12.0 value/number/F64#), |
| 52 | #[cfg(not(doc))] |
| 53 | #[allow(private_interfaces)] |
| 54 | __NonExhaustiverust-analyzer cargo ron 0.12.0 value/number/Number#__NonExhaustive#(privaterust-analyzer cargo ron 0.12.0 value/number/private/::Neverrust-analyzer cargo ron 0.12.0 value/number/private/Never#), |
| 55 | } |
| 56 | |
| 57 | mod privaterust-analyzer cargo ron 0.12.0 value/number/private/ { |
| 58 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 59 | enum _Neverrust-analyzer cargo ron 0.12.0 value/number/private/_Never# {} |
| 60 | |
| 61 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 62 | pub struct Neverrust-analyzer cargo ron 0.12.0 value/number/private/Never# { |
| 63 | neverrust-analyzer cargo ron 0.12.0 value/number/private/Never#never.: &'static _Neverrust-analyzer cargo ron 0.12.0 value/number/private/_Never#, |
| 64 | } |
| 65 | |
| 66 | impl Neverrust-analyzer cargo ron 0.12.0 value/number/private/Never# { |
| 67 | pub fn neverrust-analyzer cargo ron 0.12.0 value/number/private/impl#[Never]never().(self) -> ! { |
| 68 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self.neverrust-analyzer cargo ron 0.12.0 value/number/private/Never#never. {} |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[cfg(not(feature = "integer128"))] |
| 73 | /// ```compile_fail |
| 74 | /// # use ron::Number; |
| 75 | /// fn match_number(x: Number) { |
| 76 | /// match x { |
| 77 | /// Number::I8(v) => println!("i8: {}", v), |
| 78 | /// Number::I16(v) => println!("i16: {}", v), |
| 79 | /// Number::I32(v) => println!("i32: {}", v), |
| 80 | /// Number::I64(v) => println!("i64: {}", v), |
| 81 | /// Number::U8(v) => println!("u8: {}", v), |
| 82 | /// Number::U16(v) => println!("u16: {}", v), |
| 83 | /// Number::U32(v) => println!("u32: {}", v), |
| 84 | /// Number::U64(v) => println!("u64: {}", v), |
| 85 | /// Number::F32(v) => println!("f32: {}", v.0), |
| 86 | /// Number::F64(v) => println!("f64: {}", v.0), |
| 87 | /// } |
| 88 | /// } |
| 89 | /// ``` |
| 90 | fn _assert_non_exhaustive_check_fails_not_integer128() {} |
| 91 | |
| 92 | #[cfg(feature = "integer128")] |
| 93 | /// ```compile_fail |
| 94 | /// # use ron::Number; |
| 95 | /// fn match_number(x: Number) { |
| 96 | /// match x { |
| 97 | /// Number::I8(v) => println!("i8: {}", v), |
| 98 | /// Number::I16(v) => println!("i16: {}", v), |
| 99 | /// Number::I32(v) => println!("i32: {}", v), |
| 100 | /// Number::I64(v) => println!("i64: {}", v), |
| 101 | /// Number::I128(v) => println!("i128: {}", v), |
| 102 | /// Number::U8(v) => println!("u8: {}", v), |
| 103 | /// Number::U16(v) => println!("u16: {}", v), |
| 104 | /// Number::U32(v) => println!("u32: {}", v), |
| 105 | /// Number::U64(v) => println!("u64: {}", v), |
| 106 | /// Number::U128(v) => println!("u128: {}", v), |
| 107 | /// Number::F32(v) => println!("f32: {}", v.0), |
| 108 | /// Number::F64(v) => println!("f64: {}", v.0), |
| 109 | /// } |
| 110 | /// } |
| 111 | /// ``` |
| 112 | fn _assert_non_exhaustive_check_fails_integer128rust-analyzer cargo ron 0.12.0 value/number/private/_assert_non_exhaustive_check_fails_integer128().() {} |
| 113 | } |
| 114 | |
| 115 | impl Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize# for Numberrust-analyzer cargo ron 0.12.0 value/number/Number# { |
| 116 | fn serializerust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]serialize().<S: Serializerrust-analyzer cargo serde_core 1.0.228 ser/Serializer#>(&self, serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Error#> { |
| 117 | match self { |
| 118 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::I8rust-analyzer cargo ron 0.12.0 value/number/Number#I8#(v) => serializer.serialize_i8rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_i8().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 119 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::I16rust-analyzer cargo ron 0.12.0 value/number/Number#I16#(v) => serializer.serialize_i16rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_i16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 120 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::I32rust-analyzer cargo ron 0.12.0 value/number/Number#I32#(v) => serializer.serialize_i32rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_i32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 121 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::I64rust-analyzer cargo ron 0.12.0 value/number/Number#I64#(v) => serializer.serialize_i64rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_i64().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 122 | #[cfg(feature = "integer128")] |
| 123 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::I128rust-analyzer cargo ron 0.12.0 value/number/Number#I128#(v) => serializer.serialize_i128rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_i128().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 124 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::U8rust-analyzer cargo ron 0.12.0 value/number/Number#U8#(v) => serializer.serialize_u8rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_u8().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 125 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::U16rust-analyzer cargo ron 0.12.0 value/number/Number#U16#(v) => serializer.serialize_u16rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_u16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 126 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::U32rust-analyzer cargo ron 0.12.0 value/number/Number#U32#(v) => serializer.serialize_u32rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 127 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::U64rust-analyzer cargo ron 0.12.0 value/number/Number#U64#(v) => serializer.serialize_u64rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_u64().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 128 | #[cfg(feature = "integer128")] |
| 129 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::U128rust-analyzer cargo ron 0.12.0 value/number/Number#U128#(v) => serializer.serialize_u128rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_u128().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 130 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::F32rust-analyzer cargo ron 0.12.0 value/number/Number#F32#(v) => serializer.serialize_f32rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_f32().(v.getrust-analyzer cargo ron 0.12.0 value/number/impl#[F32]get().()), |
| 131 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::F64rust-analyzer cargo ron 0.12.0 value/number/Number#F64#(v) => serializer.serialize_f64rust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_f64().(v.getrust-analyzer cargo ron 0.12.0 value/number/impl#[F64]get().()), |
| 132 | #[cfg(not(doc))] |
| 133 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number][Serialize]::__NonExhaustiverust-analyzer cargo ron 0.12.0 value/number/Number#__NonExhaustive#(never) => never.neverrust-analyzer cargo ron 0.12.0 value/number/private/impl#[Never]never().(), |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl Numberrust-analyzer cargo ron 0.12.0 value/number/Number# { |
| 139 | pub fn visitrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]visit().<'de, V: Visitorrust-analyzer cargo serde_core 1.0.228 de/Visitor#<'de>, E: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#>( |
| 140 | &self, |
| 141 | visitor: V, |
| 142 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<V::Valuerust-analyzer cargo serde_core 1.0.228 de/Visitor#Value#, E> { |
| 143 | match self { |
| 144 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I8rust-analyzer cargo ron 0.12.0 value/number/Number#I8#(v) => visitor.visit_i8rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_i8().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 145 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I16rust-analyzer cargo ron 0.12.0 value/number/Number#I16#(v) => visitor.visit_i16rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_i16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 146 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I32rust-analyzer cargo ron 0.12.0 value/number/Number#I32#(v) => visitor.visit_i32rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_i32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 147 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I64rust-analyzer cargo ron 0.12.0 value/number/Number#I64#(v) => visitor.visit_i64rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_i64().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 148 | #[cfg(feature = "integer128")] |
| 149 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I128rust-analyzer cargo ron 0.12.0 value/number/Number#I128#(v) => visitor.visit_i128rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_i128().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 150 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U8rust-analyzer cargo ron 0.12.0 value/number/Number#U8#(v) => visitor.visit_u8rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_u8().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 151 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U16rust-analyzer cargo ron 0.12.0 value/number/Number#U16#(v) => visitor.visit_u16rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_u16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 152 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U32rust-analyzer cargo ron 0.12.0 value/number/Number#U32#(v) => visitor.visit_u32rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 153 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U64rust-analyzer cargo ron 0.12.0 value/number/Number#U64#(v) => visitor.visit_u64rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_u64().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 154 | #[cfg(feature = "integer128")] |
| 155 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U128rust-analyzer cargo ron 0.12.0 value/number/Number#U128#(v) => visitor.visit_u128rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_u128().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v), |
| 156 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::F32rust-analyzer cargo ron 0.12.0 value/number/Number#F32#(v) => visitor.visit_f32rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_f32().(v.getrust-analyzer cargo ron 0.12.0 value/number/impl#[F32]get().()), |
| 157 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::F64rust-analyzer cargo ron 0.12.0 value/number/Number#F64#(v) => visitor.visit_f64rust-analyzer cargo serde_core 1.0.228 de/Visitor#visit_f64().(v.getrust-analyzer cargo ron 0.12.0 value/number/impl#[F64]get().()), |
| 158 | #[cfg(not(doc))] |
| 159 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::__NonExhaustiverust-analyzer cargo ron 0.12.0 value/number/Number#__NonExhaustive#(never) => never.neverrust-analyzer cargo ron 0.12.0 value/number/private/impl#[Never]never().(), |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | macro_rules! float_tyrust-analyzer cargo ron 0.12.0 value/number/float_ty! { |
| 165 | ($ty:ident($float:ty)) => { |
| 166 | #[doc = concat!( |
| 167 | "A wrapper for [`", stringify!($float), "`], which implements [`Eq`], ", |
| 168 | "[`Hash`] and [`Ord`] using [`", stringify!($float), "::total_cmp`] ", |
| 169 | "for a total order comparison", |
| 170 | )] |
| 171 | #[derive(Copy, Clone, Debug)] // GRCOV_EXCL_LINE |
| 172 | pub struct $ty(pub $float); |
| 173 | |
| 174 | impl $ty { |
| 175 | #[doc = concat!("Construct a new [`", stringify!($ty), "`].")] |
| 176 | #[must_use] |
| 177 | pub fn new(v: $float) -> Self { |
| 178 | Self(v) |
| 179 | } |
| 180 | |
| 181 | #[doc = concat!("Returns the wrapped [`", stringify!($float), "`].")] |
| 182 | #[must_use] |
| 183 | pub fn get(self) -> $float { |
| 184 | self.0 |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | impl From<$float> for $ty { |
| 189 | fn from(v: $float) -> Self { |
| 190 | Self::new(v) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// Partial equality comparison |
| 195 | /// |
| 196 | #[doc = concat!( |
| 197 | "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", |
| 198 | "floating values use [`", stringify!($float), "::total_cmp`] for a total ", |
| 199 | "order comparison.", |
| 200 | )] |
| 201 | /// |
| 202 | /// See the [`Ord`] implementation. |
| 203 | impl PartialEq for $ty { |
| 204 | fn eq(&self, other: &Self) -> bool { |
| 205 | self.cmp(other).is_eq() |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /// Equality comparison |
| 210 | /// |
| 211 | #[doc = concat!( |
| 212 | "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", |
| 213 | "floating values use [`", stringify!($float), "::total_cmp`] for a total ", |
| 214 | "order comparison.", |
| 215 | )] |
| 216 | /// |
| 217 | /// See the [`Ord`] implementation. |
| 218 | impl Eq for $ty {} |
| 219 | |
| 220 | impl Hash for $ty { |
| 221 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 222 | self.0.to_bits().hash(state); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /// Partial ordering comparison |
| 227 | /// |
| 228 | #[doc = concat!( |
| 229 | "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", |
| 230 | "floating values use [`", stringify!($float), "::total_cmp`] for a total ", |
| 231 | "order comparison.", |
| 232 | )] |
| 233 | /// |
| 234 | /// See the [`Ord`] implementation. |
| 235 | impl PartialOrd for $ty { |
| 236 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 237 | Some(self.cmp(other)) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// Ordering comparison |
| 242 | /// |
| 243 | #[doc = concat!( |
| 244 | "In order to be able to use [`", stringify!($ty), "`] as a mapping key, ", |
| 245 | "floating values use [`", stringify!($float), "::total_cmp`] for a total ", |
| 246 | "order comparison.", |
| 247 | )] |
| 248 | /// |
| 249 | /// ``` |
| 250 | #[doc = concat!("use ron::value::", stringify!($ty), ";")] |
| 251 | #[doc = concat!( |
| 252 | "assert!(", stringify!($ty), "::new(", stringify!($float), "::NAN) > ", |
| 253 | stringify!($ty), "::new(", stringify!($float), "::INFINITY));", |
| 254 | )] |
| 255 | #[doc = concat!( |
| 256 | "assert!(", stringify!($ty), "::new(-", stringify!($float), "::NAN) < ", |
| 257 | stringify!($ty), "::new(", stringify!($float), "::NEG_INFINITY));", |
| 258 | )] |
| 259 | #[doc = concat!( |
| 260 | "assert!(", stringify!($ty), "::new(", stringify!($float), "::NAN) == ", |
| 261 | stringify!($ty), "::new(", stringify!($float), "::NAN));", |
| 262 | )] |
| 263 | /// ``` |
| 264 | impl Ord for $ty { |
| 265 | fn cmp(&self, other: &Self) -> Ordering { |
| 266 | self.0.total_cmp(&other.0) |
| 267 | } |
| 268 | } |
| 269 | }; |
| 270 | } |
| 271 | |
| 272 | float_tyrust-analyzer cargo ron 0.12.0 value/number/float_ty!! { F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32) } |
| 273 | float_tyrust-analyzer cargo ron 0.12.0 value/number/float_ty!! { F64rust-analyzer cargo ron 0.12.0 value/number/F64#(f64) } |
| 274 | |
| 275 | impl Numberrust-analyzer cargo ron 0.12.0 value/number/Number# { |
| 276 | /// Construct a new number. |
| 277 | pub fn newrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]new().(v: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Numberrust-analyzer cargo ron 0.12.0 value/number/Number#>) -> Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number] { |
| 278 | v.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() |
| 279 | } |
| 280 | |
| 281 | /// Returns the [`f64`] representation of the [`Number`] regardless of |
| 282 | /// whether the number is stored as a float or integer. |
| 283 | /// |
| 284 | /// # Example |
| 285 | /// |
| 286 | /// ``` |
| 287 | /// # use ron::value::Number; |
| 288 | /// let i = Number::new(5); |
| 289 | /// let f = Number::new(2.0); |
| 290 | /// assert_eq!(i.into_f64(), 5.0); |
| 291 | /// assert_eq!(f.into_f64(), 2.0); |
| 292 | /// ``` |
| 293 | #[must_use] |
| 294 | pub fn into_f64rust-analyzer cargo ron 0.12.0 value/number/impl#[Number]into_f64().(self) -> f64 { |
| 295 | #[allow(clippy::cast_precision_loss)] |
| 296 | match self { |
| 297 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I8rust-analyzer cargo ron 0.12.0 value/number/Number#I8#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<i8>`]from().(v), |
| 298 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I16rust-analyzer cargo ron 0.12.0 value/number/Number#I16#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<i16>`]from().(v), |
| 299 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I32rust-analyzer cargo ron 0.12.0 value/number/Number#I32#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<i32>`]from().(v), |
| 300 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I64rust-analyzer cargo ron 0.12.0 value/number/Number#I64#(v) => v as f64, |
| 301 | #[cfg(feature = "integer128")] |
| 302 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::I128rust-analyzer cargo ron 0.12.0 value/number/Number#I128#(v) => v as f64, |
| 303 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U8rust-analyzer cargo ron 0.12.0 value/number/Number#U8#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<u8>`]from().(v), |
| 304 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U16rust-analyzer cargo ron 0.12.0 value/number/Number#U16#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<u16>`]from().(v), |
| 305 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U32rust-analyzer cargo ron 0.12.0 value/number/Number#U32#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<u32>`]from().(v), |
| 306 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U64rust-analyzer cargo ron 0.12.0 value/number/Number#U64#(v) => v as f64, |
| 307 | #[cfg(feature = "integer128")] |
| 308 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::U128rust-analyzer cargo ron 0.12.0 value/number/Number#U128#(v) => v as f64, |
| 309 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::F32rust-analyzer cargo ron 0.12.0 value/number/Number#F32#(v) => f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(v.getrust-analyzer cargo ron 0.12.0 value/number/impl#[F32]get().()), |
| 310 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::F64rust-analyzer cargo ron 0.12.0 value/number/Number#F64#(v) => v.getrust-analyzer cargo ron 0.12.0 value/number/impl#[F64]get().(), |
| 311 | #[cfg(not(doc))] |
| 312 | Selfrust-analyzer cargo ron 0.12.0 value/number/impl#[Number]::__NonExhaustiverust-analyzer cargo ron 0.12.0 value/number/Number#__NonExhaustive#(never) => never.neverrust-analyzer cargo ron 0.12.0 value/number/private/impl#[Never]never().(), |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | macro_rules! number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl! { |
| 318 | (Number::$variant:ident($wrap:ident($ty:ty))) => { |
| 319 | impl From<$ty> for Number { |
| 320 | fn from(v: $ty) -> Number { |
| 321 | Number::$variant($wrap(v)) |
| 322 | } |
| 323 | } |
| 324 | }; |
| 325 | (Number::$variant:ident($ty:ty)) => { |
| 326 | impl From<$ty> for Number { |
| 327 | fn from(v: $ty) -> Number { |
| 328 | Number::$variant(v) |
| 329 | } |
| 330 | } |
| 331 | }; |
| 332 | } |
| 333 | |
| 334 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::I8rust-analyzer cargo ron 0.12.0 value/number/Number#I8#(i8) } |
| 335 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::I16rust-analyzer cargo ron 0.12.0 value/number/Number#I16#(i16) } |
| 336 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::I32rust-analyzer cargo ron 0.12.0 value/number/Number#I32#(i32) } |
| 337 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::I64rust-analyzer cargo ron 0.12.0 value/number/Number#I64#(i64) } |
| 338 | #[cfg(feature = "integer128")] |
| 339 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::I128rust-analyzer cargo ron 0.12.0 value/number/Number#I128#(i128) } |
| 340 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::U8rust-analyzer cargo ron 0.12.0 value/number/Number#U8#(u8) } |
| 341 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::U16rust-analyzer cargo ron 0.12.0 value/number/Number#U16#(u16) } |
| 342 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::U32rust-analyzer cargo ron 0.12.0 value/number/Number#U32#(u32) } |
| 343 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::U64rust-analyzer cargo ron 0.12.0 value/number/Number#U64#(u64) } |
| 344 | #[cfg(feature = "integer128")] |
| 345 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::U128rust-analyzer cargo ron 0.12.0 value/number/Number#U128#(u128) } |
| 346 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::F32rust-analyzer cargo ron 0.12.0 value/number/Number#F32#(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32)) } |
| 347 | number_from_implrust-analyzer cargo ron 0.12.0 value/number/number_from_impl!! { Number::F64rust-analyzer cargo ron 0.12.0 value/number/Number#F64#(F64rust-analyzer cargo ron 0.12.0 value/number/F64#(f64)) } |
| 348 | |
| 349 | #[cfg(test)] |
| 350 | mod testsrust-analyzer cargo ron 0.12.0 value/number/tests/ { |
| 351 | use superrust-analyzer cargo ron 0.12.0 value/number/::*; |
| 352 | |
| 353 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 354 | fn test_nanrust-analyzer cargo ron 0.12.0 value/number/tests/test_nan().() { |
| 355 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.), F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)); |
| 356 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.), F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)); |
| 357 | assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_ne!!(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.), F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)); |
| 358 | } |
| 359 | |
| 360 | #[cfg(feature = "std")] |
| 361 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 362 | fn test_nan_hashrust-analyzer cargo ron 0.12.0 value/number/tests/test_nan_hash().() { |
| 363 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::DefaultHasherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/DefaultHasher#; |
| 364 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::{Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#}; |
| 365 | |
| 366 | fn hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().<T: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#>(v: &T) -> u64 { |
| 367 | let mut state = DefaultHasherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/DefaultHasher#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[DefaultHasher]new().(); |
| 368 | v.hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#hash().(&mut state); |
| 369 | state.finishrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[DefaultHasher][Hasher]finish().() |
| 370 | } |
| 371 | |
| 372 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().(&F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)), hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().(&F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.))); |
| 373 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().(&F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)), hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().(&F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.))); |
| 374 | assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_ne!!(hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().(&F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)), hashrust-analyzer cargo ron 0.12.0 value/number/tests/hash().(&F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.))); |
| 375 | } |
| 376 | |
| 377 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 378 | fn test_partial_ordrust-analyzer cargo ron 0.12.0 value/number/tests/test_partial_ord().() { |
| 379 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.) >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]INFINITY.)); |
| 380 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.) <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NEG_INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NEG_INFINITY.)); |
| 381 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.) == F32rust-analyzer cargo ron 0.12.0 value/number/F32#(f32::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]NAN.)); |
| 382 | } |
| 383 | } |