Add async PacketWriter #32
Closed
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.
Given that this crate already supports async reads, it makes sense to complete its async support for writes for API parity.
To achieve this addition the packet writing logic was extracted to a new private struct,
BasePacketWriter, that is largely the same as before, with the exception of some refactors to delete data from ended streams of the map, which was a TODO comment, and getting rid of byteorder and fallible I/O operations. This new struct is not responsible for doing any I/O: it just invokes a callback, which can returnfalseto indicate a failure and stop the writing process.The blocking
PacketWriternow usesBasePacketWriterwith a callback that writes everything to the sink as before. Any error that might happen is stored and returned with the same user-visible behavior as before.The new async
PacketWriteris pretty trivial thanks to Tokio encoders and the usage of callbacks byBasePacketWriter, as it is possible to simply copy any written pages to a Tokio-provided buffer, which always succeeds. Tokio takes care of all the dirty details of actual I/O.