活塞 GenericEvent 未为事件实现
Piston GenericEvent not implemented for Event
我正在尝试使用 piston_window(0.77.0) 库在 Rust 中编写游戏。从他们的 hello world example I thought I would start by separating of rendering logic into a method using Event as parameter since according to documentation 开始,它由 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),
_ => {}
}
}
}
}
然而,这以错误结束:
self.window.draw_2d(&event, |context, graphics| {
the trait piston_window::GenericEvent
is not implemented for &piston_window::Event
没有提取渲染方法的代码按预期工作。
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);
});
},
_ => {}
}
}
}
我怎样才能提取这个?有什么我忽略的吗?
event
变量的类型为 &Event
,而不是 Event
,因此您实际上是在尝试将 &&Event
传递给 window.draw_2d
。 Event
实现了 GenericEvent
但 &Event
没有实现,这就是您看到该错误的原因。
您只需要做:
self.window.draw_2d(event, |context, graphics| {
...
}
而不是:
self.window.draw_2d(&event, |context, graphics| {
...
}
公平地说,Rust 编译器为您指明了正确的方向。当我编译你的代码时,完整的错误信息是:
error[E0277]: the trait bound `&piston_window::Event: piston_window::GenericEvent` is not satisfied
--> src/main.rs:34:21
|
34 | self.window.draw_2d(&event, |context, graphics| {
| ^^^^^^^ the trait `piston_window::GenericEvent` is not implemented for `&piston_window::Event`
|
= help: the following implementations were found:
<piston_window::Event as piston_window::GenericEvent>
最后的 "help" 部分告诉你 piston_window::Event
确实 有正确的实现,而前面的错误是说 &piston_window::Event
没有。
我正在尝试使用 piston_window(0.77.0) 库在 Rust 中编写游戏。从他们的 hello world example I thought I would start by separating of rendering logic into a method using Event as parameter since according to documentation 开始,它由 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),
_ => {}
}
}
}
}
然而,这以错误结束:
self.window.draw_2d(&event, |context, graphics| {
the traitpiston_window::GenericEvent
is not implemented for&piston_window::Event
没有提取渲染方法的代码按预期工作。
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);
});
},
_ => {}
}
}
}
我怎样才能提取这个?有什么我忽略的吗?
event
变量的类型为 &Event
,而不是 Event
,因此您实际上是在尝试将 &&Event
传递给 window.draw_2d
。 Event
实现了 GenericEvent
但 &Event
没有实现,这就是您看到该错误的原因。
您只需要做:
self.window.draw_2d(event, |context, graphics| {
...
}
而不是:
self.window.draw_2d(&event, |context, graphics| {
...
}
公平地说,Rust 编译器为您指明了正确的方向。当我编译你的代码时,完整的错误信息是:
error[E0277]: the trait bound `&piston_window::Event: piston_window::GenericEvent` is not satisfied
--> src/main.rs:34:21
|
34 | self.window.draw_2d(&event, |context, graphics| {
| ^^^^^^^ the trait `piston_window::GenericEvent` is not implemented for `&piston_window::Event`
|
= help: the following implementations were found:
<piston_window::Event as piston_window::GenericEvent>
最后的 "help" 部分告诉你 piston_window::Event
确实 有正确的实现,而前面的错误是说 &piston_window::Event
没有。