Why does the following code compile?

I'm trying to add support for embedded-graphics to my display driver crate.

I have the following code in my lib.rs:

// lib.rs

pub struct ILI9486<CSX, RSX, IF> {
    interface: IF,
    chip_select: CSX,
    reset: RSX,
}

I created a file graphics.rs to add the implementation of the required trait DrawTarget.

// graphics.rs

use embedded_graphics::DrawTarget;

impl DrawTarget for ILI9486 {}

With that code, the code compiles, even though I didn't implement draw_pixel() or size(). Why does it compile?

I would expect the compiler to complain. If I implement a trait without implementing a function the compiler complains:

pub trait MyTrait {
    fn foo();
}

pub struct ILI9486<CSX, RSX, IF> {
    interface: IF,
    chip_select: CSX,
    reset: RSX,
}

impl<CSX, RSX, IF> MyTrait for ILI9486<CSX, RSX, IF>
where
    IF: WriteOnlyDataCommand,
    CSX: OutputPin,
    RSX: OutputPin,
{
}

which results in:

error[E0046]: not all trait items implemented, missing: `foo`
  --> crates/ili9486-rs/src/lib.rs:16:1
   |
7  |       fn foo();
   |       --------- `foo` from trait
...
16 | / impl<CSX, RSX, IF> MyTrait for ILI9486<CSX, RSX, IF>
17 | | where
18 | |     IF: WriteOnlyDataCommand,
19 | |     CSX: OutputPin,
20 | |     RSX: OutputPin,
21 | | {
22 | | }
   | |_^ missing `foo` in implementation

So I am puzzled.

Add the following line to lib.rs:

mod graphics;

Without this line, the graphics.rs file is not included in your library, so the compiler never even tries to compile it.

Alright, that makes sense. Thank you :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.