Commit bdea1a1
committed
rust: macros: expose module parameters through MODPARAM struct
Instead of creating a struct + constant pair for each parameter,
expose the parameters as methods over a single struct MODPARAM.
In this example the parameter `foobar` is available at the Rust
end as `MODPARAM.foobar()`.
The rationale is that currently the `module!` macro generates a
constant with the name of the parameter which can lead to
surprising behavior when attempting to create a let binding of
the same name:
module! {
type: ParamName,
// ...
params: {
foobar : bool {
default: true,
permissions: 0o644,
description: "test",
},
},
}
fn clash() {
let foobar = 42;
}
This causes rustc to error out because `foobar` is treated as a
pattern on the left hand side of the let expression:
10 | / module! {
11 | | type: ParamName,
12 | | name: "param_name",
13 | | author: "A. U. Thor",
... |
21 | | },
22 | | }
| |_- constant defined here
...
25 | let foobar = 42;
| ^^^^^^ -- this expression has type `{integer}`
| |
| expected integer, found struct `__param_name_foobar`
| `foobar` is interpreted as a constant, not a new binding
| help: introduce a new binding instead: `other_foobar`
Essentially, having a parameter `foobar` prevents the creation of
a binding of the same name anywhere else in the module.
Signed-off-by: Philipp Gesang <phg@phi-gamma.net>
Suggested-by: Gary Guo <gary@garyguo.net>1 parent 5423795 commit bdea1a1
File tree
2 files changed
+33
-14
lines changed- rust/macros
- samples/rust
2 files changed
+33
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
332 | 333 | | |
333 | 334 | | |
334 | 335 | | |
| |||
388 | 389 | | |
389 | 390 | | |
390 | 391 | | |
391 | | - | |
| 392 | + | |
392 | 393 | | |
393 | 394 | | |
394 | 395 | | |
| |||
406 | 407 | | |
407 | 408 | | |
408 | 409 | | |
409 | | - | |
| 410 | + | |
410 | 411 | | |
411 | 412 | | |
412 | 413 | | |
| |||
421 | 422 | | |
422 | 423 | | |
423 | 424 | | |
| 425 | + | |
| 426 | + | |
424 | 427 | | |
425 | 428 | | |
426 | 429 | | |
| |||
436 | 439 | | |
437 | 440 | | |
438 | 441 | | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | 442 | | |
446 | 443 | | |
447 | 444 | | |
| |||
483 | 480 | | |
484 | 481 | | |
485 | 482 | | |
486 | | - | |
487 | 483 | | |
488 | 484 | | |
489 | 485 | | |
| |||
492 | 488 | | |
493 | 489 | | |
494 | 490 | | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
495 | 514 | | |
496 | 515 | | |
497 | 516 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | | - | |
58 | | - | |
| 57 | + | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
0 commit comments