serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

Deserializer does not support borrowed strings

Open lilyball opened this issue 7 years ago • 2 comments

It appears that serde-xml-rs cannot be used to deserialize into borrowed strings. This is rather annoying.

Example:

#[macro_use]
extern crate serde_derive; // 1.0

extern crate serde; // 1.0
extern crate serde_xml_rs; // 0.2

#[derive(Debug, Deserialize)]
struct Foo<'a> {
    name: &'a str
}

fn main() {
    let foo: Result<Foo,_> = serde_xml_rs::deserialize(&b"<foo><name>test</name></foo>"[..]);
    println!("{:?}", foo);
}

This prints

Err(invalid type: string "test", expected a borrowed string)

lilyball avatar Apr 17 '18 21:04 lilyball

Just like with JSON, this is not possible for general case, unless, like with serde-json, you don't mind runtime error whenever your string contains escaped characters. It would be possible to implement it that way, but might be an even bigger footgun than allowing only owned strings.

RReverser avatar Apr 18 '18 10:04 RReverser

I don't mind a runtime error when my string contains escaped characters. I'm opting in to that restriction when I choose to use borrowed data.

In my case, I'm parsing a multi-megabyte file of XML that I'm pretty confident doesn't contain any entities (at least, not in the text nodes I care about), and being able to use borrowed data would allow me to eliminate most of the string copying from my program.

lilyball avatar Apr 18 '18 15:04 lilyball