Skip to content

Commit afda2f8

Browse files
committed
Support deserialization of i128/u128 in flatten structs and internally tagged enums
Fixed (6): newtype::enum_::newtype newtype::enum_::struct_ newtype::enum_::tuple newtype::newtype_struct newtype::struct_ struct_
1 parent 23b594e commit afda2f8

File tree

4 files changed

+184
-2
lines changed

4 files changed

+184
-2
lines changed

serde/src/private/de.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ mod content {
236236
Content::U16(n) => Content::U16(*n),
237237
Content::U32(n) => Content::U32(*n),
238238
Content::U64(n) => Content::U64(*n),
239+
Content::U128(n) => Content::U128(*n),
239240
Content::I8(n) => Content::I8(*n),
240241
Content::I16(n) => Content::I16(*n),
241242
Content::I32(n) => Content::I32(*n),
242243
Content::I64(n) => Content::I64(*n),
244+
Content::I128(n) => Content::I128(*n),
243245
Content::F32(f) => Content::F32(*f),
244246
Content::F64(f) => Content::F64(*f),
245247
Content::Char(c) => Content::Char(*c),
@@ -268,10 +270,12 @@ mod content {
268270
Content::U16(n) => Unexpected::Unsigned(n as u64),
269271
Content::U32(n) => Unexpected::Unsigned(n as u64),
270272
Content::U64(n) => Unexpected::Unsigned(n),
273+
Content::U128(_) => Unexpected::Other("an u128"),
271274
Content::I8(n) => Unexpected::Signed(n as i64),
272275
Content::I16(n) => Unexpected::Signed(n as i64),
273276
Content::I32(n) => Unexpected::Signed(n as i64),
274277
Content::I64(n) => Unexpected::Signed(n),
278+
Content::I128(_) => Unexpected::Other("an i128"),
275279
Content::F32(f) => Unexpected::Float(f as f64),
276280
Content::F64(f) => Unexpected::Float(f),
277281
Content::Char(c) => Unexpected::Char(c),
@@ -380,6 +384,20 @@ mod content {
380384
Ok(Content::U64(value))
381385
}
382386

387+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
388+
where
389+
F: de::Error,
390+
{
391+
Ok(Content::I128(value))
392+
}
393+
394+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
395+
where
396+
F: de::Error,
397+
{
398+
Ok(Content::U128(value))
399+
}
400+
383401
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
384402
where
385403
F: de::Error,
@@ -638,6 +656,24 @@ mod content {
638656
.map(TagOrContent::Content)
639657
}
640658

659+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
660+
where
661+
F: de::Error,
662+
{
663+
ContentVisitor::new()
664+
.visit_i128(value)
665+
.map(TagOrContent::Content)
666+
}
667+
668+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
669+
where
670+
F: de::Error,
671+
{
672+
ContentVisitor::new()
673+
.visit_u128(value)
674+
.map(TagOrContent::Content)
675+
}
676+
641677
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
642678
where
643679
F: de::Error,
@@ -936,6 +972,17 @@ mod content {
936972
}
937973
}
938974

975+
fn visit_u128<E>(self, field_index: u128) -> Result<Self::Value, E>
976+
where
977+
E: de::Error,
978+
{
979+
match field_index {
980+
0 => Ok(TagOrContentField::Tag),
981+
1 => Ok(TagOrContentField::Content),
982+
_ => Err(Unexpected::invalid_u128(field_index, &self)),
983+
}
984+
}
985+
939986
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
940987
where
941988
E: de::Error,
@@ -1061,10 +1108,12 @@ mod content {
10611108
Content::U16(v) => visitor.visit_u16(v),
10621109
Content::U32(v) => visitor.visit_u32(v),
10631110
Content::U64(v) => visitor.visit_u64(v),
1111+
Content::U128(v) => visitor.visit_u128(v),
10641112
Content::I8(v) => visitor.visit_i8(v),
10651113
Content::I16(v) => visitor.visit_i16(v),
10661114
Content::I32(v) => visitor.visit_i32(v),
10671115
Content::I64(v) => visitor.visit_i64(v),
1116+
Content::I128(v) => visitor.visit_i128(v),
10681117
_ => Err(self.invalid_type(&visitor)),
10691118
}
10701119
}
@@ -1080,10 +1129,12 @@ mod content {
10801129
Content::U16(v) => visitor.visit_u16(v),
10811130
Content::U32(v) => visitor.visit_u32(v),
10821131
Content::U64(v) => visitor.visit_u64(v),
1132+
Content::U128(v) => visitor.visit_u128(v),
10831133
Content::I8(v) => visitor.visit_i8(v),
10841134
Content::I16(v) => visitor.visit_i16(v),
10851135
Content::I32(v) => visitor.visit_i32(v),
10861136
Content::I64(v) => visitor.visit_i64(v),
1137+
Content::I128(v) => visitor.visit_i128(v),
10871138
_ => Err(self.invalid_type(&visitor)),
10881139
}
10891140
}
@@ -1133,10 +1184,12 @@ mod content {
11331184
Content::U16(v) => visitor.visit_u16(v),
11341185
Content::U32(v) => visitor.visit_u32(v),
11351186
Content::U64(v) => visitor.visit_u64(v),
1187+
Content::U128(v) => visitor.visit_u128(v),
11361188
Content::I8(v) => visitor.visit_i8(v),
11371189
Content::I16(v) => visitor.visit_i16(v),
11381190
Content::I32(v) => visitor.visit_i32(v),
11391191
Content::I64(v) => visitor.visit_i64(v),
1192+
Content::I128(v) => visitor.visit_i128(v),
11401193
Content::F32(v) => visitor.visit_f32(v),
11411194
Content::F64(v) => visitor.visit_f64(v),
11421195
Content::Char(v) => visitor.visit_char(v),
@@ -1191,6 +1244,13 @@ mod content {
11911244
self.deserialize_integer(visitor)
11921245
}
11931246

1247+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1248+
where
1249+
V: Visitor<'de>,
1250+
{
1251+
self.deserialize_integer(visitor)
1252+
}
1253+
11941254
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
11951255
where
11961256
V: Visitor<'de>,
@@ -1219,6 +1279,13 @@ mod content {
12191279
self.deserialize_integer(visitor)
12201280
}
12211281

1282+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1283+
where
1284+
V: Visitor<'de>,
1285+
{
1286+
self.deserialize_integer(visitor)
1287+
}
1288+
12221289
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
12231290
where
12241291
V: Visitor<'de>,
@@ -1470,6 +1537,7 @@ mod content {
14701537
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
14711538
Content::U8(v) => visitor.visit_u8(v),
14721539
Content::U64(v) => visitor.visit_u64(v),
1540+
Content::U128(v) => visitor.visit_u128(v),
14731541
_ => Err(self.invalid_type(&visitor)),
14741542
}
14751543
}
@@ -2012,10 +2080,12 @@ mod content {
20122080
Content::U16(v) => visitor.visit_u16(v),
20132081
Content::U32(v) => visitor.visit_u32(v),
20142082
Content::U64(v) => visitor.visit_u64(v),
2083+
Content::U128(v) => visitor.visit_u128(v),
20152084
Content::I8(v) => visitor.visit_i8(v),
20162085
Content::I16(v) => visitor.visit_i16(v),
20172086
Content::I32(v) => visitor.visit_i32(v),
20182087
Content::I64(v) => visitor.visit_i64(v),
2088+
Content::I128(v) => visitor.visit_i128(v),
20192089
_ => Err(self.invalid_type(&visitor)),
20202090
}
20212091
}
@@ -2031,10 +2101,12 @@ mod content {
20312101
Content::U16(v) => visitor.visit_u16(v),
20322102
Content::U32(v) => visitor.visit_u32(v),
20332103
Content::U64(v) => visitor.visit_u64(v),
2104+
Content::U128(v) => visitor.visit_u128(v),
20342105
Content::I8(v) => visitor.visit_i8(v),
20352106
Content::I16(v) => visitor.visit_i16(v),
20362107
Content::I32(v) => visitor.visit_i32(v),
20372108
Content::I64(v) => visitor.visit_i64(v),
2109+
Content::I128(v) => visitor.visit_i128(v),
20382110
_ => Err(self.invalid_type(&visitor)),
20392111
}
20402112
}
@@ -2087,10 +2159,12 @@ mod content {
20872159
Content::U16(v) => visitor.visit_u16(v),
20882160
Content::U32(v) => visitor.visit_u32(v),
20892161
Content::U64(v) => visitor.visit_u64(v),
2162+
Content::U128(v) => visitor.visit_u128(v),
20902163
Content::I8(v) => visitor.visit_i8(v),
20912164
Content::I16(v) => visitor.visit_i16(v),
20922165
Content::I32(v) => visitor.visit_i32(v),
20932166
Content::I64(v) => visitor.visit_i64(v),
2167+
Content::I128(v) => visitor.visit_i128(v),
20942168
Content::F32(v) => visitor.visit_f32(v),
20952169
Content::F64(v) => visitor.visit_f64(v),
20962170
Content::Char(v) => visitor.visit_char(v),
@@ -2147,6 +2221,13 @@ mod content {
21472221
self.deserialize_integer(visitor)
21482222
}
21492223

2224+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2225+
where
2226+
V: Visitor<'de>,
2227+
{
2228+
self.deserialize_integer(visitor)
2229+
}
2230+
21502231
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
21512232
where
21522233
V: Visitor<'de>,
@@ -2175,6 +2256,13 @@ mod content {
21752256
self.deserialize_integer(visitor)
21762257
}
21772258

2259+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2260+
where
2261+
V: Visitor<'de>,
2262+
{
2263+
self.deserialize_integer(visitor)
2264+
}
2265+
21782266
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
21792267
where
21802268
V: Visitor<'de>,
@@ -2419,6 +2507,7 @@ mod content {
24192507
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
24202508
Content::U8(v) => visitor.visit_u8(v),
24212509
Content::U64(v) => visitor.visit_u64(v),
2510+
Content::U128(v) => visitor.visit_u128(v),
24222511
_ => Err(self.invalid_type(&visitor)),
24232512
}
24242513
}
@@ -3074,6 +3163,17 @@ where
30743163
}
30753164
}
30763165

3166+
impl<'de, E> IdentifierDeserializer<'de, E> for u128
3167+
where
3168+
E: Error,
3169+
{
3170+
type Deserializer = <u128 as IntoDeserializer<'de, E>>::Deserializer;
3171+
3172+
fn from(self) -> Self::Deserializer {
3173+
self.into_deserializer()
3174+
}
3175+
}
3176+
30773177
pub struct StrDeserializer<'a, E> {
30783178
value: &'a str,
30793179
marker: PhantomData<E>,

serde_core/src/de/impls.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,19 @@ macro_rules! variant_identifier {
16251625
$(
16261626
$index => Ok($name_kind :: $variant),
16271627
)*
1628-
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),),
1628+
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)),
1629+
}
1630+
}
1631+
1632+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
1633+
where
1634+
E: Error,
1635+
{
1636+
match value {
1637+
$(
1638+
$index => Ok($name_kind :: $variant),
1639+
)*
1640+
_ => Err(Unexpected::invalid_u128(value, &self)),
16291641
}
16301642
}
16311643

@@ -2911,6 +2923,18 @@ where
29112923
}
29122924
}
29132925

2926+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
2927+
where
2928+
E: Error,
2929+
{
2930+
match value {
2931+
0 => Ok(Field::Unbounded),
2932+
1 => Ok(Field::Included),
2933+
2 => Ok(Field::Excluded),
2934+
_ => Err(Unexpected::invalid_u128(value, &self)),
2935+
}
2936+
}
2937+
29142938
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
29152939
where
29162940
E: Error,
@@ -3023,6 +3047,17 @@ where
30233047
}
30243048
}
30253049

3050+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
3051+
where
3052+
E: Error,
3053+
{
3054+
match value {
3055+
0 => Ok(Field::Ok),
3056+
1 => Ok(Field::Err),
3057+
_ => Err(Unexpected::invalid_u128(value, &self)),
3058+
}
3059+
}
3060+
30263061
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
30273062
where
30283063
E: Error,

serde_core/src/private/content.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ pub enum Content<'de> {
1414
U16(u16),
1515
U32(u32),
1616
U64(u64),
17+
U128(u128),
1718

1819
I8(i8),
1920
I16(i16),
2021
I32(i32),
2122
I64(i64),
23+
I128(i128),
2224

2325
F32(f32),
2426
F64(f64),

0 commit comments

Comments
 (0)