Borrow field identifiers from deserializer, instead of input #185
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #167.
Bitvec's handwritten
Deserialize
impls tried to borrow a bunch of strings directly from the deserializer's input, including the field names "order", "head", "bits", "data", "width", "index" and the value of the "order" field for comparing againsttype_name::<O>()
.This sometimes works, for example when deserializing from a JSON &str. However, this is not how derived
Deserialize
impls work and a lot of formats won't be able to hand out borrowed data from the input like that. For example, when deserializing any data format from aR: std::io::Read
. (You can tell because https://doc.rust-lang.org/std/io/trait.Read.html literally has no methods for borrowing data. Everything that this trait does is defined in terms of copying data into a caller-provided buffer, not borrowing it from the underlying i/o object.)This PR changes the
Deserialize
impls to borrow from theDeserializer
instead, as opposed to the input data. This works efficiently in all formats. Deserializers that operate on i/o resources will let you borrow from the same buffer that they use for obtaining data from the underlyingRead
object. Deserializers that wrap an in-memory data structure likeserde_json::Value
will borrow from there without copying or allocation.