I have two traits, TypeOne
and TypeTwo
describing different kinds of objects. Both implement Sized
so they are not object-safe (I cannot implement for dyn TypeOne
or dyn TypeTwo
). I also have a third trait Hashable
which I would like to implement for both TypeOne
and TypeTwo
.
pub trait Hashable {
fn hash(&self) -> &str;
}
pub trait TypeOne { }
pub trait TypeTwo { }
impl<T: TypeOne> Hashable for T {
fn hash(&self) -> &str {
todo!();
}
}
impl<T: TypeTwo> Hashable for T {
fn hash(&self) -> &str {
todo!();
}
}
The above code gives a conflicting implementations error, which I fully understand. If a struct were to implement both TypeOne
and TypeTwo
it would have two different (i.e. conflicting) implementations.
From my understanding after asking a related question, there is a way to circumvent this limitation when the traits are defined within the same crate (see @kmdreko's response). Does anyone know a way around this?