Allow deserialization from std::io::Read
#10
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.
This Pull Request implements BCS deserialization from implementers of
std::io::Read.Strategy
The PR aims to maintain a minimal diff over the previous implementation. To that end, the following changes have been made.
Deserializergeneric over an inner typeRBcsReader, which implements BCS in terms of a few primitive operations. Implementserde::Deserialize<'de> for Deserializer<R> where Deserializer<R>: BcsReader<'de>BcsReader<'de>forDeserializer<&'de [u8]>using existing codeBcsReadergenerically for (a version of)Deserializer<R: Read>.Implementing
BcsReadergenericallyThe existing implementation is very close to supporting deserialization from readers. There are only two places in which its behavior relied on access to a byte slice was in the implementation of
de::MapAccess.The first case is
mapdeserialization.bcsneeds access to the serialized representation of map keys in order to enforce canonicity. When deserializing from a slice, this is straightforward - the implementation can simply deserialize a map key, and then "look back" at the original input slice to determine its serialized representation. When deserializing from a reader, however, this kind of rewinding is not generally possible. To solve this problem, I introduce a theTeeReaderstruct, which wraps aReader and optionally copies its bytes into acapture_bufferfor later retrieval. Then, I simply implementBcsReaderforDeserializer<TeeReader<...>>.The second case is the
endmethod, which checks that all input bytes have been consumed. To handle this case, I simply attempt to read one extra byte from theReader after deserialization and assert that an EOF error is returned from the underlyingReader.Testing
All existing unit tests except for
zero_copy_parsehave been modified to test that deserializationfrom_bytesandfrom_readeryield the same output. That test is not applicable since readers are not capable of zero-copy deserialization.