Implementing Mutually Exclusive Traits and Trait Bounds

In that case you can create a local copy of Handler and then write the blanket impl in terms of the local copy. The local copy will contain the actual implementation.

use some_crate::Handler;

trait MyHandler<M, P = M::Prio> where M : Message {
    fn my_handle(&self, msg : M);
}

impl<M> MyHandler<M, Normal> for MedicalDevice
where M : Message<Prio=Normal> {
    fn my_handle(&self, msg : M) {
        println!("Reacting slowly to {:?}",msg);
    }
}

impl<M> MyHandler<M, Urgent> for MedicalDevice
where M : Message<Prio=Urgent> {
    fn my_handle(&self, msg : M) {
        println!("Reacting urgently to {:?}",msg);
    }
}

impl<M> Handler<M> for MedicalDevice
where
    M: Message,
    Self: MyHandler<M>,
{
    fn handle(&self, msg : M) {
        self.my_handle(msg)
    }
}

looks like you do need to specify the second type parameter explicitly :frowning_face:

3 Likes