简体   繁体   English

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

[英]Implementing trait with method returning generic trait

I am trying to design a pair of traits (eg RowVector and ColumnVector from linear algebra) where each trait returns the other from one of its methods (eg transpose ). 我试图设计一对特征(例如来自线性代数的RowVectorColumnVector ),其中每个特征从其中一个方法返回另一个特征(例如transpose )。 I want to be able to add implementations of either trait in the future (such as dense and sparse vector implementations). 我希望将来能够添加任何特征的实现(例如密集和稀疏矢量实现)。

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

I get an error saying that VecColumnVector is not a ColumnVector and it's expecting a 'static value. 我得到一个错误,说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>`

Have I not made VecColumnVector a subtype of ColumnVector ? 我不是做VecColumnVector的一个亚型ColumnVector Or do I somehow need to tell the trait that it doesn't need to be a static lifetime? 或者我是否需要告诉特性它不需要是static生命周期?

You are trying to return a trait. 你正试图回归一个特质。 While this is possible using a trait object , it probably isn't what you want to do. 虽然这可以使用特征对象 ,但它可能不是您想要做的。 A better design would be to introduce a Transpose trait, which you can model in a similar way to Rust's built-in From and Into conversion traits. 更好的设计是引入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)
    }
}

When two types need to be related, the best solution is often associated types . 当需要关联两种类型时,最佳解决方案通常是关联类型 This precludes using dynamic dispatch like a trait object, but dynamic dispatch is still pretty limited in Rust. 这排除了使用像特征对象一样的动态调度,但在Rust中动态调度仍然非常有限。 Rust is more expressive with static dispatch, which associated types leverage. 使用静态调度时,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 }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM