Commit 9439a7b
committed
Add support for mapping old fields to new ones in TLV read macros
As we've grown, we regularly face a question of whether to "break
out" of our nice TLV-based struct/enum reading/writing macros in
order to handle mapping legacy fields to new ones, or deal with
keeping the legacy fields and handling at runtime what should be
hanlded at (de-)serialization time.
This attempts to address this tradeoff by adding support for a
"legacy" TLV read. This read style allows us to read a TLV which is
not directly mapped to any fields in the struct/enum but which can
be computed from the struct/enum's layout at write-time and which
is incorporated into the read data at read-time.
It takes a type, a `$read` expression (which is executed after all
TLVs are read but before the struct/enum is built) and a `$write`
expression (which is executed to calculate the value to write in
the TLV).
They are always read as `option`s to retain a future ability to
remove the `legacy` fields.
Sadly, there's two issues with doing this trivially which force us
into `proc-macro` land:
(a) when matching the original struct we want to list the fields
in the match arm so that we have them available to write.
Sadly, we can't call a macro to have it write out the field
name based on the field type, so instead need to pass the whole
match to a proc-macro and have it walk through to find the
types and skip fields that are `legacy`.
(b) when building a final struct/enum after reading, we need to
list a few `$field: $expr`s and cannot decide whether to
include a field based on a regular macro.
The proc-macros to do so aren't trivial, but they aren't that bad
either. We could instead try to rewrite our TLV stream processing
macros to handle a new set of TLVs which are passed via a separate
argument, but as TLVs are required to in ordered by type this
requires a good chunk of additional generated code in each TLV
write. It also would result in a somewhat less ergonomic callsite
as it would no longer fit into our existing list of TLVs.1 parent 1e0f43f commit 9439a7b
File tree
3 files changed
+228
-14
lines changed- lightning-macros/src
- lightning
- src/util
3 files changed
+228
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
| |||
206 | 209 | | |
207 | 210 | | |
208 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
209 | 215 | | |
210 | 216 | | |
211 | 217 | | |
| |||
284 | 290 | | |
285 | 291 | | |
286 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
287 | 296 | | |
288 | 297 | | |
289 | 298 | | |
| |||
341 | 350 | | |
342 | 351 | | |
343 | 352 | | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
344 | 356 | | |
345 | 357 | | |
346 | 358 | | |
| |||
388 | 400 | | |
389 | 401 | | |
390 | 402 | | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
391 | 407 | | |
392 | 408 | | |
393 | 409 | | |
| |||
449 | 465 | | |
450 | 466 | | |
451 | 467 | | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
452 | 479 | | |
453 | 480 | | |
454 | 481 | | |
| |||
605 | 632 | | |
606 | 633 | | |
607 | 634 | | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
608 | 638 | | |
609 | 639 | | |
610 | 640 | | |
| |||
771 | 801 | | |
772 | 802 | | |
773 | 803 | | |
| 804 | + | |
774 | 805 | | |
775 | 806 | | |
776 | 807 | | |
| |||
818 | 849 | | |
819 | 850 | | |
820 | 851 | | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
821 | 855 | | |
822 | 856 | | |
823 | 857 | | |
| |||
877 | 911 | | |
878 | 912 | | |
879 | 913 | | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
880 | 918 | | |
881 | 919 | | |
882 | 920 | | |
| |||
932 | 970 | | |
933 | 971 | | |
934 | 972 | | |
935 | | - | |
| 973 | + | |
936 | 974 | | |
937 | 975 | | |
938 | 976 | | |
939 | | - | |
| 977 | + | |
940 | 978 | | |
941 | 979 | | |
942 | 980 | | |
| |||
1030 | 1068 | | |
1031 | 1069 | | |
1032 | 1070 | | |
1033 | | - | |
1034 | | - | |
| 1071 | + | |
| 1072 | + | |
1035 | 1073 | | |
1036 | 1074 | | |
1037 | 1075 | | |
| |||
1049 | 1087 | | |
1050 | 1088 | | |
1051 | 1089 | | |
1052 | | - | |
| 1090 | + | |
1053 | 1091 | | |
1054 | 1092 | | |
1055 | 1093 | | |
| |||
1119 | 1157 | | |
1120 | 1158 | | |
1121 | 1159 | | |
1122 | | - | |
| 1160 | + | |
1123 | 1161 | | |
1124 | 1162 | | |
1125 | 1163 | | |
1126 | | - | |
| 1164 | + | |
1127 | 1165 | | |
1128 | 1166 | | |
1129 | 1167 | | |
| |||
1168 | 1206 | | |
1169 | 1207 | | |
1170 | 1208 | | |
1171 | | - | |
| 1209 | + | |
1172 | 1210 | | |
1173 | 1211 | | |
1174 | 1212 | | |
1175 | | - | |
| 1213 | + | |
1176 | 1214 | | |
1177 | 1215 | | |
1178 | 1216 | | |
| |||
1231 | 1269 | | |
1232 | 1270 | | |
1233 | 1271 | | |
1234 | | - | |
| 1272 | + | |
1235 | 1273 | | |
1236 | 1274 | | |
1237 | 1275 | | |
1238 | | - | |
| 1276 | + | |
1239 | 1277 | | |
1240 | 1278 | | |
1241 | 1279 | | |
| |||
1287 | 1325 | | |
1288 | 1326 | | |
1289 | 1327 | | |
1290 | | - | |
| 1328 | + | |
1291 | 1329 | | |
1292 | 1330 | | |
1293 | 1331 | | |
1294 | | - | |
| 1332 | + | |
1295 | 1333 | | |
1296 | 1334 | | |
1297 | 1335 | | |
| |||
0 commit comments