Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rust Piston can't find correct type hint

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
}

>Solution :

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading