“期望的类型参数”一般结构的构造函数中出错 [英] "Expected type parameter" error in the constructor of a generic struct

查看:100
本文介绍了“期望的类型参数”一般结构的构造函数中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  struct TextureFactory< R>其中R:gfx :: Resources {
block_textures:Vec< Rc< Texture< R>> ;,
}

impl< R> TextureFactory< R>其中R:gfx :: Resources {
fn new(window:PistonWindow) - >自{{b $ b 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,
}
}
}

这不会编译:
$ b

  src / main.rs:37:9:39:10错误:不匹配的类型:
expected'TextureFactory< R>,
found'TextureFactory< gfx_device_gl :: Resources>`
(期望的类型参数,
找到enum`gfx_device_gl :: Resources`)

gfx_device_gl ::资源 执行<$ c $虽然(我认为这只是设备特定的实现),但我并不在意它是什么类型,但我需要知道,以便我可以存储它在结构中。



我做了一个 Github上的可编译回购



(我怀疑 Rust泛型/特征:expected'Foo'B'',找到'Foo< Foo2>'> 是同一个问题,但我不知道如何将它应用于我的问题。)

解决方案

以下是您的错误再现:

  struct Foo< T> {
val:T,
}

impl< T> FOO< T> {
fn new() - >自{{b $ b Foo {val:true}
}
}

fn main(){

}

问题在于你试图对编译器说谎。此代码:

  impl< T> FOO< T> {
fn new() - >自{{
}

说:对于任何 T 调用者选择,我将用该类型创建 Foo 。然后你的实际实现选择一个具体类型 - 在这个例子中,一个 bool 。不能保证 T bool 。请注意,你的 new 函数甚至不接受任何类型为 T 的参数,这非常值得怀疑,因为这就是调用者在99%的时间内选择具体类型。



正确的说法是 $ b

  impl Foo< bool> {
fn new() - > Self {
Foo {val:true}
}
}

尽管您可能想要选择比 new 更具体的名称,因为它看起来好像是在试图使您的结构变得通用。大概会有其他类型的构造函数。



为了确切的代码,您可能需要像

  impl TextureFactory< gfx_device_gl :: Resources> {...} 

当然,另一个可能的解决方案是从您的结构。如果你只是使用 gfx_device_gl :: Resources 来构建它,那么没有理由使它成为通用的。


I am trying to store piston textures in a struct.

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,
        }
    }
}

This does not compile:

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 though (I think it's just the device specific implementation.) I don't actually care what type this is, but I need to know so that I can store it in the struct.

I made a compilable repo on Github.

(I suspect Rust generics/traits: "expected 'Foo<B>', found 'Foo<Foo2>'" is the same question, but I can't figure out how to apply it to my problem.)

解决方案

Here's a reproduction of your error:

struct Foo<T> {
    val: T,
}

impl<T> Foo<T> {
    fn new() -> Self {
        Foo { val: true }
    }
}

fn main() {

}

The problem arises because you tried to lie to the compiler. This code:

impl<T> Foo<T> {
    fn new() -> Self {}
}

Says "For whatever T the caller chooses, I will create a Foo with that type". Then your actual implementation picks a concrete type — in the example, a bool. There's no guarantee that T is a bool. Note that your new function doesn't even accept any parameter of type T, which is highly suspect as that's how the caller picks the concrete type 99% of the time.

The correct way of saying this would be

impl Foo<bool> {
    fn new() -> Self {
        Foo { val: true }
    }
}

Although you probably want to pick a more specific name than new, as it looks as if you are trying to make your struct generic. Presumably there would be other constructors with different types.

For your exact code, you probably want something like

impl TextureFactory<gfx_device_gl::Resources> { ... }

Of course, another possible solution would be to remove the generic type parameter from your struct. If you only ever construct it with a gfx_device_gl::Resources, then there's no reason to make it generic.

这篇关于“期望的类型参数”一般结构的构造函数中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆