getsentry / rust-sourcemap

A library for rust that implements basic sourcemap handling
Other
224 stars 27 forks source link

Implement `Serialize` and `Deserialize` for `DecodedMap` #94

Closed wbinnssmith closed 1 week ago

wbinnssmith commented 2 weeks ago

This uses the struct’s existing from_reader() and to_writer() methods to implement serde’s Serialize and Deserialize traits.

loewenheim commented 2 weeks 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.

wbinnssmith commented 2 weeks ago

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.

loewenheim commented 1 week ago

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.