Conflicting implementation for models

I'm trying the ws, and would like to split the Handler trait implementation into multiple files, so I wrote the below:

on_open.rs:

impl Handler for Client {
    fn on_open(&mut self, _: Handshake) -> Result<()> {
        println!("Socket opened");
        Ok(())
    }
}

And in another file, on_message.rs:

impl Handler for Client {
    fn on_message(&mut self, msg: Message) -> Result<()> {
        println!("Server got message '{}'. ", msg);
        Ok(())
    }
}

While compiling I got the below error:

error[E0119]: conflicting implementations of trait `ws::handler::Handler` for type `models::client::Client`:
 --> src\sockets\on_message.rs:9:1
  |
9 | impl Handler for Client {
  | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `models::client::Client`
  |
 ::: src\sockets\on_open.rs:8:1
  |
8 | impl Handler for Client {
  | ----------------------- first implementation here

I want the files to be separated so that each developer work on a separate one, is there a way to make it, or I'm forced to have the full train implementation into single file?

I wouldn't recommend splitting but could use include!. It's bad for adding functionality since everything would need to be nested in the one function.

You can't split trait implementation. The best you can do is to delegate to a non-trait method:

impl Handler for Client {
fn on_open(&mut self, _: Handshake) -> Result<()> {
   self.actual_on_open()
}
fn on_message(&mut self, msg: Message) -> Result<()> {
   self.actual_on_message(msg)
}
}

and somewhere else:

impl Client { // NOT handler
   fn actual_on_open(&mut self, etc…) {etc…}
}
1 Like

Thanks, my app structure is as shown, and it worked by calling mod sockets; which is the content of the actual sockets implementation.
I added the mod sockets; in the main.rs, in the same main.rs I added mod socket; to connect the handler, can not I call the mod sockets; in the socket.rs itself instead of doing so from the main.rs

hope my long statement is clear :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.