Sto tentando di memorizzare le trame del pistone in una struttura.Errore "parametro tipo previsto" nel costruttore di una struttura generica
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,
}
}
}
Questo non può essere compilato:
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
se (credo che sia solo l'implementazione specifica dispositivo.) Io in realtà non importa di che tipo si tratta, ma ho bisogno di sapere in modo che posso conservarlo nella struttura.
Ho fatto un compilable repo on Github.
(ho il sospetto Rust generics/traits: "expected 'Foo<B>', found 'Foo<Foo2>'" è la stessa domanda, ma non riesco a capire come applicarlo al mio problema.)
Possibile duplicato di http://stackoverflow.com/questions/31490913/rust-generics-expected-t-found-foo o http://stackoverflow.com/questions/31060851/generics-error-expected-type- parameter-found-struct – Shepmaster
Puoi usare [oggetti tratti] (http://doc.rust-lang.org/book/trait-objects.html), per ottenere il tipo di polimorfismo che il tuo codice sembra implicare. – cheme