Skip to content

Commit

Permalink
feat: Override traits for faster execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Mar 12, 2024
1 parent 9151173 commit fd60a3c
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions model/src/cst_node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::hash::Hash;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Point {
Expand Down Expand Up @@ -35,7 +36,7 @@ impl CSTNode<'_> {
}
}

#[derive(Debug, Default, PartialEq, Clone, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Default, Clone)]
pub struct NonTerminal<'a> {
pub id: uuid::Uuid,
pub kind: &'a str,
Expand All @@ -45,6 +46,32 @@ pub struct NonTerminal<'a> {
pub are_children_unordered: bool,
}

impl<'a> PartialEq for NonTerminal<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl<'a> Eq for NonTerminal<'a> {}

impl<'a> PartialOrd for NonTerminal<'a> {

Check failure on line 57 in model/src/cst_node.rs

View workflow job for this annotation

GitHub Actions / clippy

non-canonical implementation of `partial_cmp` on an `Ord` type
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.id.partial_cmp(&other.id)
}
}

impl<'a> Ord for NonTerminal<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}

impl <'a> Hash for NonTerminal<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl NonTerminal<'_> {
pub fn contents(&self) -> String {
self.children.iter().fold(String::from(""), |acc, node| {
Expand All @@ -64,7 +91,7 @@ impl<'a> TryFrom<&'a CSTNode<'a>> for &'a NonTerminal<'a> {
}
}

#[derive(Debug, Default, PartialEq, Clone, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Default, Clone)]
pub struct Terminal<'a> {
pub id: uuid::Uuid,
pub kind: &'a str,
Expand All @@ -74,6 +101,32 @@ pub struct Terminal<'a> {
pub is_block_end_delimiter: bool,
}

impl<'a> PartialEq for Terminal<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl<'a> Eq for Terminal<'a> {}

impl<'a> PartialOrd for Terminal<'a> {

Check failure on line 112 in model/src/cst_node.rs

View workflow job for this annotation

GitHub Actions / clippy

non-canonical implementation of `partial_cmp` on an `Ord` type
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.id.partial_cmp(&other.id)
}
}

impl<'a> Ord for Terminal<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}

impl <'a> Hash for Terminal<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Terminal<'_> {
pub fn contents(&self) -> String {
String::from(self.value)
Expand Down

0 comments on commit fd60a3c

Please sign in to comment.