Skip to content

Commit 84c80e2

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 1137c5a commit 84c80e2

File tree

3 files changed

+177
-2
lines changed

3 files changed

+177
-2
lines changed

serde/src/de/impls.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,19 @@ macro_rules! variant_identifier {
16361636
$(
16371637
$index => Ok($name_kind :: $variant),
16381638
)*
1639-
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),),
1639+
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)),
1640+
}
1641+
}
1642+
1643+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
1644+
where
1645+
E: Error,
1646+
{
1647+
match value {
1648+
$(
1649+
$index => Ok($name_kind :: $variant),
1650+
)*
1651+
_ => Err(Unexpected::invalid_u128(value, &self)),
16401652
}
16411653
}
16421654

@@ -2923,6 +2935,18 @@ where
29232935
}
29242936
}
29252937

2938+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
2939+
where
2940+
E: Error,
2941+
{
2942+
match value {
2943+
0 => Ok(Field::Unbounded),
2944+
1 => Ok(Field::Included),
2945+
2 => Ok(Field::Excluded),
2946+
_ => Err(Unexpected::invalid_u128(value, &self)),
2947+
}
2948+
}
2949+
29262950
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
29272951
where
29282952
E: Error,
@@ -3033,6 +3057,17 @@ where
30333057
}
30343058
}
30353059

3060+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
3061+
where
3062+
E: Error,
3063+
{
3064+
match value {
3065+
0 => Ok(Field::Ok),
3066+
1 => Ok(Field::Err),
3067+
_ => Err(Unexpected::invalid_u128(value, &self)),
3068+
}
3069+
}
3070+
30363071
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
30373072
where
30383073
E: Error,

serde/src/private/de.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,13 @@ mod content {
227227
U16(u16),
228228
U32(u32),
229229
U64(u64),
230+
U128(u128),
230231

231232
I8(i8),
232233
I16(i16),
233234
I32(i32),
234235
I64(i64),
236+
I128(i128),
235237

236238
F32(f32),
237239
F64(f64),
@@ -270,10 +272,12 @@ mod content {
270272
Content::U16(n) => Unexpected::Unsigned(n as u64),
271273
Content::U32(n) => Unexpected::Unsigned(n as u64),
272274
Content::U64(n) => Unexpected::Unsigned(n),
275+
Content::U128(_) => Unexpected::Other("an u128"),
273276
Content::I8(n) => Unexpected::Signed(n as i64),
274277
Content::I16(n) => Unexpected::Signed(n as i64),
275278
Content::I32(n) => Unexpected::Signed(n as i64),
276279
Content::I64(n) => Unexpected::Signed(n),
280+
Content::I128(_) => Unexpected::Other("an i128"),
277281
Content::F32(f) => Unexpected::Float(f as f64),
278282
Content::F64(f) => Unexpected::Float(f),
279283
Content::Char(c) => Unexpected::Char(c),
@@ -395,6 +399,20 @@ mod content {
395399
Ok(Content::U64(value))
396400
}
397401

402+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
403+
where
404+
F: de::Error,
405+
{
406+
Ok(Content::I128(value))
407+
}
408+
409+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
410+
where
411+
F: de::Error,
412+
{
413+
Ok(Content::U128(value))
414+
}
415+
398416
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
399417
where
400418
F: de::Error,
@@ -649,6 +667,24 @@ mod content {
649667
.map(TagOrContent::Content)
650668
}
651669

670+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
671+
where
672+
F: de::Error,
673+
{
674+
ContentVisitor::new()
675+
.visit_i128(value)
676+
.map(TagOrContent::Content)
677+
}
678+
679+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
680+
where
681+
F: de::Error,
682+
{
683+
ContentVisitor::new()
684+
.visit_u128(value)
685+
.map(TagOrContent::Content)
686+
}
687+
652688
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
653689
where
654690
F: de::Error,
@@ -944,6 +980,17 @@ mod content {
944980
}
945981
}
946982

983+
fn visit_u128<E>(self, field_index: u128) -> Result<Self::Value, E>
984+
where
985+
E: de::Error,
986+
{
987+
match field_index {
988+
0 => Ok(TagOrContentField::Tag),
989+
1 => Ok(TagOrContentField::Content),
990+
_ => Err(Unexpected::invalid_u128(field_index, &self)),
991+
}
992+
}
993+
947994
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
948995
where
949996
E: de::Error,
@@ -1067,10 +1114,12 @@ mod content {
10671114
Content::U16(v) => visitor.visit_u16(v),
10681115
Content::U32(v) => visitor.visit_u32(v),
10691116
Content::U64(v) => visitor.visit_u64(v),
1117+
Content::U128(v) => visitor.visit_u128(v),
10701118
Content::I8(v) => visitor.visit_i8(v),
10711119
Content::I16(v) => visitor.visit_i16(v),
10721120
Content::I32(v) => visitor.visit_i32(v),
10731121
Content::I64(v) => visitor.visit_i64(v),
1122+
Content::I128(v) => visitor.visit_i128(v),
10741123
_ => Err(self.invalid_type(&visitor)),
10751124
}
10761125
}
@@ -1086,10 +1135,12 @@ mod content {
10861135
Content::U16(v) => visitor.visit_u16(v),
10871136
Content::U32(v) => visitor.visit_u32(v),
10881137
Content::U64(v) => visitor.visit_u64(v),
1138+
Content::U128(v) => visitor.visit_u128(v),
10891139
Content::I8(v) => visitor.visit_i8(v),
10901140
Content::I16(v) => visitor.visit_i16(v),
10911141
Content::I32(v) => visitor.visit_i32(v),
10921142
Content::I64(v) => visitor.visit_i64(v),
1143+
Content::I128(v) => visitor.visit_i128(v),
10931144
_ => Err(self.invalid_type(&visitor)),
10941145
}
10951146
}
@@ -1146,10 +1197,12 @@ mod content {
11461197
Content::U16(v) => visitor.visit_u16(v),
11471198
Content::U32(v) => visitor.visit_u32(v),
11481199
Content::U64(v) => visitor.visit_u64(v),
1200+
Content::U128(v) => visitor.visit_u128(v),
11491201
Content::I8(v) => visitor.visit_i8(v),
11501202
Content::I16(v) => visitor.visit_i16(v),
11511203
Content::I32(v) => visitor.visit_i32(v),
11521204
Content::I64(v) => visitor.visit_i64(v),
1205+
Content::I128(v) => visitor.visit_i128(v),
11531206
Content::F32(v) => visitor.visit_f32(v),
11541207
Content::F64(v) => visitor.visit_f64(v),
11551208
Content::Char(v) => visitor.visit_char(v),
@@ -1204,6 +1257,13 @@ mod content {
12041257
self.deserialize_integer(visitor)
12051258
}
12061259

1260+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1261+
where
1262+
V: Visitor<'de>,
1263+
{
1264+
self.deserialize_integer(visitor)
1265+
}
1266+
12071267
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
12081268
where
12091269
V: Visitor<'de>,
@@ -1232,6 +1292,13 @@ mod content {
12321292
self.deserialize_integer(visitor)
12331293
}
12341294

1295+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1296+
where
1297+
V: Visitor<'de>,
1298+
{
1299+
self.deserialize_integer(visitor)
1300+
}
1301+
12351302
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
12361303
where
12371304
V: Visitor<'de>,
@@ -1483,6 +1550,7 @@ mod content {
14831550
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
14841551
Content::U8(v) => visitor.visit_u8(v),
14851552
Content::U64(v) => visitor.visit_u64(v),
1553+
Content::U128(v) => visitor.visit_u128(v),
14861554
_ => Err(self.invalid_type(&visitor)),
14871555
}
14881556
}
@@ -1664,10 +1732,12 @@ mod content {
16641732
Content::U16(v) => visitor.visit_u16(v),
16651733
Content::U32(v) => visitor.visit_u32(v),
16661734
Content::U64(v) => visitor.visit_u64(v),
1735+
Content::U128(v) => visitor.visit_u128(v),
16671736
Content::I8(v) => visitor.visit_i8(v),
16681737
Content::I16(v) => visitor.visit_i16(v),
16691738
Content::I32(v) => visitor.visit_i32(v),
16701739
Content::I64(v) => visitor.visit_i64(v),
1740+
Content::I128(v) => visitor.visit_i128(v),
16711741
_ => Err(self.invalid_type(&visitor)),
16721742
}
16731743
}
@@ -1683,10 +1753,12 @@ mod content {
16831753
Content::U16(v) => visitor.visit_u16(v),
16841754
Content::U32(v) => visitor.visit_u32(v),
16851755
Content::U64(v) => visitor.visit_u64(v),
1756+
Content::U128(v) => visitor.visit_u128(v),
16861757
Content::I8(v) => visitor.visit_i8(v),
16871758
Content::I16(v) => visitor.visit_i16(v),
16881759
Content::I32(v) => visitor.visit_i32(v),
16891760
Content::I64(v) => visitor.visit_i64(v),
1761+
Content::I128(v) => visitor.visit_i128(v),
16901762
_ => Err(self.invalid_type(&visitor)),
16911763
}
16921764
}
@@ -1752,10 +1824,12 @@ mod content {
17521824
Content::U16(v) => visitor.visit_u16(v),
17531825
Content::U32(v) => visitor.visit_u32(v),
17541826
Content::U64(v) => visitor.visit_u64(v),
1827+
Content::U128(v) => visitor.visit_u128(v),
17551828
Content::I8(v) => visitor.visit_i8(v),
17561829
Content::I16(v) => visitor.visit_i16(v),
17571830
Content::I32(v) => visitor.visit_i32(v),
17581831
Content::I64(v) => visitor.visit_i64(v),
1832+
Content::I128(v) => visitor.visit_i128(v),
17591833
Content::F32(v) => visitor.visit_f32(v),
17601834
Content::F64(v) => visitor.visit_f64(v),
17611835
Content::Char(v) => visitor.visit_char(v),
@@ -1812,6 +1886,13 @@ mod content {
18121886
self.deserialize_integer(visitor)
18131887
}
18141888

1889+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1890+
where
1891+
V: Visitor<'de>,
1892+
{
1893+
self.deserialize_integer(visitor)
1894+
}
1895+
18151896
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
18161897
where
18171898
V: Visitor<'de>,
@@ -1840,6 +1921,13 @@ mod content {
18401921
self.deserialize_integer(visitor)
18411922
}
18421923

1924+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1925+
where
1926+
V: Visitor<'de>,
1927+
{
1928+
self.deserialize_integer(visitor)
1929+
}
1930+
18431931
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
18441932
where
18451933
V: Visitor<'de>,
@@ -2084,6 +2172,7 @@ mod content {
20842172
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
20852173
Content::U8(v) => visitor.visit_u8(v),
20862174
Content::U64(v) => visitor.visit_u64(v),
2175+
Content::U128(v) => visitor.visit_u128(v),
20872176
_ => Err(self.invalid_type(&visitor)),
20882177
}
20892178
}
@@ -2392,6 +2481,17 @@ where
23922481
}
23932482
}
23942483

2484+
impl<'de, E> IdentifierDeserializer<'de, E> for u128
2485+
where
2486+
E: Error,
2487+
{
2488+
type Deserializer = <u128 as IntoDeserializer<'de, E>>::Deserializer;
2489+
2490+
fn from(self) -> Self::Deserializer {
2491+
self.into_deserializer()
2492+
}
2493+
}
2494+
23952495
pub struct StrDeserializer<'a, E> {
23962496
value: &'a str,
23972497
marker: PhantomData<E>,

0 commit comments

Comments
 (0)