泛型結構構造函數中的“預期類型參數”錯誤我試圖存儲活塞紋理在一個結構。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
提交
取消