Correctly deserialize newtype struct (#168)

This commit is contained in:
Alex Orlenko 2022-05-24 23:26:17 +01:00
parent 0076aa735a
commit 30ba616a8a
No known key found for this signature in database
GPG key ID: 4C150C250863B96D
2 changed files with 22 additions and 2 deletions

View file

@ -320,10 +320,17 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
self.deserialize_map(visitor)
}
#[inline]
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes
byte_buf unit unit_struct newtype_struct
identifier ignored_any
byte_buf unit unit_struct identifier ignored_any
}
}

View file

@ -375,6 +375,19 @@ fn test_from_value_struct() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[test]
fn test_from_value_newtype_struct() -> Result<(), Box<dyn std::error::Error>> {
let lua = Lua::new();
#[derive(Deserialize, PartialEq, Debug)]
struct Test(f64);
let got = lua.from_value(Value::Number(123.456))?;
assert_eq!(Test(123.456), got);
Ok(())
}
#[test]
fn test_from_value_enum() -> Result<(), Box<dyn std::error::Error>> {
let lua = Lua::new();