无约束类型参数错误

Jmb*_*Jmb 3 rust glium

我正在尝试将glium与cgmath接口。按照这个答案,我实现了一个ToArray特征,将的实例转换cgmath::Matrix4为glium可以使用的格式:

pub trait ToArray {
    type Output;
    fn to_array(&self) -> Self::Output;
}

impl<S: cgmath::BaseNum> ToArray for cgmath::Matrix4<S> {
    type Output = [[S; 4]; 4];
    fn to_array(&self) -> Self::Output {
        (*self).into()
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我并不总是Matrix4直接使用,因此我需要对cgmath转换类型进行类似的实现。例如cgmath::Decomposed

impl<S: cgmath::BaseFloat, R: cgmath::Rotation3<S>> ToArray
    for cgmath::Decomposed<cgmath::Vector3<S>, R> {
    type Output = [[S; 4]; 4];
    fn to_array(&self) -> Self::Output {
        cgmath::Matrix4::<S>::from(*self).into()
    }
}
Run Code Online (Sandbox Code Playgroud)

这可行,但是我想避免为所有转换类型重复代码,所以我认为我将为可以转换为的任何东西定义一个通用实现Matrix4

impl<S: cgmath::BaseFloat, T: Into<cgmath::Matrix4<S>>> ToArray for T {
    type Output = [[S; 4]; 4];
    fn to_array(&self) -> Self::Output {
        cgmath::Matrix4::<S>::from(*self).into()
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用:

pub trait ToArray {
    type Output;
    fn to_array(&self) -> Self::Output;
}

impl<S: cgmath::BaseNum> ToArray for cgmath::Matrix4<S> {
    type Output = [[S; 4]; 4];
    fn to_array(&self) -> Self::Output {
        (*self).into()
    }
}
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  • 上面的代码为什么不起作用?通过阅读rustc --explain输出,我希望能够T: Into<cgmath::Matrix4<S>>S以及T
  • 如何为可转换为的任何内容编写通用实现Matrix4

Fra*_*gné 5

想象一下,我定义了这样的类型1

struct PolymorphicMatrix;

impl Into<cgmath::Matrix4<f32>> for PolymorphicMatrix {
    fn into(self) -> cgmath::Matrix4<f32> {
        cgmath::Matrix4::new(
            1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 1.0)
    }
}

impl Into<cgmath::Matrix4<f64>> for PolymorphicMatrix {
    fn into(self) -> cgmath::Matrix4<f64> {
        cgmath::Matrix4::new(
            2.0, 2.0, 2.0, 2.0,
            2.0, 2.0, 2.0, 2.0,
            2.0, 2.0, 2.0, 2.0,
            2.0, 2.0, 2.0, 2.0)
    }
}
Run Code Online (Sandbox Code Playgroud)

这些实现中的哪一个将用于实现ToArray?两者都适用,但是您只能为实现ToArray一次PolymorphicMatrix,因为ToArray没有类型参数。这就是错误的含义:它是无效的,因为它会在这种情况下引起问题。

由于您既不控制Into也不控制cgmath::Matrix4,因此唯一可以更改的方面是ToArray。您可以添加特征定义本身未使用的类型参数,并且实现可以使用该类型参数。

pub trait ToArray<S> {
    type Output;
    fn to_array(&self) -> Self::Output;
}

impl<S: cgmath::BaseFloat, T: Into<cgmath::Matrix4<S>>> ToArray<S> for T {
    type Output = [[S; 4]; 4];
    fn to_array(&self) -> Self::Output {
        cgmath::Matrix4::<S>::from(*self).into()
    }
}
Run Code Online (Sandbox Code Playgroud)

自然,您不能在S和之间强制任何关联Output。另外,该类型参数可能会引起一些歧义:由于在特征中未使用它,因此编译器S在某些情况下可能无法从使用情况推断出,因此您可能必须明确指定它。如果这成为问题,则可能要使用探索generic-array。它可以让您提升数组尺寸以使用类型参数,这样您就可以摆脱关联的类型,而直接在返回类型中使用类型参数to_array,这将有助于编译器的推断。


1通常,将实施From而不是Into。我在Into这里使用是为了更接近所述问题。