Hacker News new | ask | show | jobs
by pornel 1936 days ago
Another option would be to define a type alias:

    #[cfg(feature = "splitapple")]
    type Port = nrf52840_hal::pac::P1;

    #[cfg(feature = "keytron")]
    type Port = nrf52833_hal::pac::P0;

    fn read_keys(port: Port) -> Packet {}

but I think the whole problem here is that author tried to do what would have been #ifdef in C, but that doesn't get you far in Rust when macros are AST-based and types are more specific than `int` and `char*`.

A more "rustic" solution would be to use a trait, e.g.

    trait ReadKeys {
       type Port;
       fn read_keys(Self::Port) -> Packet {}
    }

    impl ReadKeys for Splitapple {…}
    impl ReadKeys for Keytron {…}