如何实现包含其自身向量的Cow的枚举?

时间:2018-09-15 11:32:45

标签: vector enums rust

我是trying to implement a flexible type system at runtime in Rust。到目前为止,这是我所能使用的:

use std::borrow::Cow;

pub struct Float {
    pub min: f64,
    pub max: f64,
    pub value: f64,
}

pub struct Text<'a> {
    pub value: Cow<'a, str>
}

pub enum Value<'a> {
    None,
    Float(Float),
    Text(Text<'a>),
}

这可以按我想要的方式工作,现在我需要一个向量(下一步就是地图),所以我添加了:

pub struct Vector<'a> {
    pub value: Cow<'a, Vec<Value<'a>>>,
}

并将枚举扩展为:

pub enum Value<'a> {
    None,
    Float(Float),
    Text(Text<'a>),
    Vector(Vector<'a>),
}

现在我收到一条错误消息:

error[E0277]: the trait bound `Value<'a>: std::clone::Clone` is not satisfied
  --> src/lib.rs:14:5
   |
14 |     pub value: Cow<'a, Vec<Value<'a>>>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `Value<'a>`
   |
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::vec::Vec<Value<'a>>`
   = note: required because of the requirements on the impl of `std::borrow::ToOwned` for `std::vec::Vec<Value<'a>>`
   = note: required by `std::borrow::Cow`

error[E0277]: the trait bound `Value<'a>: std::clone::Clone` is not satisfied
  --> src/lib.rs:21:12
   |
21 |     Vector(Vector<'a>),
   |            ^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `Value<'a>`
   |
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::vec::Vec<Value<'a>>`
   = note: required because of the requirements on the impl of `std::borrow::ToOwned` for `std::vec::Vec<Value<'a>>`
   = note: required because it appears within the type `Vector<'a>`
   = note: no field of an enum variant may have a dynamically sized type

我尝试了几种实现Clone的方法,但是作为一个初学者,我最终遇到了无数其他错误消息。如何将Value的向量导入该系统?

为什么要这样做?

我有以下代码来简化Value的使用:

impl<'a> Value<'a> {
    pub fn float_val(&self) -> f64 {
        match self {
            Value::None => 0.0,
            Value::Float(f) => f.value,
            Value::Text(t) => t.value.parse().unwrap_or(0.0),
        }
    }

    pub fn str_val(&'a self) -> Cow<'a, str> {
        match self {
            Value::None => Cow::Owned("".to_string()),
            Value::Float(f) => Cow::Owned(f.value.to_string()),
            Value::Text(t) => Cow::Borrowed(&t.value),
        }
    }
}

这使我可以在以下功能中使用它:

fn append_string(s1: &Value, s2: &Value) {
    Value::Text(Text {
        value: format!("{}{}", s1.str_val(), s2.str_val()),
    })
}

我想要一个向量,我想这应该是这样的:

pub fn vec(&'a self) -> Vec<Value> {
    match self {
        Value::Vector(v) => v.value,
        _ => Cow::Owned(Vector { value: vec![] }),
    }
}

1 个答案:

答案 0 :(得分:3)

  

我想要一个向量,我想这应该是这样的:

pub fn vec(&'a self) -> Vec<Value> {
match self {
        Value::Vector(v) => v.value,
        _ => Cow::Owned(Vector { value: vec![] }),
    }
}

首先,从函数返回Cow并不意味着您必须将数据存储为Cow。您可以这样做:

pub struct Vector<'a> {
    pub value: Vec<Value<'a>>,
}

pub enum Value<'a> {
    None,
    Float(Float),
    Text(Text<'a>),
    Vector(Vector<'a>),
}

然后您的to_vec 看起来像这样:

impl<'a> Value<'a> {
    pub fn to_vec(&'a self) -> Cow<'a, [Value<'a>]> {
        match self {
            Value::Vector(v) => Cow::Borrowed(&v.value),
            _ => Cow::Owned(Vec::new()),
        }
    }
}

除了对于ToOwned实施Value<'a>仍然会有一些问题,因此这将不会立即起作用。

但是,我仍然不明白为什么这里需要CowCow对借用vs拥有类型进行抽象,但是您拥有的值始终为空,那么在两种情况下返回借用切片的危害是什么?看起来像这样:

impl<'a> Value<'a> {
    pub fn to_vec_slice(&'a self) -> &'a [Value<'a>] {
        match self {
            Value::Vector(v) => &v.value,
            _ => &[],
        }
    }
}