gpt4 book ai didi

enums - 比较两个枚举的变体及其值时如何避免代码重复?

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

我需要比较两个 serde_json::Value小号:

enum Value {
Null,
Bool(bool),
Number(Number),
String(String),
Array(Vec<Value>),
Object(Map<String, Value>),
}

比较将返回另一个枚举:

enum Diff {
Equal,
Different,
NotFound,
}

我开始编写结构如下的代码:

fn compare(val1: &Value, val2: &Value) {
let cmp = match val1 {
Value::Null => {
if let Value::Null = val2 {
Diff::Equal
} else {
Diff::NotFound
}
}
Value::Bool(b1) => {
if let Value::Bool(b2) = val2 {
if b1 == b2 {
Diff::Equal
} else {
Diff::Different
}
} else {
Diff::NotFound
}
}
Value::Number(ref n1) => {
if let Value::Number(ref n2) = val2 {
if n1 == n2 {
Diff::Equal
} else {
Diff::Different
}
} else {
Diff::NotFound
}
}
Value::String(ref s1) => {
if let Value::String(ref s2) = val2 {
if s1 == s2 {
Diff::Equal
} else {
Diff::Different
}
} else {
Diff::NotFound
}
}
_ => {
// etc...
Diff::NotFound
}
};

}

我想避免很多代码重复。

  1. 有没有更好的方法来比较一般的枚举?
  2. 如何避免这种类型的重复(例如,在 C++ 中,您可以使用宏)

最佳答案

您可以同时匹配 2 个值:

let cmp = match (val1, val2) {
(Value::Null, Value::Null) => Diff::Equal,
_ => {
// etc...
Diff::NotFound
}
};

您还可以实现一个转换函数或特征并使用它:

impl Diff {
fn new<T: PartialEq>(lhs: T, rhs: T) -> Diff {
if lhs == rhs { Diff::Equal } else { Diff::Different }
}
}

fn compare(val1: &Value, val2: &Value) {
let cmp = match (val1, val2) {
(Value::Null, Value::Null) => Diff::Equal,
(Value::Bool(b1), Value::Bool(b2)) => Diff::new(b1, b2),
_ => Diff::NotFound,
};
}

关于enums - 比较两个枚举的变体及其值时如何避免代码重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54606664/

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