I have a trait with a couple of methods. Some, but not all structs that implement this trait have all the same implementation of one of the methods:
trait Blabla {
fn xy(self, x: u32, y: u32) -> u32;
}
// ---------------------------------
struct Ding {}
impl Blabla for Ding {
fn xy(self, x: u32, y: u32) -> u32 {
x + y
}
}
// ---------------------------------
struct Dong {
dong_attribute: u32,
}
impl Dong {
fn get_1(self) -> u32 {
1
}
}
impl Blabla for Dong {
fn xy(self, x: u32, y: u32) -> u32 {
x + y + self.get_1()
}
}
// ---------------------------------
struct Dung {
dung_attribute: f32,
}
impl Dung {
fn get_1(self) -> u32 {
1
}
}
impl Blabla for Dung {
fn xy(self, x: u32, y: u32) -> u32 {
x + y + self.get_1()
}
}
There are three structs in that example, each implementing the Blabla
trait, and two of which implementing the xy
method exactly the same way.
Is there a way to have them share the code for that function?
I was thinking of a second trait that inherits from Blabla
and provide xy
as a default implementation of that trait, like this:
trait Blabla {
fn xy(self, x: u32, y: u32) -> u32;
}
// ----------------------------------
trait Blubblub: Blabla {
fn get_1(self) -> u32;
fn xy(self, x: u32, y: u32) -> u32 {
x + y + self.get_1()
}
}
// ---------------------------------
struct Ding {}
impl Blabla for Ding {
fn xy(self, x: u32, y: u32) -> u32 {
x + y
}
}
// ---------------------------------
struct Dong {
dong_attribute: u32,
}
impl Blubblub for Dong {
fn get_1(self) -> u32 {
1
}
}
// ---------------------------------
struct Dung {
dung_attribute: f32,
}
impl Blubblub for Dung {
fn get_1(self) -> u32 {
1
}
}
fn main() {
let x = Dung {
dung_attribute: 1.0,
};
println!{"{}", x.xy(1,1)};
}
This doesn't work because trait inheritance in Rust means that both traits need to be implemented, and thus it doesn't save me from typing the same code in the implementation of Blabla::xy
for Dong
and Dung
.
Implement Blabla
with the default implementation for all Blubblub
and your example works:
trait Blubblub: Blabla {
fn get_1(self) -> u32;
}
impl<T: Blubblub> Blabla for T {
fn xy(self, x: u32, y: u32) -> u32 {
x + y + self.get_1()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With