How to handle Cow-like strings for partial zero-copy deserialization from JSON? #5500
Replies: 2 comments
-
|
I figured out an alternate representation that avoids the problems I described above. |
Beta Was this translation helpful? Give feedback.
-
Yeah, this would absolutely not be safe or valid ULE. But you don't need to. If you want to stick a vector inside a vector, use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to understand how to use
zerovecfor (partial) zero-copy deserialization of a data format that is specified as JSON.Full zero-copy deserialization from the JSON is of course not possible for the usual reason of potentially needing to unescape certain strings when parsing as UTF-8.
The way I currently handle this is by deserializing all UTF-8 strings to
Cow<str>, where everything is borrowed unless unescaping occurred.As far as I understand, using zerovec here is still desirable because it would allow for more efficient deserialization and serialization from machine-friendly binary formats (which could be useful for caching data originally from the JSON, etc).
The main problem I'm running into while trying to use
zerovecis that I don't see a straightforward way to represent theseCow-like strings withinZeroVecorVarZeroVec.There is an instance
Cow<str>: EncodeAsVarULE<str>which is used withVarZeroVecOwned::pushbut that doesn't seem very useful here because it would force me to make entire vectors owned just for a single owned string.It almost seems like what I would want here is to have a
ULEtype which just encodes the string pointer representation as bytes, and useZeroVecinstead ofVarZeroVec, butULEhas aCopybound, which prevents me from making a type (like, e.g.,compact_str::CompactString) that can implDropfor the owned case to clean up theStringremnants appropriately.Maybe I'm thinking about this the wrong way or maybe
zerovecjust isn't appropriate here?I suppose I could roll my own
Cow-like vector types and just usezerofrom(andyoke) but I'd still miss out on more efficient deserialization.Any advice on what to do for this scenario?
Beta Was this translation helpful? Give feedback.
All reactions