Rust

General Notes

On the usage of impl Trait

impl Trait is not intended as a replacement for dynamic dispatch (dyn Trait).

It only allows you to write a placeholder for some types that are unrepresentable, like closures and iterators.

On creating config files

always have a version field

That:

  • Never changes place.
  • Exists from the beginning of the versions of the config file.
  • Preferably at the top level of the file.

This way we can easily check if a config file is compatible with our parser.

It is also interesting to separate the config structure+parser in a crate, so we can use it’s version exclusively for this.

Trick for checking the version of a unknown file

Create a struct with just the version field, and without #[serde(deny_unknown_fields)], so it can “extract” the version field safely from a file that you don’t know the rest of its structure and discard the rest.

Compound Match cases

match value {
  pattern if expr => { value },
  /*...*/
}

Embedded Rust

General Reminders

Implementing traits for all peripheral ‘kinds’ (as in peripherals that have the same function but different hardware implementations) eases migrating in the future from one implementation to the next and also allows you to implement virtual peripherals for testing without the real hardware.