Skip to content

Commit

Permalink
Support deserialization of i128/u128 in flatten structs and internall…
Browse files Browse the repository at this point in the history
…y tagged enums

Fixed (6):
    newtype::enum_::newtype
    newtype::enum_::struct_
    newtype::enum_::tuple
    newtype::newtype_struct
    newtype::struct_
    struct_
  • Loading branch information
Mingun committed Oct 21, 2024
1 parent 2af38d3 commit 8126c2c
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
37 changes: 36 additions & 1 deletion serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,19 @@ macro_rules! variant_identifier {
$(
$index => Ok($name_kind :: $variant),
)*
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),),
_ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)),
}
}

fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
where
E: Error,
{
match value {
$(
$index => Ok($name_kind :: $variant),
)*
_ => Err(Unexpected::invalid_u128(value, &self)),
}
}

Expand Down Expand Up @@ -2923,6 +2935,18 @@ where
}
}

fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
where
E: Error,
{
match value {
0 => Ok(Field::Unbounded),
1 => Ok(Field::Included),
2 => Ok(Field::Excluded),
_ => Err(Unexpected::invalid_u128(value, &self)),
}
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
Expand Down Expand Up @@ -3033,6 +3057,17 @@ where
}
}

fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
where
E: Error,
{
match value {
0 => Ok(Field::Ok),
1 => Ok(Field::Err),
_ => Err(Unexpected::invalid_u128(value, &self)),
}
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
Expand Down
100 changes: 100 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@ mod content {
U16(u16),
U32(u32),
U64(u64),
U128(u128),

I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),

F32(f32),
F64(f64),
Expand Down Expand Up @@ -270,10 +272,12 @@ mod content {
Content::U16(n) => Unexpected::Unsigned(n as u64),
Content::U32(n) => Unexpected::Unsigned(n as u64),
Content::U64(n) => Unexpected::Unsigned(n),
Content::U128(_) => Unexpected::Other("an u128"),
Content::I8(n) => Unexpected::Signed(n as i64),
Content::I16(n) => Unexpected::Signed(n as i64),
Content::I32(n) => Unexpected::Signed(n as i64),
Content::I64(n) => Unexpected::Signed(n),
Content::I128(_) => Unexpected::Other("an i128"),
Content::F32(f) => Unexpected::Float(f as f64),
Content::F64(f) => Unexpected::Float(f),
Content::Char(c) => Unexpected::Char(c),
Expand Down Expand Up @@ -406,6 +410,20 @@ mod content {
Ok(Content::U64(value))
}

fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
where
F: de::Error,
{
Ok(Content::I128(value))
}

fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
where
F: de::Error,
{
Ok(Content::U128(value))
}

fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
where
F: de::Error,
Expand Down Expand Up @@ -660,6 +678,24 @@ mod content {
.map(TagOrContent::Content)
}

fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
where
F: de::Error,
{
ContentVisitor::new()
.visit_i128(value)
.map(TagOrContent::Content)
}

fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
where
F: de::Error,
{
ContentVisitor::new()
.visit_u128(value)
.map(TagOrContent::Content)
}

fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
where
F: de::Error,
Expand Down Expand Up @@ -955,6 +991,17 @@ mod content {
}
}

fn visit_u128<E>(self, field_index: u128) -> Result<Self::Value, E>
where
E: de::Error,
{
match field_index {
0 => Ok(TagOrContentField::Tag),
1 => Ok(TagOrContentField::Content),
_ => Err(Unexpected::invalid_u128(field_index, &self)),
}
}

fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
where
E: de::Error,
Expand Down Expand Up @@ -1078,10 +1125,12 @@ mod content {
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
Content::I8(v) => visitor.visit_i8(v),
Content::I16(v) => visitor.visit_i16(v),
Content::I32(v) => visitor.visit_i32(v),
Content::I64(v) => visitor.visit_i64(v),
Content::I128(v) => visitor.visit_i128(v),
_ => Err(self.invalid_type(&visitor)),
}
}
Expand All @@ -1097,10 +1146,12 @@ mod content {
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
Content::I8(v) => visitor.visit_i8(v),
Content::I16(v) => visitor.visit_i16(v),
Content::I32(v) => visitor.visit_i32(v),
Content::I64(v) => visitor.visit_i64(v),
Content::I128(v) => visitor.visit_i128(v),
_ => Err(self.invalid_type(&visitor)),
}
}
Expand Down Expand Up @@ -1149,10 +1200,12 @@ mod content {
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
Content::I8(v) => visitor.visit_i8(v),
Content::I16(v) => visitor.visit_i16(v),
Content::I32(v) => visitor.visit_i32(v),
Content::I64(v) => visitor.visit_i64(v),
Content::I128(v) => visitor.visit_i128(v),
Content::F32(v) => visitor.visit_f32(v),
Content::F64(v) => visitor.visit_f64(v),
Content::Char(v) => visitor.visit_char(v),
Expand Down Expand Up @@ -1207,6 +1260,13 @@ mod content {
self.deserialize_integer(visitor)
}

fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
self.deserialize_integer(visitor)
}

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -1235,6 +1295,13 @@ mod content {
self.deserialize_integer(visitor)
}

fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
self.deserialize_integer(visitor)
}

fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -1486,6 +1553,7 @@ mod content {
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
Content::U8(v) => visitor.visit_u8(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
_ => Err(self.invalid_type(&visitor)),
}
}
Expand Down Expand Up @@ -1667,10 +1735,12 @@ mod content {
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
Content::I8(v) => visitor.visit_i8(v),
Content::I16(v) => visitor.visit_i16(v),
Content::I32(v) => visitor.visit_i32(v),
Content::I64(v) => visitor.visit_i64(v),
Content::I128(v) => visitor.visit_i128(v),
_ => Err(self.invalid_type(&visitor)),
}
}
Expand All @@ -1686,10 +1756,12 @@ mod content {
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
Content::I8(v) => visitor.visit_i8(v),
Content::I16(v) => visitor.visit_i16(v),
Content::I32(v) => visitor.visit_i32(v),
Content::I64(v) => visitor.visit_i64(v),
Content::I128(v) => visitor.visit_i128(v),
_ => Err(self.invalid_type(&visitor)),
}
}
Expand Down Expand Up @@ -1748,10 +1820,12 @@ mod content {
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
Content::I8(v) => visitor.visit_i8(v),
Content::I16(v) => visitor.visit_i16(v),
Content::I32(v) => visitor.visit_i32(v),
Content::I64(v) => visitor.visit_i64(v),
Content::I128(v) => visitor.visit_i128(v),
Content::F32(v) => visitor.visit_f32(v),
Content::F64(v) => visitor.visit_f64(v),
Content::Char(v) => visitor.visit_char(v),
Expand Down Expand Up @@ -1808,6 +1882,13 @@ mod content {
self.deserialize_integer(visitor)
}

fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
self.deserialize_integer(visitor)
}

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -1836,6 +1917,13 @@ mod content {
self.deserialize_integer(visitor)
}

fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
self.deserialize_integer(visitor)
}

fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -2080,6 +2168,7 @@ mod content {
Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
Content::U8(v) => visitor.visit_u8(v),
Content::U64(v) => visitor.visit_u64(v),
Content::U128(v) => visitor.visit_u128(v),
_ => Err(self.invalid_type(&visitor)),
}
}
Expand Down Expand Up @@ -2388,6 +2477,17 @@ where
}
}

impl<'de, E> IdentifierDeserializer<'de, E> for u128
where
E: Error,
{
type Deserializer = <u128 as IntoDeserializer<'de, E>>::Deserializer;

fn from(self) -> Self::Deserializer {
self.into_deserializer()
}
}

pub struct StrDeserializer<'a, E> {
value: &'a str,
marker: PhantomData<E>,
Expand Down
Loading

0 comments on commit 8126c2c

Please sign in to comment.