This trait has two alternatives of implementing the same functionality. You should implement either get_x
and get_y
or get_coords
. Is there any way to catch this at compile time? current it compiles and runs, but it will just crash with a stack overflow.
#[derive(Debug, Default)]
struct Point {
x: i32,
y: i32,
}
trait Test {
fn get_x(&self) -> i32 {
self.get_coords().x
}
fn get_y(&self) -> i32 {
self.get_coords().y
}
fn get_coords(&self) -> Point {
Point {
x: self.get_x(),
y: self.get_y(),
}
}
}
// bad implementation, this should be a compile error
impl Test for Point {}
fn main() {
let a = Point::default();
println!("{:?}", a.get_coords());
}