如何实现一个包含自身向量的牛的枚举?

问题描述 投票:0回答:1

我是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![] }),
    }
}
vector enums rust
1个回答
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,
            _ => &[],
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.