Skip to content

Commit 4d9987f

Browse files
committed
Refactor Value's PartialEq into single match expression
1 parent a1ef7ee commit 4d9987f

File tree

1 file changed

+26
-107
lines changed

1 file changed

+26
-107
lines changed

src/value.rs

Lines changed: 26 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::environment::Environment;
22
use crate::ifn::IFn;
3+
use crate::keyword::Keyword;
34
use crate::lambda;
45
use crate::maps::MapEntry;
56
use crate::persistent_list::PersistentList::Cons;
67
use crate::persistent_list::{PersistentList, ToPersistentList, ToPersistentListIter};
78
use crate::persistent_list_map::{PersistentListMap, ToPersistentListMapIter};
89
use crate::persistent_vector::PersistentVector;
910
use crate::symbol::Symbol;
10-
use crate::keyword::Keyword;
1111
use crate::type_tag::TypeTag;
1212

1313
extern crate rand;
@@ -70,110 +70,29 @@ impl PartialEq for Value {
7070
// @TODO implement our generic IFns some other way? After all, again, this isn't Java
7171
fn eq(&self, other: &Value) -> bool {
7272
//
73-
if let I32(i) = self {
74-
if let I32(i2) = other {
75-
return i == i2;
76-
}
77-
}
78-
79-
if let F64(d) = self {
80-
if let F64(d2) = other {
81-
return d == d2;
82-
}
83-
}
84-
85-
if let Boolean(b) = self {
86-
if let Boolean(b2) = other {
87-
return b == b2;
88-
}
89-
}
90-
91-
if let Symbol(sym) = self {
92-
if let Symbol(sym2) = other {
93-
return sym == sym2;
94-
}
95-
}
96-
97-
if let Keyword(kw) = self {
98-
if let Keyword(kw2) = other {
99-
return kw == kw2;
100-
}
101-
}
102-
// Equality not defined on functions, similar to Clojure
103-
// Change this perhaps? Diverge?
104-
if let IFn(_) = self {
105-
if let IFn(_) = other {
106-
return false;
107-
}
108-
}
109-
// Is it misleading for equality to sometimes work?
110-
if let LexicalEvalFn = self {
111-
if let LexicalEvalFn = other {
112-
return true;
113-
}
114-
}
115-
116-
if let PersistentList(plist) = self {
117-
if let PersistentList(plist2) = other {
118-
return plist == plist2;
119-
}
120-
}
121-
122-
if let PersistentVector(pvector) = self {
123-
if let PersistentVector(pvector2) = other {
124-
return *pvector == *pvector2;
125-
}
73+
match (self, other) {
74+
(I32(i), I32(i2)) => i == i2,
75+
(F64(d), F64(d2)) => d == d2,
76+
(Boolean(b), Boolean(b2)) => b == b2,
77+
(Symbol(sym), Symbol(sym2)) => sym == sym2,
78+
(Keyword(kw), Keyword(kw2)) => kw == kw2,
79+
// Equality not defined on functions, similar to Clojure
80+
// Change this perhaps? Diverge?
81+
(IFn(_), IFn(_)) => false,
82+
// Is it misleading for equality to sometimes work?
83+
(LexicalEvalFn, LexicalEvalFn) => true,
84+
(PersistentList(plist), PersistentList(plist2)) => plist == plist2,
85+
(PersistentVector(pvector), PersistentVector(pvector2)) => *pvector == *pvector2,
86+
(PersistentListMap(plistmap), PersistentListMap(plistmap2)) => *plistmap == *plistmap2,
87+
(Condition(msg), Condition(msg2)) => msg == msg2,
88+
(QuoteMacro, QuoteMacro) => true,
89+
(DefmacroMacro, DefmacroMacro) => true,
90+
(DefMacro, DefMacro) => true,
91+
(LetMacro, LetMacro) => true,
92+
(String(string), String(string2)) => string == string2,
93+
(Nil, Nil) => true,
94+
_ => false,
12695
}
127-
128-
if let PersistentListMap(plistmap) = self {
129-
if let PersistentListMap(plistmap2) = other {
130-
return *plistmap == *plistmap2;
131-
}
132-
}
133-
134-
if let Condition(msg) = self {
135-
if let Condition(msg2) = other {
136-
return msg == msg2;
137-
}
138-
}
139-
140-
if let QuoteMacro = self {
141-
if let QuoteMacro = other {
142-
return true;
143-
}
144-
}
145-
146-
if let DefmacroMacro = self {
147-
if let DefmacroMacro = other {
148-
return true;
149-
}
150-
}
151-
152-
if let DefMacro = self {
153-
if let DefMacro = other {
154-
return true;
155-
}
156-
}
157-
158-
if let LetMacro = self {
159-
if let LetMacro = other {
160-
return true;
161-
}
162-
}
163-
164-
if let String(string) = self {
165-
if let String(string2) = other {
166-
return string == string2;
167-
}
168-
}
169-
170-
if let Nil = self {
171-
if let Nil = other {
172-
return true;
173-
}
174-
}
175-
176-
false
17796
}
17897
}
17998

@@ -197,7 +116,7 @@ impl Hash for Value {
197116
F64(d) => d.to_value().hash(state),
198117
Boolean(b) => b.hash(state),
199118
Symbol(sym) => sym.hash(state),
200-
Keyword(kw) => kw.hash(state),
119+
Keyword(kw) => kw.hash(state),
201120
IFn(_) => {
202121
let mut rng = rand::thread_rng();
203122
let n2: u16 = rng.gen();
@@ -235,7 +154,7 @@ impl fmt::Display for Value {
235154
F64(val) => val.to_string(),
236155
Boolean(val) => val.to_string(),
237156
Symbol(sym) => sym.to_string(),
238-
Keyword(kw) => kw.to_string(),
157+
Keyword(kw) => kw.to_string(),
239158
IFn(_) => std::string::String::from("#function[]"),
240159
LexicalEvalFn => std::string::String::from("#function[lexical-eval*]"),
241160
PersistentList(plist) => plist.to_string(),
@@ -275,7 +194,7 @@ impl Value {
275194
Value::F64(_) => TypeTag::F64,
276195
Value::Boolean(_) => TypeTag::Boolean,
277196
Value::Symbol(_) => TypeTag::Symbol,
278-
Value::Keyword(_) => TypeTag::Keyword,
197+
Value::Keyword(_) => TypeTag::Keyword,
279198
Value::IFn(_) => TypeTag::IFn,
280199
Value::LexicalEvalFn => TypeTag::IFn,
281200
Value::PersistentList(_) => TypeTag::PersistentList,

0 commit comments

Comments
 (0)