I have a snake game with 2 types of food:
The Fly
:
struct Fly {
x: u32,
y: u32,
}
And the Spider
, which disappears if not eaten in time:
struct Spider {
x: u32,
y: u32,
ticks_alive: i32,
}
I want them to have some convenient methods like position(&self) -> (u32, u32)
or proxy() -> SpiderOrFly
:
pub trait Food<Type> {
fn position(&self) -> (u32, u32) {
(self.x, self.y)
}
fn proxy() -> Type {
Type { x: 0, y: 0 }
}
}
I'm getting errors that x
is not a field of &self
and that Type
is not a struct
Is there a way to tell Rust that Type
has to be Fly
or Spider
so that I can access the instance variables in the generic implementation and don't have to produce doubled code?