I am trying to write a game in Rust using piston_window(0.77.0) library. Starting from their hello world example I thought I would start by separating of rendering logic into a method using Event as parameter since according to documentation it is returned by window.next()
.
use piston_window::*;
pub struct UI<'a> {
window: &'a mut PistonWindow,
}
impl <'a> UI<'a> {
pub fn new(window: &mut PistonWindow) -> UI {
UI {
window,
}
}
fn render(&self, event: &Event) {
self.window.draw_2d(&event, |context, graphics| {
clear([1.0; 4], graphics);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 100.0, 100.0],
context.transform,
graphics);
});
}
pub fn run(&mut self) {
use Loop::Render;
use Event::Loop;
while let Some(event) = self.window.next() {
match event {
Loop(Render(_)) => self.render(&event),
_ => {}
}
}
}
}
However this ends with an error:
self.window.draw_2d(&event, |context, graphics| {
the traitpiston_window::GenericEvent
is not implemented for&piston_window::Event
Code without extracted render method works as expected.
pub fn run(&mut self) {
use Loop::Render;
use Event::Loop;
while let Some(event) = self.window.next() {
match event {
Loop(Render(_)) => {
self.window.draw_2d(&event, |context, graphics| {
clear([1.0; 4], graphics);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 100.0, 100.0],
context.transform,
graphics);
});
},
_ => {}
}
}
}
How can I extract this? Is there something I am overlooking?