用返回通用特征的方法实现特征

时间:2017-02-28 01:53:18

标签: generics rust traits

我正在尝试设计一对特征(例如来自线性代数的RowVectorColumnVector),其中每个特征从其中一个方法返回另一个特征(例如transpose)。我希望将来能够添加任何特征的实现(例如密集和稀疏矢量实现)。

#[macro_use]
extern crate derive_new;

trait RowVector<Element> {
    fn transpose(self) -> ColumnVector<Element>;
}

trait ColumnVector<Element> {
    fn transpose(self) -> RowVector<Element>;
}

#[derive(new, Debug)]
struct VecRowVector<Element> {
    vec: Vec<Element>
}

#[derive(new, Debug)]
struct VecColumnVector<Element> {
    vec: Vec<Element>
}

impl<Element> RowVector<Element> for VecRowVector<Element> {
    fn transpose(self) -> VecColumnVector<Element> {
        VecColumnVector::new(self.vec)
    }
}

impl<Element> ColumnVector<Element> for VecColumnVector<Element> {
    fn transpose(self) -> VecRowVector<Element> {
        VecRowVector::new(self.vec)
    }
}

fn main() {
    let row_vector = VecRowVector::new(vec![1,2,3]);
    let col_vector = VecColumnVector::new(vec![1,2,3]);
    println!("{:?}", row_vector.transpose());
    println!("{:?}", col_vector.transpose());
}

我收到一条错误消息,指出VecColumnVector不是ColumnVector,并且期望'static值。

error[E0053]: method `transpose` has an incompatible type for trait
  --> src\main.rs:22:31
   |
4  |         fn transpose(self) -> ColumnVector<Element>;
   |                               --------------------- type in trait
...
22 |         fn transpose(self) -> VecColumnVector<Element> {
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^ expected trait ColumnVector, found struct `VecColumnVector`
   |
   = note: expected type `fn(VecRowVector<Element>) -> ColumnVector<Element> + 'static`
   = note:    found type `fn(VecRowVector<Element>) -> VecColumnVector<Element>`

我没有VecColumnVector ColumnVector的子类型吗?或者我是否需要告诉特性它不需要static一生?

2 个答案:

答案 0 :(得分:2)

您正在尝试返回特征。虽然使用trait object可以实现这一点,但这可能不是您想要做的。更好的设计是引入Transpose特征,您可以使用与Rust的内置FromInto转换特征类似的方式进行建模。

trait Transpose<To> {
    fn transpose(self) -> To;
}

impl<Element> Transpose<VecColumnVector<Element>> for VecRowVector<Element> {
    fn transpose(self) -> VecColumnVector<Element> {
        VecColumnVector::new(self.vec)
    }
}

impl<Element> Transpose<VecRowVector<Element>> for VecColumnVector<Element> {
    fn transpose(self) -> VecRowVector<Element> {
        VecRowVector::new(self.vec)
    }
}

答案 1 :(得分:1)

当需要关联两种类型时,最佳解决方案通常为associated types。这排除了使用像特征对象一样的动态调度,但在Rust中动态调度仍然非常有限。使用静态调度时,Rust更具表现力,相关类型具有杠杆作用。

pub trait RowVector<Element>: Sized {
    type Transpose: ColumnVector<Element>;

    fn transpose(self) -> Self::Transpose;
}

pub trait ColumnVector<Element>: Sized {
    type Transpose: RowVector<Element>;

    fn transpose(self) -> Self::Transpose;
}

pub struct VecRowVector<Element> {
    pub vec: Vec<Element>
}

pub struct VecColumnVector<Element> {
    pub vec: Vec<Element>
}

impl<Element> RowVector<Element> for VecRowVector<Element> {
    type Transpose = VecColumnVector<Element>;

    fn transpose(self) -> VecColumnVector<Element> {
        VecColumnVector { vec: self.vec }
    }
}

impl<E: Debug> ColumnVector<Element> for VecColumnVector<Element> {
    type Transpose = VecRowVector<Element>;

    fn transpose(self) -> VecRowVector<Element> {
        VecRowVector { vec: self.vec }
    }
}