泛型结构构造函数中的“预期类型参数”错误我试图存储活塞纹理在一个结构。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不过(我认为这只是设备特定的实现)。我并不关心这是什么类型,但我需要知道,这样我才能将它存储在结构中。我做了一个GUUTUB上的可编译回购.(我怀疑锈病泛型/性状:“预期‘foo<B>’,发现‘foo<foo 2>’”是同一个问题,但我想不出如何把它应用到我的问题上。)
1 回答
白板的微信
TA贡献1883条经验 获得超3个赞
struct Foo<T> { val: T,}impl<T> Foo<T> { fn new() -> Self { Foo { val: true } }}fn main() {}
impl<T> Foo<T> { fn new() -> Self { /* ... */ }}
T
Foo
bool
T
bool
new
T
impl Foo<bool> { fn new() -> Self { Foo { val: true } }}
new
impl TextureFactory<gfx_device_gl::Resources> { /* ... */ }
gfx_device_gl::Resources
impl Foo<Box<dyn std::fmt::Display>> { fn new() -> Self { Foo { val: Box::new(true) } }}
impl Trait
#![feature(existential_type)]struct Foo<T> { val: T,}existential type D: std::fmt::Display;impl Foo<D> { fn new() -> Self { Foo { val: true } }}
添加回答
举报
0/150
提交
取消