Skip to content

Commit ef702a9

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 7b9302b commit ef702a9

File tree

3 files changed

+182
-2
lines changed

3 files changed

+182
-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),
@@ -406,6 +410,20 @@ mod content {
406410
Ok(Content::U64(value))
407411
}
408412

413+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
414+
where
415+
F: de::Error,
416+
{
417+
Ok(Content::I128(value))
418+
}
419+
420+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
421+
where
422+
F: de::Error,
423+
{
424+
Ok(Content::U128(value))
425+
}
426+
409427
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
410428
where
411429
F: de::Error,
@@ -660,6 +678,24 @@ mod content {
660678
.map(TagOrContent::Content)
661679
}
662680

681+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
682+
where
683+
F: de::Error,
684+
{
685+
ContentVisitor::new()
686+
.visit_i128(value)
687+
.map(TagOrContent::Content)
688+
}
689+
690+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
691+
where
692+
F: de::Error,
693+
{
694+
ContentVisitor::new()
695+
.visit_u128(value)
696+
.map(TagOrContent::Content)
697+
}
698+
663699
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
664700
where
665701
F: de::Error,
@@ -955,6 +991,17 @@ mod content {
955991
}
956992
}
957993

994+
fn visit_u128<E>(self, field_index: u128) -> Result<Self::Value, E>
995+
where
996+
E: de::Error,
997+
{
998+
match field_index {
999+
0 => Ok(TagOrContentField::Tag),
1000+
1 => Ok(TagOrContentField::Content),
1001+
_ => Err(Unexpected::invalid_u128(field_index, &self)),
1002+
}
1003+
}
1004+
9581005
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
9591006
where
9601007
E: de::Error,
@@ -1078,10 +1125,12 @@ mod content {
10781125
Content::U16(v) => visitor.visit_u16(v),
10791126
Content::U32(v) => visitor.visit_u32(v),
10801127
Content::U64(v) => visitor.visit_u64(v),
1128+
Content::U128(v) => visitor.visit_u128(v),
10811129
Content::I8(v) => visitor.visit_i8(v),
10821130
Content::I16(v) => visitor.visit_i16(v),
10831131
Content::I32(v) => visitor.visit_i32(v),
10841132
Content::I64(v) => visitor.visit_i64(v),
1133+
Content::I128(v) => visitor.visit_i128(v),
10851134
_ => Err(self.invalid_type(&visitor)),
10861135
}
10871136
}
@@ -1097,10 +1146,12 @@ mod content {
10971146
Content::U16(v) => visitor.visit_u16(v),
10981147
Content::U32(v) => visitor.visit_u32(v),
10991148
Content::U64(v) => visitor.visit_u64(v),
1149+
Content::U128(v) => visitor.visit_u128(v),
11001150
Content::I8(v) => visitor.visit_i8(v),
11011151
Content::I16(v) => visitor.visit_i16(v),
11021152
Content::I32(v) => visitor.visit_i32(v),
11031153
Content::I64(v) => visitor.visit_i64(v),
1154+
Content::I128(v) => visitor.visit_i128(v),
11041155
_ => Err(self.invalid_type(&visitor)),
11051156
}
11061157
}
@@ -1149,10 +1200,12 @@ mod content {
11491200
Content::U16(v) => visitor.visit_u16(v),
11501201
Content::U32(v) => visitor.visit_u32(v),
11511202
Content::U64(v) => visitor.visit_u64(v),
1203+
Content::U128(v) => visitor.visit_u128(v),
11521204
Content::I8(v) => visitor.visit_i8(v),
11531205
Content::I16(v) => visitor.visit_i16(v),
11541206
Content::I32(v) => visitor.visit_i32(v),
11551207
Content::I64(v) => visitor.visit_i64(v),
1208+
Content::I128(v) => visitor.visit_i128(v),
11561209
Content::F32(v) => visitor.visit_f32(v),
11571210
Content::F64(v) => visitor.visit_f64(v),
11581211
Content::Char(v) => visitor.visit_char(v),
@@ -1207,6 +1260,13 @@ mod content {
12071260
self.deserialize_integer(visitor)
12081261
}
12091262

1263+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1264+
where
1265+
V: Visitor<'de>,
1266+
{
1267+
self.deserialize_integer(visitor)
1268+
}
1269+
12101270
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
12111271
where
12121272
V: Visitor<'de>,
@@ -1235,6 +1295,13 @@ mod content {
12351295
self.deserialize_integer(visitor)
12361296
}
12371297

1298+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1299+
where
1300+
V: Visitor<'de>,
1301+
{
1302+
self.deserialize_integer(visitor)
1303+
}
1304+
12381305
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
12391306
where
12401307
V: Visitor<'de>,
@@ -1486,6 +1553,7 @@ mod content {
14861553
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
14871554
Content::U8(v) => visitor.visit_u8(v),
14881555
Content::U64(v) => visitor.visit_u64(v),
1556+
Content::U128(v) => visitor.visit_u128(v),
14891557
_ => Err(self.invalid_type(&visitor)),
14901558
}
14911559
}
@@ -1667,10 +1735,12 @@ mod content {
16671735
Content::U16(v) => visitor.visit_u16(v),
16681736
Content::U32(v) => visitor.visit_u32(v),
16691737
Content::U64(v) => visitor.visit_u64(v),
1738+
Content::U128(v) => visitor.visit_u128(v),
16701739
Content::I8(v) => visitor.visit_i8(v),
16711740
Content::I16(v) => visitor.visit_i16(v),
16721741
Content::I32(v) => visitor.visit_i32(v),
16731742
Content::I64(v) => visitor.visit_i64(v),
1743+
Content::I128(v) => visitor.visit_i128(v),
16741744
_ => Err(self.invalid_type(&visitor)),
16751745
}
16761746
}
@@ -1686,10 +1756,12 @@ mod content {
16861756
Content::U16(v) => visitor.visit_u16(v),
16871757
Content::U32(v) => visitor.visit_u32(v),
16881758
Content::U64(v) => visitor.visit_u64(v),
1759+
Content::U128(v) => visitor.visit_u128(v),
16891760
Content::I8(v) => visitor.visit_i8(v),
16901761
Content::I16(v) => visitor.visit_i16(v),
16911762
Content::I32(v) => visitor.visit_i32(v),
16921763
Content::I64(v) => visitor.visit_i64(v),
1764+
Content::I128(v) => visitor.visit_i128(v),
16931765
_ => Err(self.invalid_type(&visitor)),
16941766
}
16951767
}
@@ -1748,10 +1820,12 @@ mod content {
17481820
Content::U16(v) => visitor.visit_u16(v),
17491821
Content::U32(v) => visitor.visit_u32(v),
17501822
Content::U64(v) => visitor.visit_u64(v),
1823+
Content::U128(v) => visitor.visit_u128(v),
17511824
Content::I8(v) => visitor.visit_i8(v),
17521825
Content::I16(v) => visitor.visit_i16(v),
17531826
Content::I32(v) => visitor.visit_i32(v),
17541827
Content::I64(v) => visitor.visit_i64(v),
1828+
Content::I128(v) => visitor.visit_i128(v),
17551829
Content::F32(v) => visitor.visit_f32(v),
17561830
Content::F64(v) => visitor.visit_f64(v),
17571831
Content::Char(v) => visitor.visit_char(v),
@@ -1808,6 +1882,13 @@ mod content {
18081882
self.deserialize_integer(visitor)
18091883
}
18101884

1885+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1886+
where
1887+
V: Visitor<'de>,
1888+
{
1889+
self.deserialize_integer(visitor)
1890+
}
1891+
18111892
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
18121893
where
18131894
V: Visitor<'de>,
@@ -1836,6 +1917,13 @@ mod content {
18361917
self.deserialize_integer(visitor)
18371918
}
18381919

1920+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1921+
where
1922+
V: Visitor<'de>,
1923+
{
1924+
self.deserialize_integer(visitor)
1925+
}
1926+
18391927
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
18401928
where
18411929
V: Visitor<'de>,
@@ -2080,6 +2168,7 @@ mod content {
20802168
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
20812169
Content::U8(v) => visitor.visit_u8(v),
20822170
Content::U64(v) => visitor.visit_u64(v),
2171+
Content::U128(v) => visitor.visit_u128(v),
20832172
_ => Err(self.invalid_type(&visitor)),
20842173
}
20852174
}
@@ -2388,6 +2477,17 @@ where
23882477
}
23892478
}
23902479

2480+
impl<'de, E> IdentifierDeserializer<'de, E> for u128
2481+
where
2482+
E: Error,
2483+
{
2484+
type Deserializer = <u128 as IntoDeserializer<'de, E>>::Deserializer;
2485+
2486+
fn from(self) -> Self::Deserializer {
2487+
self.into_deserializer()
2488+
}
2489+
}
2490+
23912491
pub struct StrDeserializer<'a, E> {
23922492
value: &'a str,
23932493
marker: PhantomData<E>,

0 commit comments

Comments
 (0)