我可以有可选的特征范围吗?

时间:2018-07-18 09:14:04

标签: generics rust

给出一个为条件概率分布建模的特征:

trait Distribution {
    type T;
    fn sample<U>(&self, x: U) -> Self::T;
}

我想为两个结构ConditionalNormalMultivariateConditionalNormal实现特征,这两个结构分别对标量和向量值分布进行建模。

此类实现如下:

struct ConditionalNormal;

impl Distribution for ConditionalNormal {
    type T = f64;

    fn sample<U>(&self, x: U) -> Self::T {
        0.0
    }
}

struct MultivariateConditionalNormal;

impl Distribution for MultivariateConditionalNormal {
    type T = f64;

    fn sample<U>(&self, x: U) -> Self::T {
        0.0 + x[0]
    }
}

playground

但是,MultivariateConditionalNormal的实现无效,因为通用x[0]无法建立索引。 如果我添加特征边界std::ops::Index<usize>,则ConditionalNormal的实现无效,因为标量f64不可索引。

我听说过Sized特征通过?Sized接受可选的特征边界;我可以做类似的事情吗?有什么办法可以解决这个问题?

2 个答案:

答案 0 :(得分:3)

您可以将特征的定义更改为

trait Distribution<U> {
    type T;
    fn sample(&self, x: U) -> Self::T;
}

这使您可以在具有不同特征范围的各种类型上实现它。

impl<U> Distribution<U> for ConditionalNormal {
    // ...
}

impl<U> Distribution<U> for MultivariateConditionalNormal
where
    U: std::ops::Index<usize, Output = f64>,
{
    // ...
}

Playground

答案 1 :(得分:3)

您可以添加新的特征以指定U的功能:

trait Distribution {
    type T;
    fn sample<U>(&self, x: U) -> Self::T
    where
        U: Samplable;
}

struct ConditionalNormal;

impl Distribution for ConditionalNormal {
    type T = f64;

    fn sample<U>(&self, x: U) -> Self::T
    where
        U: Samplable,
    {
        0.0.value()
    }
}

struct MultivariateConditionalNormal;

impl Distribution for MultivariateConditionalNormal {
    type T = f64;

    fn sample<U>(&self, x: U) -> Self::T
    where
        U: Samplable,
    {
        0.0 + x.value()
    }
}

trait Samplable {
    fn value(&self) -> f64;
}

impl Samplable for f64 {
    fn value(&self) -> f64 {
        *self
    }
}

impl Samplable for Vec<f64> {
    fn value(&self) -> f64 {
        self[0]
    }
}

fn main() {}