我试图将我的头脑包裹在特质界限和生命周期指示符之间,但我真的不知道这里有什么问题。
fn render_screen<'a, T>(window: &mut RenderWindow, chip: &mut Chip, rect: &'a mut T)
where
T: Shape<'a> + Drawable,
{
window.clear(&Color::BLACK);
for x in 0..SCREEN_COLUMNS {
for y in 0..SCREEN_ROWS {
if chip.vid_mem[y][x] == 1 {
let x_pos = (x * SCALE) as f32;
let y_pos = (y * SCALE) as f32;
&mut rect.set_position((x_pos, y_pos));
window.draw(&rect);
}
}
}
}
error[E0277]: the trait bound `&'a mut T: sfml::graphics::Drawable` is not satisfied
--> src/main.rs:114:29
|
114 | window.draw(&rect);
| ^^^^^ the trait `sfml::graphics::Drawable` is not implemented for `&'a mut T`
|
= note: required for the cast to the object type `sfml::graphics::Drawable`
我真的不知道如何指定我的问题,因为我只写了Rust大约3个星期,而且我还不熟悉这种语言。
答案 0 :(得分:0)
the trait bound `&'a mut T: sfml::graphics::Drawable` is not satisfied
那是因为它不是 - 所有你需要的是T
实现Drawable
:
T: Shape<'a> + Drawable,
T
,&T
和&mut T
都是不同的类型;仅仅因为T
实现特征并不意味着&mut T
。如果您需要对T
的可变引用设置限制,您可以:
fn render_screen<'a, T>(window: &mut RenderWindow, chip: &mut Chip, rect: &'a mut T)
where
T: Shape<'a>,
for <'a> &'a mut T: Drawable,
您也可以接受具有适当界限的T
:
fn render_screen<'a, T>(window: &mut RenderWindow, chip: &mut Chip, mut rect: T)
where
T: Shape<'a> + Drawable,
然后只需传入一个&mut Foo
即可。