"expected type parameter, found struct", what I did was wrong here?


#1
pub struct Handler {

}

pub struct Client {

}

impl Handler {
    pub fn new() -> Handler {
        Handler{}
    }
}

impl Service for Handler {
    fn client<C: ServiceClient>(&self) -> C {
                                          - expected `C` because of return type
        Client{}
        ^^^^^^^^ expected type parameter, found struct `infrastructure::server::handler::Client`
    }
}

impl ServiceClient for Client {
    fn read(&mut self, reader: &Read) -> StdIoResult<usize> {
        Ok(0)
    }
}

In Service, method client creates a Client which implements ServiceClient. But this cause Rust refuse to compile.

What’s wrong with my code?

Here is the declaration of those two traits:

pub trait ServiceClient {
    fn read(&mut self, reader: &Read) -> StdIoResult<usize>;
}

pub trait Service {
    fn client<C: Client>(&self) -> C;
}

#2

The caller of client gets to choose which type C is, not the implementation. You can use an associated type:

trait Service {
    type Client: ServiceClient;

    fn client(&self) -> Self::Client;
}

#3

Ok, problem seems to be resolved. Thank you very much :grinning:

Now, I have to get some sleep that I should be getting 8 hours ago.