为什么在专门化特征时会出现冲突的实现错误? [英] Why do I get a conflicting implementations error when specializing a trait?
问题描述
我不确定我是否理解为什么这段代码不能编译.似乎新的向量"Mul
专业化比默认的更具体,而且我不认为我依赖于 Vectorizable
特征尚未为我的箱子外部的类型定义.
I'm not sure that I understand why this code won't compile. It seems like the new "vector" Mul
specialization is more specific than the default one, and I don't think that I'm relying on the Vectorizable
trait not having been defined for a type external to my crate.
#![feature(cfg_target_feature)]
#![feature(specialization)]
use std::marker::PhantomData;
use std::ops::{Mul, Add};
type Dimension = (usize, usize);
type Coordinate = (usize, usize);
pub trait Ordering {
// omitted
}
pub struct RowMajor {}
impl Ordering for RowMajor {}
pub struct ColumnMajor {}
impl Ordering for ColumnMajor {}
// NxM matrix
pub struct Matrix<T, O: Ordering> {
dim: Dimension,
values: Vec<T>,
// needed so that we can add type bound to struct
ordering: PhantomData<O>,
}
trait VectorSize {}
struct V4x {}
impl VectorSize for V4x {}
// others defined for other sizes
trait Vectorizable {
type SimdType; /*: simd::Simd */
type VectorSize: VectorSize;
}
#[cfg(target_feature = "sse")]
impl Vectorizable for f32 {
type SimdType = f32; /* simd::f32x4 */
type VectorSize = V4x;
}
impl<'a, 'b, T1, T2, O1: Ordering, O2: Ordering>
Mul<&'b Matrix<T2, O2>> for &'a Matrix<T1, O1>
where
T1: Mul<T2> + Clone,
T2: Clone,
<T1 as Mul<T2>>::Output: Add<Output = <T1 as Mul<T2>>::Output> + Clone + Default,
{
// always output row major because we compute in row major order
type Output = Matrix<
<T1 as Mul<T2>>::Output
, RowMajor>;
// self is a &'a
default fn mul(self, rhs: &'b Matrix<T2, O2>) -> Self::Output
{
unimplemented!();
}
}
impl<'a, 'b, T: Vectorizable> Mul<&'b Matrix<T, ColumnMajor>> for &'a Matrix<T, RowMajor> {
fn mul(self, rhs: &'b Matrix<T, ColumnMajor>) -> Self::Output {
unimplemented!();
}
}
(游乐场)
error[E0119]: conflicting implementations of trait `std::ops::Mul<&Matrix<_, ColumnMajor>>` for type `&Matrix<_, RowMajor>`:
--> src/main.rs:65:1
|
46 | / impl<'a, 'b, T1, T2, O1: Ordering, O2: Ordering>
47 | | Mul<&'b Matrix<T2, O2>> for &'a Matrix<T1, O1>
48 | | where
49 | | T1: Mul<T2> + Clone,
... |
62 | | }
63 | | }
| |_- first implementation here
64 |
65 | / impl<'a, 'b, T: Vectorizable> Mul<&'b Matrix<T, ColumnMajor>> for &'a Matrix<T, RowMajor> {
66 | | fn mul(self, rhs: &'b Matrix<T, ColumnMajor>) -> Self::Output {
67 | | unimplemented!();
68 | | }
69 | | }
| |_^ conflicting implementation for `&Matrix<_, RowMajor>`
推荐答案
Vectorizable
实现不是更具体,例如它没有提到任何关于 T * T
为有效操作,一般要求.
The Vectorizable
implementation is not more specific, for instance it does not mention anything about T * T
being a valid operation, required by the general one.
您需要向 Vectorizable
impl 添加更多边界以匹配一般的边界:
You need to add more bounds to the Vectorizable
impl to match the general one:
impl<'a, 'b, T> Mul<&'b Matrix<T, ColumnMajor>> for &'a Matrix<T, RowMajor>
where
T: Vectorizable + Mul + Clone,
T::Output: Add<Output = T::Output> + Clone + Default,
{
或者,您可以将这些边界添加为 Vectorizable
的超特征:
Alternatively, you could add those bounds as the supertrait of Vectorizable
:
trait Vectorizable: Mul<Output=Self> + Add<Output = Self> + Clone + Default {
// ...
}
impl<'a, 'b, T: Vectorizable> Mul<&'b Matrix<T, ColumnMajor>> for &'a Matrix<T, RowMajor> {
// ...
}
这篇关于为什么在专门化特征时会出现冲突的实现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!