I have two enums
, and there is some connection between their variant:
enum A {
Lo,
Sh
}
enum B {
Lo,
Sh
}
A::Lo
and B::Lo
is classfied in the same group, so:
trait PrintDire {
fn print(&self);
}
impl PrintDire for A {
fn print(&self) {
match self {
Self::Lo => println!("Lo"),
Self::Sh => println!("Sh")
}
}
}
impl PrintDire for B {
fn print(&self) {
match self {
Self::Lo => println!("Lo"),
Self::Sh => println!("Sh")
}
}
}
Obviously, the code repeat itself. So is there a way I can better organise the data structure?
I can figure out one style, of course it can not complie:
struct ALo;
struct ASh;
struct BLo;
struct BSh;
trait A {}
trait B {}
trait Lo {}
trait Sh {}
impl A for ALo {}
impl A for ASh {}
impl B for BLo {}
impl B for BSh {}
impl Lo for ALo {}
impl Lo for BLo {}
impl Sh for ASh {}
impl Sh for BSh {}
trait PrintDire {
fn print(&self);
}
impl<T> PrintDire for T
where
T: Lo
{
fn print(&self) {
println!("Lo")
}
}
impl<T> PrintDire for T
where
T: Sh,
{
fn print(&self) {
println!("Sh")
}
}