Skip to content

Attributes are not zero copy #352

@Dav1dde

Description

@Dav1dde
use std::borrow::Cow;
use serde::Deserialize;

#[derive(Deserialize)]
struct Root<'a> {
    #[serde(borrow)]
    rattr: Cow<'a, str>,
}

const XML: &str = r#"
<Root rattr="rattr">
</Root>
"#;

fn main() {
    let r: Root = quick_xml::de::from_str(XML).unwrap();

    assert!(matches!(r.rattr, Cow::Borrowed(_)));
}

This assert fails.

I assume this is because of this call to visit_str (which should be visit_borrowed_str):
https://github.com/tafia/quick-xml/blob/master/src/de/escape.rs#L79

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let unescaped = self.unescaped()?;
        #[cfg(not(feature = "encoding"))]
        let value = self.decoder.decode(&unescaped)?;

        #[cfg(feature = "encoding")]
        let value = self.decoder.decode(&unescaped);
        visitor.visit_str(&value) // <-- visit_borrowed_str here
    }

Unfortunately I was not able to make this work due to lifetimes not matching

Metadata

Metadata

Assignees

Labels

enhancementserdeIssues 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