therealprof / display-interface

Rust crates providing a generic interface for display drivers and some default implementations (GPIO, SPI and I2C)
Apache License 2.0
73 stars 27 forks source link

Add trait bounds to `new` methods #49

Open rfuest opened 7 months ago

rfuest commented 7 months ago

At the moment the new methods in I2CInterface and SPIInterface don't have any trait bounds, which makes it possible to create instances of these types which don't implement the WriteOnlyCommand trait. This makes the display interface unusable, but doesn't generate an error immediately. Requiring the necessary traits when new is called would generate better compiler errors and make it easier to pinpoint the cause of the error by users.

pdgilbert commented 4 months ago

I think this issue is also the source of a problem using ssd1306 with rtic:

...
error[E0599]: the method `clear_buffer` exists for mutable reference `&mut Ssd1306<I2CInterface<...>, ..., ...>`, but its trait bounds were not satisfied
   --> examples/rtic/display_stuff_rtic.rs:215:28
    |
215 |           cx.local.display.clear_buffer();
    |                            ^^^^^^^^^^^^ method cannot be called on `&mut Ssd1306<I2CInterface<...>, ..., ...>` due to unsatisfied trait bounds
    |
   ::: /home/paul/.cargo/registry/src/index.crates.io-6f17d22bba15001f/display-interface-i2c-0.5.0/src/lib.rs:10:1
    |
10  | pub struct I2CInterface<I2C> {
    | ---------------------------- doesn't satisfy `_: WriteOnlyDataCommand`
    |
...

(I have tried using several forks of ssd1306, for example, @bugadan fork branch = "ehal1" and @markszente fork branch = "master").