Tôi đang cố gắng lưu trữ kết cấu piston trong cấu trúc.Lỗi "Tham số kiểu dự kiến" trong hàm tạo của cấu trúc chung
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,
}
}
}
này không biên dịch:
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
dù (tôi nghĩ nó chỉ là việc thực hiện thiết bị cụ thể.) Tôi không thực sự quan tâm đến những gì kiểu này, nhưng tôi cần phải biết để mà tôi có thể lưu trữ nó trong cấu trúc.
Tôi đã tạo compilable repo on Github.
(tôi nghi ngờ Rust generics/traits: "expected 'Foo<B>', found 'Foo<Foo2>'" là cùng một câu hỏi, nhưng tôi không thể tìm ra cách để áp dụng nó vào vấn đề của tôi.)
Bản sao có thể có của http://stackoverflow.com/questions/31490913/rust-generics-expected-t-found-foo hoặc http://stackoverflow.com/questions/31060851/generics-error-expected-type- parameter-found-struct – Shepmaster
Bạn có thể sử dụng [đối tượng trait] (http://doc.rust-lang.org/book/trait-objects.html) để đạt được loại đa hình mà mã của bạn có vẻ liên quan. – cheme