Closed owengage closed 3 years ago
This is a problem of zero-copy deserialization, it can be solved by duplicating all the struct definitions and adding a variant that owns its data. This new variant could also be used to mutate and serialize chunk data. The best way to do this is using a macro to avoid duplicating code but I don't know if that macro exists. The simple alternative is to use Cow
in all the fields, but that has a small runtime overhead.
This should be a common problem in the ecosystem, but I don't know how it has been solved by other projects. As for the user experience, see this stackoverflow answer which suggests using the rental crate:
Chunks now own their data. There wasn'tuch measurable performance benefit as it turns out.
Because a chunk is typically from compressed data, it often gets stored in a dynamically allocated slice or some sort.
Users of fastnbt then need to store this vec with the chunk somehow. If a chunk could somehow own its buffer it might be more ergonomic to use in some use cases.