Imagine I defined a type like this1:
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)
}
}
这些 impls 中的哪一个将用于实现ToArray
?两者都适用,但只能实现ToArray
一次为PolymorphicMatrix
, 因为ToArray
没有类型参数。这就是错误的含义:它无效,因为它会在这种情况下导致问题。
既然你都控制不了Into
nor 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()
}
}
当然,您不能强制执行之间的任何类型的相关性S
and Output
。此外,该类型参数可能会导致一些歧义:由于它没有在特征中使用,编译器可能无法推断S
在某些情况下,您可能需要明确指定它。如果这成为一个问题,您可能想探索使用generic-array https://crates.io/crates/generic-array。它可以让您将数组维度提升为类型参数,这样您就可以摆脱关联的类型,而是直接在返回类型中使用类型参数to_array
,这将有助于编译器的推理。
1 Normally, one would implement From
rather than Into
. I'm using Into
here to stay closer to the problem as stated.