Rust Piston 找不到正确的类型提示
Rust Piston can't find correct type hint
我是 Rust 新手,来自另一种编程语言。
这是我的依赖项
[dependencies]
piston_window = "0.123.0"
piston = "0.53.1"
gfx = "0.18.2"
我在使用手动类型提示时遇到问题
当我在不添加类型提示的情况下加载纹理时,纹理已加载并可以显示在 window。
let dirt = piston_window::Texture::from_path(&mut window.create_texture_context(),
Path::new("assets/sprites/dirt.png"), Flip::None, &TextureSettings::new()).unwrap();
当我尝试手动添加 vscode 推荐的类型提示时,出现错误。
例如
let dirt: Texture<Resources>= piston_window::Texture::from_path(&mut window....
在具有 dummy.png 文件
的环境中,运行 以下主文件可以重现该错误
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();
}
实际类型是Texture<gfx_device_gl::Resources>
,不是Texture<gfx::Resources>
。
请注意 gfx::Resources
是特征,而不是类型。在 Texture<R>
中,R
泛型类型不能是协变的,即使它是协变的类型也可能需要是 Texture<dyn gfx::Resources>
。虽然 gfx_device_gl::Resources
确实实现了 gfx::Resources
,但这并不意味着 Texture<gfx_device_gl::Resources>
和 Texture<dyn gfx::Resources>
是兼容的类型。 (R
类型参数也需要是协变的。)
如果你想确保 failing
是一个 Texture
而不给出特定的资源类型,你可以使用注释 Texture<_>
来代替,它允许编译器推断 Texture
的泛型类型参数。
进一步阅读:
我是 Rust 新手,来自另一种编程语言。
这是我的依赖项
[dependencies]
piston_window = "0.123.0"
piston = "0.53.1"
gfx = "0.18.2"
我在使用手动类型提示时遇到问题
当我在不添加类型提示的情况下加载纹理时,纹理已加载并可以显示在 window。
let dirt = piston_window::Texture::from_path(&mut window.create_texture_context(),
Path::new("assets/sprites/dirt.png"), Flip::None, &TextureSettings::new()).unwrap();
当我尝试手动添加 vscode 推荐的类型提示时,出现错误。 例如
let dirt: Texture<Resources>= piston_window::Texture::from_path(&mut window....
在具有 dummy.png 文件
的环境中,运行 以下主文件可以重现该错误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();
}
实际类型是Texture<gfx_device_gl::Resources>
,不是Texture<gfx::Resources>
。
请注意 gfx::Resources
是特征,而不是类型。在 Texture<R>
中,R
泛型类型不能是协变的,即使它是协变的类型也可能需要是 Texture<dyn gfx::Resources>
。虽然 gfx_device_gl::Resources
确实实现了 gfx::Resources
,但这并不意味着 Texture<gfx_device_gl::Resources>
和 Texture<dyn gfx::Resources>
是兼容的类型。 (R
类型参数也需要是协变的。)
如果你想确保 failing
是一个 Texture
而不给出特定的资源类型,你可以使用注释 Texture<_>
来代替,它允许编译器推断 Texture
的泛型类型参数。
进一步阅读: