mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-22 22:58:02 +00:00
Merge pull request 1526 from fanzeyi/try_from
This commit is contained in:
@@ -269,6 +269,8 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
|
||||
deserialize_transparent(cont, params)
|
||||
} else if let Some(type_from) = cont.attrs.type_from() {
|
||||
deserialize_from(type_from)
|
||||
} else if let Some(type_try_from) = cont.attrs.type_try_from() {
|
||||
deserialize_try_from(type_try_from)
|
||||
} else if let attr::Identifier::No = cont.attrs.identifier() {
|
||||
match cont.data {
|
||||
Data::Enum(ref variants) => deserialize_enum(params, variants, &cont.attrs),
|
||||
@@ -298,6 +300,7 @@ fn deserialize_in_place_body(cont: &Container, params: &Parameters) -> Option<St
|
||||
|
||||
if cont.attrs.transparent()
|
||||
|| cont.attrs.type_from().is_some()
|
||||
|| cont.attrs.type_try_from().is_some()
|
||||
|| cont.attrs.identifier().is_some()
|
||||
|| cont
|
||||
.data
|
||||
@@ -390,6 +393,14 @@ fn deserialize_from(type_from: &syn::Type) -> Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_try_from(type_try_from: &syn::Type) -> Fragment {
|
||||
quote_block! {
|
||||
_serde::export::Result::and_then(
|
||||
<#type_try_from as _serde::Deserialize>::deserialize(__deserializer),
|
||||
|v| _serde::export::TryFrom::try_from(v).map_err(_serde::de::Error::custom))
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
let type_name = cattrs.name().deserialize_name();
|
||||
|
||||
Reference in New Issue
Block a user