gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:24:48 24 4
gpt4 key购买 nike

我是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>),
}

这按照我的意愿工作,现在我想要一个自身的矢量(下一步是 map ),所以我添加了:

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![] }),
}
}

最佳答案

I want the same for a vector, which I assume would be like:

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> ,所以这不会立即起作用。

但是,我不明白为什么 Cow反正这里是有必要的。 Cow对借用类型和拥有类型进行抽象,但您拥有的值始终为空,那么在这两种情况下返回借用的切片有什么害处?它看起来像这样:

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

关于vector - 如何实现包含自身向量的 Cow 的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52344295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com