Closed wbinnssmith closed 1 week ago
Hi, thank you for the contribution! Can you tell me what your use case for this is? De/serializing a struct from bytes is pretty unusual.
We currently do this in Turbopack in a wrapper type: https://github.com/vercel/next.js/blob/f48596387987ad0b7180e06f1dc944a65f9286d7/turbopack/crates/turbopack-core/src/source_map/mod.rs#L695-L711
Bytes is part of the contract of serde's Serialize/Deserialize traits, yeah? This uses the string writer to serialize the map as a json string and use that encoding as the serialized bytes.
If this isn't useful for others, I totally understand and am happy to continue just wrapping this struct for our own use.
After thinking about it for a bit, we're not going to move forward with this. Serializing bytes to json
, for example, writes them as a list of integers, which is very inefficient. I wouldn't want to bake that into the type's De/Serialize
implementation. However, we could use the encoded form for de/serializing sourcemaps.
This uses the struct’s existing
from_reader()
andto_writer()
methods to implement serde’sSerialize
andDeserialize
traits.