我正在尝试将活塞杆纹理存储在一个结构体中。
struct TextureFactory<R> where R: gfx::Resources {
block_textures: Vec<Rc<Texture<R>>>,
}
impl<R> TextureFactory<R> where R: gfx::Resources {
fn new(window: PistonWindow) -> Self {
let texture = Rc::new(gfx_texture::Texture::from_path(
&mut *window.factory.borrow_mut(),
"assets/element_red_square.png",
Flip::None, &TextureSettings::new()
).unwrap());
let block_textures = Vec::new();
block_textures.push(texture);
TextureFactory {
block_textures: block_textures,
}
}
}
这段代码无法编译:
src/main.rs:37:9: 39:10 error: mismatched types:
expected `TextureFactory<R>`,
found `TextureFactory<gfx_device_gl::Resources>`
(expected type parameter,
found enum `gfx_device_gl::Resources`)
gfx_device_gl::Resources
实现了 gfx::Resources
(我认为这只是特定设备的实现)。我实际上并不关心它的类型,但我需要知道这样我才能将其存储在结构体中。
我在 Github 上创建了一个可编译的仓库。
(我怀疑问题 Rust generics/traits: "expected 'Foo<B>', found 'Foo<Foo2>'" 是相同的问题,但我无法弄清如何将其应用于我的问题。)