Skip to content

Commit f333835

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 15152ec commit f333835

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
@@ -1639,7 +1639,19 @@ macro_rules! variant_identifier {
16391639
$(
16401640
$index => Ok($name_kind :: $variant),
16411641
)*
1642-
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),),
1642+
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)),
1643+
}
1644+
}
1645+
1646+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
1647+
where
1648+
E: Error,
1649+
{
1650+
match value {
1651+
$(
1652+
$index => Ok($name_kind :: $variant),
1653+
)*
1654+
_ => Err(Unexpected::invalid_u128(value, &self)),
16431655
}
16441656
}
16451657

@@ -2937,6 +2949,18 @@ where
29372949
}
29382950
}
29392951

2952+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
2953+
where
2954+
E: Error,
2955+
{
2956+
match value {
2957+
0 => Ok(Field::Unbounded),
2958+
1 => Ok(Field::Included),
2959+
2 => Ok(Field::Excluded),
2960+
_ => Err(Unexpected::invalid_u128(value, &self)),
2961+
}
2962+
}
2963+
29402964
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
29412965
where
29422966
E: Error,
@@ -3047,6 +3071,17 @@ where
30473071
}
30483072
}
30493073

3074+
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
3075+
where
3076+
E: Error,
3077+
{
3078+
match value {
3079+
0 => Ok(Field::Ok),
3080+
1 => Ok(Field::Err),
3081+
_ => Err(Unexpected::invalid_u128(value, &self)),
3082+
}
3083+
}
3084+
30503085
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
30513086
where
30523087
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,
@@ -647,6 +665,24 @@ mod content {
647665
.map(TagOrContent::Content)
648666
}
649667

668+
fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
669+
where
670+
F: de::Error,
671+
{
672+
ContentVisitor::new()
673+
.visit_i128(value)
674+
.map(TagOrContent::Content)
675+
}
676+
677+
fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
678+
where
679+
F: de::Error,
680+
{
681+
ContentVisitor::new()
682+
.visit_u128(value)
683+
.map(TagOrContent::Content)
684+
}
685+
650686
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
651687
where
652688
F: de::Error,
@@ -940,6 +976,17 @@ mod content {
940976
}
941977
}
942978

979+
fn visit_u128<E>(self, field_index: u128) -> Result<Self::Value, E>
980+
where
981+
E: de::Error,
982+
{
983+
match field_index {
984+
0 => Ok(TagOrContentField::Tag),
985+
1 => Ok(TagOrContentField::Content),
986+
_ => Err(Unexpected::invalid_u128(field_index, &self)),
987+
}
988+
}
989+
943990
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
944991
where
945992
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
}
@@ -1136,10 +1187,12 @@ mod content {
11361187
Content::U16(v) => visitor.visit_u16(v),
11371188
Content::U32(v) => visitor.visit_u32(v),
11381189
Content::U64(v) => visitor.visit_u64(v),
1190+
Content::U128(v) => visitor.visit_u128(v),
11391191
Content::I8(v) => visitor.visit_i8(v),
11401192
Content::I16(v) => visitor.visit_i16(v),
11411193
Content::I32(v) => visitor.visit_i32(v),
11421194
Content::I64(v) => visitor.visit_i64(v),
1195+
Content::I128(v) => visitor.visit_i128(v),
11431196
Content::F32(v) => visitor.visit_f32(v),
11441197
Content::F64(v) => visitor.visit_f64(v),
11451198
Content::Char(v) => visitor.visit_char(v),
@@ -1194,6 +1247,13 @@ mod content {
11941247
self.deserialize_integer(visitor)
11951248
}
11961249

1250+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1251+
where
1252+
V: Visitor<'de>,
1253+
{
1254+
self.deserialize_integer(visitor)
1255+
}
1256+
11971257
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
11981258
where
11991259
V: Visitor<'de>,
@@ -1222,6 +1282,13 @@ mod content {
12221282
self.deserialize_integer(visitor)
12231283
}
12241284

1285+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1286+
where
1287+
V: Visitor<'de>,
1288+
{
1289+
self.deserialize_integer(visitor)
1290+
}
1291+
12251292
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
12261293
where
12271294
V: Visitor<'de>,
@@ -1473,6 +1540,7 @@ mod content {
14731540
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
14741541
Content::U8(v) => visitor.visit_u8(v),
14751542
Content::U64(v) => visitor.visit_u64(v),
1543+
Content::U128(v) => visitor.visit_u128(v),
14761544
_ => Err(self.invalid_type(&visitor)),
14771545
}
14781546
}
@@ -1654,10 +1722,12 @@ mod content {
16541722
Content::U16(v) => visitor.visit_u16(v),
16551723
Content::U32(v) => visitor.visit_u32(v),
16561724
Content::U64(v) => visitor.visit_u64(v),
1725+
Content::U128(v) => visitor.visit_u128(v),
16571726
Content::I8(v) => visitor.visit_i8(v),
16581727
Content::I16(v) => visitor.visit_i16(v),
16591728
Content::I32(v) => visitor.visit_i32(v),
16601729
Content::I64(v) => visitor.visit_i64(v),
1730+
Content::I128(v) => visitor.visit_i128(v),
16611731
_ => Err(self.invalid_type(&visitor)),
16621732
}
16631733
}
@@ -1673,10 +1743,12 @@ mod content {
16731743
Content::U16(v) => visitor.visit_u16(v),
16741744
Content::U32(v) => visitor.visit_u32(v),
16751745
Content::U64(v) => visitor.visit_u64(v),
1746+
Content::U128(v) => visitor.visit_u128(v),
16761747
Content::I8(v) => visitor.visit_i8(v),
16771748
Content::I16(v) => visitor.visit_i16(v),
16781749
Content::I32(v) => visitor.visit_i32(v),
16791750
Content::I64(v) => visitor.visit_i64(v),
1751+
Content::I128(v) => visitor.visit_i128(v),
16801752
_ => Err(self.invalid_type(&visitor)),
16811753
}
16821754
}
@@ -1735,10 +1807,12 @@ mod content {
17351807
Content::U16(v) => visitor.visit_u16(v),
17361808
Content::U32(v) => visitor.visit_u32(v),
17371809
Content::U64(v) => visitor.visit_u64(v),
1810+
Content::U128(v) => visitor.visit_u128(v),
17381811
Content::I8(v) => visitor.visit_i8(v),
17391812
Content::I16(v) => visitor.visit_i16(v),
17401813
Content::I32(v) => visitor.visit_i32(v),
17411814
Content::I64(v) => visitor.visit_i64(v),
1815+
Content::I128(v) => visitor.visit_i128(v),
17421816
Content::F32(v) => visitor.visit_f32(v),
17431817
Content::F64(v) => visitor.visit_f64(v),
17441818
Content::Char(v) => visitor.visit_char(v),
@@ -1795,6 +1869,13 @@ mod content {
17951869
self.deserialize_integer(visitor)
17961870
}
17971871

1872+
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1873+
where
1874+
V: Visitor<'de>,
1875+
{
1876+
self.deserialize_integer(visitor)
1877+
}
1878+
17981879
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
17991880
where
18001881
V: Visitor<'de>,
@@ -1823,6 +1904,13 @@ mod content {
18231904
self.deserialize_integer(visitor)
18241905
}
18251906

1907+
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1908+
where
1909+
V: Visitor<'de>,
1910+
{
1911+
self.deserialize_integer(visitor)
1912+
}
1913+
18261914
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
18271915
where
18281916
V: Visitor<'de>,
@@ -2047,6 +2135,7 @@ mod content {
20472135
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
20482136
Content::U8(v) => visitor.visit_u8(v),
20492137
Content::U64(v) => visitor.visit_u64(v),
2138+
Content::U128(v) => visitor.visit_u128(v),
20502139
_ => Err(self.invalid_type(&visitor)),
20512140
}
20522141
}
@@ -2493,6 +2582,17 @@ where
24932582
}
24942583
}
24952584

2585+
impl<'de, E> IdentifierDeserializer<'de, E> for u128
2586+
where
2587+
E: Error,
2588+
{
2589+
type Deserializer = <u128 as IntoDeserializer<'de, E>>::Deserializer;
2590+
2591+
fn from(self) -> Self::Deserializer {
2592+
self.into_deserializer()
2593+
}
2594+
}
2595+
24962596
pub struct StrDeserializer<'a, E> {
24972597
value: &'a str,
24982598
marker: PhantomData<E>,

0 commit comments

Comments
 (0)