-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is Decoder::decode
returning a Cow
?
#512
Comments
That is how it was implemented before and we specially change it in #180 because API that changes depending on the feature flags is hard to use -- features are additive and different dependencies can enable different set of features.
If you get an owned
That is how it worked internally, decoding is done only when you get content of events, which require you to manually call corresponding function. So if you don't need content you'll not pay for decoding (but this is dangerous because you can pass invalid documents). I cannot imagine a situation where you'll need raw bytes of XML stream.
Parsing of XML should be done after decoding. XML is defined in terms of characters, not bytes. Right now we do the opposite and this is the case why UTF-16 and ISO-2022-JP are not supported. For the other supported encodings the are no difference in practice, because them are ASCII-compatible and all XML markup characters are ASCII. |
I was responding when I realized
Sorry, I should've been a little more clear on this. I had an owned |
According to:
quick-xml/src/encoding.rs
Lines 81 to 90 in 92de42e
Decoder::decode
returns an ownedCow
when featureencoding
is enabled, otherwise a borrowed one. How come there aren't two definitions fordecode
, one defined for featureencoding
that returns an ownedString
and one defined without the feature that returns a borrowedstr
?I ran across this issue trying to return a borrowed string (or bytes) from a function, but unfortunately the
Cow
is owned, so I couldn't. My only solution would be to return the ownedCow
, then further pattern match on that to get the borrowed value out. If I wanted to convert theCow
to bytes, it would be even more of a hassle.On a side note, would it be useful to have a function to return the raw bytes rather than checking its valid
UTF-8
? I took a quick peek around the function and it seemsquick-xml
has an additional abstraction that pipes encodings toencoding_rs
. Maybe the user should handle it themselves for more flexibility? Or perhapsquick-xml
should be split into a lower-level crate that handles the bare bones of XML parsing, then have an abstraction on top for this kind of flexibility?The text was updated successfully, but these errors were encountered: