-
-
Notifications
You must be signed in to change notification settings - Fork 854
Add helper CowStrVisitor and CowBytesVisitor types #2922
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense. Please add a test for it
Doctests is not enough? |
They just test compilation, not execution actually producing |
Ah, yes. Will be soon |
BorrowedBytesDeserializer, BorrowedStrDeserializer, CowBytesVisitor, CowStrVisitor, Error, | ||
MapDeserializer, | ||
}; | ||
use serde::de::{Deserialize, DeserializeSeed, Deserializer, IntoDeserializer}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DeserializeSeed
is required to be in scope to be able to call it methods on Cow...Visitor
s
let de_bytes = BorrowedBytesDeserializer::<Error>::new(b"borrowed"); | ||
|
||
// This example shows, that without CowStrVisitor the result is different | ||
match Cow::<str>::deserialize(de_str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing we can't use the new visitor in the Cow impl because that's generic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because of that.
@dtolnay this seems like a good addition to serde. even if it could live out of tree, it's for basic libstd types |
As all known, when you deserialize
Cow
, it will always be deserialized asOwned
, which makes it difficult to implement effective deserialization when you manually want to deserializeCow<str>
orCow<[u8]>
. This PR adds two helper types,CowStrVisitor
andCowBytesVisitor
, which both serves as aVisitor
and aDeserializeSeed
and allows you to read borrowed data from, for example,MapAccess
: