2017-08-25 32 views
0

我不確定我明白爲什麼這段代碼不能編譯。看起來像新的「矢量」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>` 

回答

2

Vectorizable實現更具體的,比如它沒有提及任何T * T是一個有效的操作,由一般的需要。

你需要更多的邊界添加到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, 
{ 

或者,你可以添加這些邊界爲Vectorizable的supertrait:

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> { 
    // ... 
} 
+0

那現在很明顯它已被指出! – dpzmick