Skip to content

Commit

Permalink
Merge pull request #6 from sile/impl-hash
Browse files Browse the repository at this point in the history
Implement `Eq` and `Hash` for terms
  • Loading branch information
sile committed Apr 2, 2022
2 parents e466fce + 0d212df commit e01e1ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ coveralls = {repository = "sile/eetf"}
num = "0.4"
byteorder = "1"
libflate = "1"
ordered-float = "2"
thiserror = "1"
43 changes: 27 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use crate::codec::EncodeError;
pub use crate::codec::EncodeResult;

/// Term.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Term {
Atom(Atom),
FixInteger(FixInteger),
Expand Down Expand Up @@ -176,7 +176,7 @@ impl From<Map> for Term {
}

/// Atom.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Atom {
/// The name of the atom.
pub name: String,
Expand Down Expand Up @@ -204,7 +204,7 @@ impl From<String> for Atom {
}

/// Fixed width integer.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct FixInteger {
/// The value of the integer
pub value: i32,
Expand Down Expand Up @@ -249,7 +249,7 @@ impl From<i32> for FixInteger {
}

/// Multiple precision integer.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct BigInteger {
/// The value of the integer
pub value: BigInt,
Expand Down Expand Up @@ -338,7 +338,7 @@ impl<'a> From<&'a FixInteger> for BigInteger {
}

/// Floating point number
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Clone)]
pub struct Float {
/// The value of the number
pub value: f64,
Expand Down Expand Up @@ -372,9 +372,20 @@ impl TryFrom<f64> for Float {
}
}
}
impl PartialEq for Float {
fn eq(&self, other: &Self) -> bool {
ordered_float::OrderedFloat(self.value) == ordered_float::OrderedFloat(other.value)
}
}
impl Eq for Float {}
impl std::hash::Hash for Float {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
ordered_float::OrderedFloat(self.value).hash(state);
}
}

/// Process Identifier.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Pid {
pub node: Atom,
pub id: u32,
Expand Down Expand Up @@ -412,7 +423,7 @@ impl<'a> From<(&'a str, u32, u32)> for Pid {
}

/// Port.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Port {
pub node: Atom,
pub id: u32,
Expand All @@ -434,7 +445,7 @@ impl<'a> From<(&'a str, u32)> for Port {
}

/// Reference.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Reference {
pub node: Atom,
pub id: Vec<u32>,
Expand Down Expand Up @@ -469,7 +480,7 @@ impl<'a> From<(&'a str, Vec<u32>)> for Reference {
}

/// External Function.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct ExternalFun {
pub module: Atom,
pub function: Atom,
Expand All @@ -491,7 +502,7 @@ impl<'a, 'b> From<(&'a str, &'b str, u8)> for ExternalFun {
}

/// Internal Function.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum InternalFun {
/// Old representation.
Old {
Expand Down Expand Up @@ -537,7 +548,7 @@ impl fmt::Display for InternalFun {
}

/// Binary.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Binary {
pub bytes: Vec<u8>,
}
Expand Down Expand Up @@ -568,7 +579,7 @@ impl From<Vec<u8>> for Binary {
}

/// Bit string.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct BitBinary {
pub bytes: Vec<u8>,
pub tail_bits_size: u8,
Expand Down Expand Up @@ -611,7 +622,7 @@ impl From<(Vec<u8>, u8)> for BitBinary {
}

/// List.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct List {
pub elements: Vec<Term>,
}
Expand Down Expand Up @@ -648,7 +659,7 @@ impl From<Vec<Term>> for List {
}

/// Improper list.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct ImproperList {
pub elements: Vec<Term>,
pub last: Box<Term>,
Expand Down Expand Up @@ -677,7 +688,7 @@ impl From<(Vec<Term>, Term)> for ImproperList {
}

/// Tuple.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Tuple {
pub elements: Vec<Term>,
}
Expand Down Expand Up @@ -708,7 +719,7 @@ impl From<Vec<Term>> for Tuple {
}

/// Map.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Map {
pub entries: Vec<(Term, Term)>,
}
Expand Down

0 comments on commit e01e1ee

Please sign in to comment.