aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-02-21 17:12:01 +0100
committerEdvard Thörnros <edvard.thornros@gmail.com>2021-02-21 17:12:53 +0100
commitf437bb2cc353ba0463b31c48851673b1f4a7dd28 (patch)
tree4a1c0107f8d9754e1c66a81c28db5971dfd5a1e0 /src/lib.rs
parent3715433024e2df742a6ad16488ab2a580e397b86 (diff)
downloadsylt-f437bb2cc353ba0463b31c48851673b1f4a7dd28.tar.gz
don't store multiple versions of same constant
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 91938f1..5908fa4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,6 +4,7 @@ use std::collections::hash_map::Entry;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::rc::Rc;
+use std::hash::{Hash, Hasher};
use owo_colors::OwoColorize;
@@ -198,6 +199,43 @@ impl Debug for Value {
}
}
+impl PartialEq<Value> for Value {
+ fn eq(&self, other: &Value) -> bool {
+ match (self, other) {
+ (Value::Float(a), Value::Float(b)) => a == b,
+ (Value::Int(a), Value::Int(b)) => a == b,
+ (Value::Bool(a), Value::Bool(b)) => a == b,
+ (Value::String(a), Value::String(b)) => a == b,
+ (Value::Tuple(a), Value::Tuple(b)) => {
+ a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| a == b)
+ }
+ (Value::Nil, Value::Nil) => true,
+ _ => false,
+ }
+ }
+}
+
+impl Eq for Value {}
+
+impl Hash for Value {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ match self {
+ Value::Float(a) => {
+ // We have to limit the values, because
+ // floats are wierd.
+ assert!(a.is_finite());
+ a.to_bits().hash(state);
+ },
+ Value::Int(a) => a.hash(state),
+ Value::Bool(a) => a.hash(state),
+ Value::String(a) => a.hash(state),
+ Value::Tuple(a) => a.hash(state),
+ Value::Nil => state.write_i8(0),
+ _ => {},
+ };
+ }
+}
+
impl Value {
fn identity(self) -> Self {
match self {