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.
-
I'm trying to understand how to use
zerovec
for (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
zerovec
is that I don't see a straightforward way to represent theseCow
-like strings withinZeroVec
orVarZeroVec
.There is an instance
Cow<str>: EncodeAsVarULE<str>
which is used withVarZeroVecOwned::push
but 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
ULE
type which just encodes the string pointer representation as bytes, and useZeroVec
instead ofVarZeroVec
, butULE
has aCopy
bound, which prevents me from making a type (like, e.g.,compact_str::CompactString
) that can implDrop
for the owned case to clean up theString
remnants appropriately.Maybe I'm thinking about this the wrong way or maybe
zerovec
just 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