|
|
|
|
|
by the__alchemist
1932 days ago
|
|
There's a tradeoff between catching errors at compile time as you describe, and code flexibility. For example, here's a line from a current project using one of the HALs: static SENSOR: Mutex<
RefCell<
Option<
Mlx9061x<
I2c<
I2C1,
(
PB6<Alternate<AF4, Output<OpenDrain>>>,
PB7<Alternate<AF4, Output<OpenDrain>>>,
),
>,
ic::Mlx90614,
>,
>,
>,
> = Mutex::new(RefCell::new(None));
The pin types here are due to this type of programming. They aren't used by the I2c peripheral; they're just for the check.If you only use a peripheral struct (eg i2c here) in the main function, the type state system makes sense. If you pass it across function boundaries, or use statically like, this, it may not be. The rust HALs and tutorials that use this pattern tend to leave function boundaries etc (where you need to explicitly declare types) out of examples. |
|