简体   繁体   English

为什么在特化特征时出现冲突的实现错误?

[英]Why do I get a conflicting implementations error when specializing a trait?

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. 看来新的“向量” Mul专业化比默认的更为专门,而且我认为我没有依靠尚未为包装箱外部类型定义的Vectorizable特性。

#![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!();
    }
}

( playground ) 游乐场

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>`

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实现不是更具体,例如,它没有提及任何有关T * T是有效操作的内容,这是常规操作所必需的。

You need to add more bounds to the Vectorizable impl to match the general one: 您需要向Vectorizable impl添加更多界限以匹配常规界限:

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

Alternatively, you could add those bounds as the supertrait of Vectorizable : 或者,您可以将这些边界添加为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> {
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我没有实现没有实现Ord的f32的“特征冲突实现”? - Why do I get “conflicting implementations of trait” for f32 which does not implement Ord? 尝试通用时,“特征的冲突实施” - “conflicting implementations for trait” when trying to be generic Rust 中 trait 的冲突实现 - Conflicting implementations of trait in Rust 为什么在将高级特征边界与关联类型结合时会出现 Rust 编译错误? - Why do I get a Rust compilation error when combining higher-rank trait bounds with associated types? 为什么在使用 `dyn Trait` 类型别名时会出现大小错误? - Why do I get a size error when using a type alias of `dyn Trait`? 为什么即使T实现了特征,也出现错误“未对&mut T实现特征Foo”? - Why do I get the error “the trait `Foo` is not implemented for `&mut T`” even though T implements the trait? 实现Iterator + Clone的特征:冲突的实现 - Implement trait for Iterator+Clone: conflicting implementations 当使用ReadBytesExt从字节片中读取整数时,为什么会出现错误“特征边界未得到满足”? - Why do I get the error “trait bounds were not satisfied” when using ReadBytesExt to read an integer from a slice of bytes? 显然已经实现特征的类型的冲突特征实现 - Conflicting trait implementations for types that clearly already implement the trait 如何为实现另一个特征的类型实现特征而不冲突的实现 - How to implement trait for types implementing another trait without conflicting implementations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM