Replace repr with with - #172
Draft
csnover wants to merge 2 commits into
Draft
Conversation
csnover
force-pushed
the
with-outwith-reprs
branch
3 times, most recently
from
November 21, 2022 01:33
2111381 to
e2a91f7
Compare
This feature does not work and fundamentally cannot work because the write-side always receives references to values, but (1) there are no `From<&T>` conversions for any std type that anyone would use as a repr, and (2) this would cause a complete copy of the converted object to be retained in memory just to write the object. The `with` feature offers a more appropriate approach to custom serialisations that does not suffer from these limitations. This reverts: "Implement top-level `repr` attribute for structs and enums." commit 6f32003 "Fix nightly tests" commit 38b48d4 "Implement field-level `repr` for conversion." commit 8353699 ...with the exception of portions of those commits which fixed a bug where arbitrary errors from `try_map` in BinWrite were not allowed due to overly-restrictive type hints.
csnover
force-pushed
the
with-outwith-reprs
branch
from
November 26, 2022 21:13
e2a91f7 to
99696d4
Compare
Codecov Report
@@ Coverage Diff @@
## master #172 +/- ##
==========================================
- Coverage 95.53% 95.32% -0.22%
==========================================
Files 64 64
Lines 6719 6734 +15
==========================================
Hits 6419 6419
- Misses 300 315 +15
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
csnover
marked this pull request as draft
November 29, 2022 15:28
jam1garner
force-pushed
the
master
branch
3 times, most recently
from
February 5, 2023 03:17
10520d3 to
9175d20
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Unfortunately, the
reprfeature does not work and fundamentally cannot work efficiently because the write-side always receives references to values, but (1) there are noFrom<&T>conversions for any std type that anyone would use as a repr, and (2) this would cause a complete copy of the converted object to be retained in memory just to write the object. So this PR removesreprentirely from the library and replaces it with something that definitely doesn’t have any of its own serious flaws or unnecessary complexity 👁️👄👁️.The replacement feature,
with, uses separate conversion types.To use a conversion type, there are a few options: the attribute (e.g.
#[brw(with(NullString))] field: String), theWithwrapper (e.g.field: With<NullString, String>), or by calling the methods of the newReadWithandWriteWithtraits or the new methods on the existingBinReaderExtandBinWriterExttraits.To implement a conversion type, implement
ReadFrom<ConverterT> for TandWriteInto<ConverterT> for T. A converter can be used with many types; for example, in this patch, theNullStringconverter is implemented to read intoVec<u8>andStringtypes, and to write from[u8],str, orStringtypes. (n.b. Should that actually use a generic for anything that is likeAsRef<[u8]>? Maybe, please bring it up in a code review!)Since this creates a new idiom for how to serialise objects that don’t have obvious default serialisations (i.e.
String), theNullStringandNullWideStringtypes are no longer containers, but rather just converters. Authors would switch to use an appropriate concrete type (Vec<u8>orString) and thewithdirective, or else theWithwrapper class.This patch doesn’t yet include any intermediate trait to enable third party to third party conversions as mentioned in #98. The number of different ways to do custom parsing is starting to feel a little burdensome; this seemed like a place to start.