I am New to Rust and come from another programming language.
here are my dependencies
[dependencies]
piston_window = "0.123.0"
piston = "0.53.1"
gfx = "0.18.2"
I am having a problem with the manual type hinting
when I load a Texture without adding a type hint the texture is loaded and can be displayed in the window.
let dirt = piston_window::Texture::from_path(&mut window.create_texture_context(),
Path::new("assets/sprites/dirt.png"), Flip::None, &TextureSettings::new()).unwrap();
when I try to manually add the type hint which vscode recommends me, I get an Error. e.g
let dirt: Texture<Resources>= piston_window::Texture::from_path(&mut window....
The Error can be reproduced by running the following main file in an environment with a dummy.png file
extern crate piston_window;
extern crate piston;
extern crate gfx;
use piston_window::{
PistonWindow,
WindowSettings,
Flip,
TextureSettings,
Texture,
};
use std::path::Path;
fn main(){
let opengl_version = piston_window::OpenGL::V3_2;
let mut window: PistonWindow = WindowSettings::new("test", [1280., 720.]).exit_on_esc(true).graphics_api(opengl_version).build().unwrap();
let working = piston_window::Texture::from_path(&mut window.create_texture_context(), Path::new("dummy.png"), Flip::None, &TextureSettings::new()).unwrap();
let failing:Texture<gfx::Resources> = piston_window::Texture::from_path(&mut window.create_texture_context(), Path::new("dummy.png"), Flip::None, &TextureSettings::new()).unwrap();
}
The actual type is Texture<gfx_device_gl::Resources>
, not Texture<gfx::Resources>
.
Note that gfx::Resources
is a trait, not a type. In Texture<R>
the R
generic type must not be covariant, and even if it were the type would likely need to be Texture<dyn gfx::Resources>
. While it's true that gfx_device_gl::Resources
implements gfx::Resources
, that doesn't on its own imply that Texture<gfx_device_gl::Resources>
and Texture<dyn gfx::Resources>
are compatible types. (The R
type parameter would also need to be covariant.)
If you want to ensure that failing
is a Texture
without giving a particular resource type, you could use the annotation Texture<_>
instead, which permits the compiler to infer Texture
's generic type argument.
Further reading: