Repository navigation
feat: IStreamInto decodes into a message the caller owns - #154
Merged
Merged
Conversation
IStreamObject owns the message it fills, so a caller who already has one -- which every generated try_decode does -- cannot hand it over. The generator worked around that with an IStreamInline whose callback forwards to the destination, and that shape needs the stream inside its own callback: the lambda cannot capture the object it is being handed to, so the generated code reached it through a pointer patched in right after construction. IStreamInto is IStreamObject with the instance taken out: it routes each top-level field into the message handed to its constructor and holds a pointer where IStreamObject holds a MessageType. Same callback, same dispatch, no knot -- try_decode becomes three lines, and the same three in both C++ profiles (corelib-c-cpp#197 adds the counterpart there, sofa-buffers/generator#744). The contract is IStreamObject's, with one difference the doc states: the destination is the caller's, so putting it back between messages is the caller's too. reset() here resets the DECODER alone -- generated code calls the message's own reset(), which keeps the buffers it has already paid for, the reason to hand a destination in at all. A rejected input leaves the fields decoded before the error in it, as it always has for an object decoded into. Tests: the caller's object is the one written (all five checks fail if the stream stages in one of its own -- verified by mutation), what the caller cleared stays cleared, an absent field keeps the previous value (§2, stated because it is the contract), and a rejected message leaves what it had decoded. README gains the three-line form beside the reset() section.
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.
Prerequisite for sofa-buffers/generator#744, and the twin of sofa-buffers/corelib-c-cpp#197 — the point of doing both is that the two C++ profiles stop having two shapes for the same entry point.
What
IStreamObject<T>owns the message it fills. A caller who already has one — which every generatedtry_decodedoes — cannot hand it over, so the generator worked around it with anIStreamInlinewhose callback forwards to the destination. That shape needs the stream inside its own callback, so the lambda cannot capture the object it is being handed to:IStreamInto<T>isIStreamObject<T>with the instance taken out — it routes each top-level field into the message handed to its constructor, and holds a pointer where the other holds aMessageType. Same callback, same dispatch:_out.reset(); sofab::IStreamInto<Example> _in{_out, sofab::Limits{SIZE_MAX}}; return _in.feed(_data, _len);Those three lines are now what both C++ profiles generate, the fixed one included (corelib-c-cpp#197).
The contract
IStreamObject's, with one difference the doc states: the destination is the caller's, so putting it back between messages is the caller's too.reset()here resets the decoder alone — generated code calls the message's ownreset(), which keeps the buffers it has already paid for, the reason to hand a destination in at all. A rejected input leaves the fields decoded before the error in it, as it always has for an object decoded into.Tests
test/test_roundtrip.cppgainsdecodeIntoCallerObject(): the caller's object is the one written, what the caller cleared stays cleared, an absent field keeps the previous value (§2 — stated because it is the contract, not a bug), and a rejected message leaves what it had decoded before the error.Mutation-checked: giving
IStreamIntoan instance of its own and decoding into that fails 5 of the 5 new checks, with the suite's own messages.README gains the three-line form beside the
reset()section.No existing API changed.
IStreamObjectis untouched and still what a caller without a destination uses.