mpgen: support primitive std::optional struct fields#243
mpgen: support primitive std::optional struct fields#243ryanofsky wants to merge 3 commits intobitcoin-core:masterfrom
Conversation
This is a move-only change that should be easy to review with --color-moved. No behavior is changing.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-02-25 11:01:07 |
This doesn't change generated code at all, just noves functionality out of Generate method into a helper method so it can be reused in the next commit.
Currently optional primitive fields like `std::optional<int>` are not well supported as struct members. Non-primitive optional fields like `std::optional<std::string>` and optional struct fields are well-supported because Cap'n Proto allows non-primitive fields to be unset, but primitive fields are always considered set so there is natural way to represent null values. Libmultiprocess does already support primitive optional method parameters and result values, by allowing the .capnp files to declare extra boolean parameters prefixed with "has" and treating the extra boolean parameters as indicators of whether options are set or unset. This commit just this functionality to work for struct members as well. For example a C++ `std::optional<int> param` parameter can be represented by 'param :Int32, hasParam :Bool` parameters in a .capnp file and libmultiprocess will use both Cap'n Proto fields together to represent the C++ value. Now C++ struct fields can be represented the same way (see unit changes test for an example).
|
It would be useful to demonstrate that other implementations can straightforwardly handle this, e.g. with a functional tests in a Bitcoin Core branch (or PR). |
My next rebase of bitcoin/bitcoin#10102 will use this for the In the meantime I updated the PR description with a more specific example of how this can be used and I think the unit test should provide a good test of functionality. Updated 9f99c7d -> 4de4429 ( |
Currently C++ structs with primitive
std::optionalmembers (ints, bools, floats) cannot easily by mapped to Cap'n Proto structs because Cap'n Proto does not provide a way to leave primitive fields unset, so there isn't a natural way to representstd::nulloptvalues. This PR makes it possible to map C++ structs with fields like:std::optional<int> foo;to Cap'n Proto structs by using extra
Boolfields prefixed with "has" for primitive optional members:Boolean "has" fields were already supported by the code generator and used to pass primitive
std::optionalparameters and return values, so this PR just extends it work with all struct fields, not just fields in params and result structs.Note: Motivation for this change is dealing with the
CreatedTransactionResult::change_posfield introduced to the wallet interface in bitcoin-core/gui#807. This also could have been useful in bitcoin/bitcoin#33965 (comment)