From fd60a3c3d2ad8f793188d73b9472072409ef0360 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Tue, 12 Mar 2024 20:28:17 -0300 Subject: [PATCH] feat: Override traits for faster execution time --- model/src/cst_node.rs | 59 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/model/src/cst_node.rs b/model/src/cst_node.rs index 321f689..d66dd55 100644 --- a/model/src/cst_node.rs +++ b/model/src/cst_node.rs @@ -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 { @@ -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, @@ -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> { + fn partial_cmp(&self, other: &Self) -> Option { + 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(&self, state: &mut H) { + self.id.hash(state) + } +} + impl NonTerminal<'_> { pub fn contents(&self) -> String { self.children.iter().fold(String::from(""), |acc, node| { @@ -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, @@ -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> { + fn partial_cmp(&self, other: &Self) -> Option { + 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(&self, state: &mut H) { + self.id.hash(state) + } +} + impl Terminal<'_> { pub fn contents(&self) -> String { String::from(self.value)