Can't figure out the syntax for generics

Hello,

I'm struggling to find a way to write some stuff with generics, I hope I can get some help here :slight_smile:

I have the following struct:

pub struct ControlInterface<RESX, CSX, DCX, WRX, RDX, DB> {
    resx: Option<RESX>,
    csx: Option<CSX>,
    dcx: DCX,
    wrx: WRX,
    rdx: RDX,
    db: [DB; 8],
}

impl<RESX, CSX, DCX, WRX, RDX, DB> ControlInterface<RESX, CSX, DCX, WRX, RDX, DB>
where
    RESX: hal::digital::v2::OutputPin,
    CSX: hal::digital::v2::OutputPin,
    DCX: hal::digital::v2::OutputPin,
    WRX: hal::digital::v2::OutputPin,
    RDX: hal::digital::v2::OutputPin,
    DB: hal::digital::v2::OutputPin,
    RESX::Error: Debug,
    CSX::Error: Debug,
    DCX::Error: Debug,
    WRX::Error: Debug,
    RDX::Error: Debug,
    DB::Error: Debug,
{
	...
}

and I have a Command enum that I want to implement a function which takes a ControlInterface argument:

pub enum Command {
	Command1,
	Command2,
}

impl<RESX, CSX, DCX, WRX, RDX, DB> Command<RESX, CSX, DCX, WRX, RDX, DB> {
    fn send(self, ctrl: &ControlInterface<RESX, CSX, DCX, WRX, RDX, DB>) {
    	...
    }
}

With the code I posted I get the following error:

error[E0107]: wrong number of type arguments: expected 0, found 6
  --> src/ili9486/command.rs:59:44
   |
59 | impl<RESX, CSX, DCX, WRX, RDX, DB> Command<RESX, CSX, DCX, WRX, RDX, DB> {
   |                                            ^^^^  ^^^  ^^^  ^^^  ^^^  ^^ unexpected type argument
   |                                            |     |    |    |    |
   |                                            |     |    |    |    unexpected type argument
   |                                            |     |    |    unexpected type argument
   |                                            |     |    unexpected type argument
   |                                            |     unexpected type argument
   |                                            unexpected type argument

I already tried different permutations of <...> stuff, but I can't make it work. What am I missing here? I also tried removing the <...> stuff (btw, what do you call the <...> stuff?), and it doesn't work either:

impl Command {
    fn send(self, ctrl: &ControlInterface) {
    }
}

that gives:

error[E0107]: wrong number of type arguments: expected 6, found 0
  --> src/ili9486/command.rs:60:26
   |
60 |     fn send(self, ctrl: &ControlInterface) {
   |                          ^^^^^^^^^^^^^^^^ expected 6 type arguments

You can add the type parameters to the function signature where they are used:

fn send<RESX, CSX, DCX, WRX, RDX, DB>(self, ctrl: &ControlInterface<RESX, CSX, DCX, WRX, RDX, DB>) {
    // stuff
}

I believe type parameter is the correct term for the items inside the angle brackets: Type parameters - The Rust Reference
Not sure if there's a word for the "whole thing" including all the type parameters and the brackets

1 Like

It works, thanks :smiley:

1 Like

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.