Skip to content

DeserializeOwned prevents deserialization into structs with lifetime bounds #195

@sezna

Description

@sezna

First of all, thanks for this great crate. It has been serving me very well and I am a big fan.

I am trying to deserialize a struct I just serialized, just a basic round-trip. The struct, to be specific, is this one. You'll notice the lifetimes and Cow<'a, str>s in the nested structs. Serialization works fine, but deserialization does not.

implementation of `_IMPL_DESERIALIZE_FOR_EdiConvertToRequest::_serde::Deserialize` is not general enough
   --> src/main.rs:102:28
    |
102 |           DataFormat::Xml => quick_xml::de::from_str(&req.data)?,
    |                              ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `_IMPL_DESERIALIZE_FOR_EdiConvertToRequest::_serde::Deserialize` is not general enough
    | 
   ::: /home/alex/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.104/src/de/mod.rs:531:1
    |
531 | / pub trait Deserialize<'de>: Sized {
532 | |     /// Deserialize this value from the given Serde deserializer.
533 | |     ///
534 | |     /// See the [Implementing `Deserialize`][impl-deserialize] section of the
...   |
569 | |     }
570 | | }
    | |_- trait `_IMPL_DESERIALIZE_FOR_EdiConvertToRequest::_serde::Deserialize` defined here
    |
    = note: `edi::edi_document::EdiDocument<'_, '_>` must implement `_IMPL_DESERIALIZE_FOR_EdiConvertToRequest::_serde::Deserialize<'0>`, for any lifetime `'0`...
    = note: ...but `edi::edi_document::EdiDocument<'_, '_>` actually implements `_IMPL_DESERIALIZE_FOR_EdiConvertToRequest::_serde::Deserialize<'1>`, for some specific lifetime `'1`

Following the guidance on this SO post, I think changing this crate's Deserialize definition to one like serde_json's would work. Note that I am able to successfully deserialize this struct back from JSON without issue, it is just the XML that isn't working.

I report this as an issue because as far as I can tell, there is no workaround besides forking the crate that defines the struct and making everything it contains owned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementoptimizationIssues related to reducing time needed to parse XML or to memory consumptionserdeIssues related to mapping from Rust types to XML

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions