From 13e67ea13670cefb46c728bed08027c0364130b2 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Thu, 5 Sep 2024 15:46:21 +0000 Subject: [PATCH] hashset vs item --- src/db.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/db.rs b/src/db.rs index baa72a4..6f5f14a 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,13 +1,12 @@ use std::collections::HashSet; -use std::hash::{Hash}; +use std::hash::{Hash, Hasher}; use std::fs; use std::io::{self, Write, BufRead}; use std::fmt; +use std::cmp::PartialEq; -// TODO: use this structure in HashSet of items -#[derive(PartialEq, Eq, Hash)] pub struct Item { pub id: String, pub content: String @@ -20,6 +19,20 @@ impl Item { } } +impl PartialEq for Item { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Eq for Item {} + +impl Hash for Item { + fn hash(&self, state: &mut H) { + self.id.hash(state); + } +} + impl fmt::Display for Item { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "{}", self.id)?;