【发布时间】:2015-09-13 15:17:07
【问题描述】:
我正在尝试将活塞纹理存储在结构中。
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 implements gfx::Resources 虽然(我认为这只是特定于设备的实现。)我实际上并不关心这是什么类型,但我需要知道以便我可以将它存储在结构中。
(我怀疑Rust generics/traits: "expected 'Foo<B>', found 'Foo<Foo2>'" 是同一个问题,但我不知道如何将其应用于我的问题。)
【问题讨论】:
-
您可以使用trait objects,来实现您的代码似乎涉及的那种多态性。