Add helper CowStrVisitor and CowBytesVisitor types#2922
Open
Mingun wants to merge 1 commit intoserde-rs:masterfrom
Open
Add helper CowStrVisitor and CowBytesVisitor types#2922Mingun wants to merge 1 commit intoserde-rs:masterfrom
Mingun wants to merge 1 commit intoserde-rs:masterfrom
Conversation
oli-obk
approved these changes
May 8, 2025
Member
oli-obk
left a comment
There was a problem hiding this comment.
makes sense. Please add a test for it
Contributor
Author
|
Doctests is not enough? |
Member
|
They just test compilation, not execution actually producing |
Contributor
Author
|
Ah, yes. Will be soon |
Mingun
commented
May 8, 2025
| BorrowedBytesDeserializer, BorrowedStrDeserializer, CowBytesVisitor, CowStrVisitor, Error, | ||
| MapDeserializer, | ||
| }; | ||
| use serde::de::{Deserialize, DeserializeSeed, Deserializer, IntoDeserializer}; |
Contributor
Author
There was a problem hiding this comment.
DeserializeSeed is required to be in scope to be able to call it methods on Cow...Visitors
oli-obk
reviewed
May 8, 2025
| let de_bytes = BorrowedBytesDeserializer::<Error>::new(b"borrowed"); | ||
|
|
||
| // This example shows, that without CowStrVisitor the result is different | ||
| match Cow::<str>::deserialize(de_str) { |
Member
There was a problem hiding this comment.
I'm guessing we can't use the new visitor in the Cow impl because that's generic?
Contributor
Author
There was a problem hiding this comment.
Yes, because of that.
Member
|
@dtolnay this seems like a good addition to serde. even if it could live out of tree, it's for basic libstd types |
03d1a87 to
57d6835
Compare
|
As it can help with my problem (#3027), I would like to express the support. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,CowStrVisitorandCowBytesVisitor, which both serves as aVisitorand aDeserializeSeedand allows you to read borrowed data from, for example,MapAccess: