From c08946aa7c56513ae13c90bc5066277bbe9a7564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Mon, 15 Apr 2024 18:58:02 +0200 Subject: [PATCH] jid: skip `at` and `slash` in comparison operators They only contain cached information and thus don't need to be included in comparison and identity operators. Fixes #123. --- jid/src/lib.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/jid/src/lib.rs b/jid/src/lib.rs index e687080..eb7f6d5 100644 --- a/jid/src/lib.rs +++ b/jid/src/lib.rs @@ -32,7 +32,9 @@ use core::num::NonZeroU16; use std::borrow::{Borrow, Cow}; +use std::cmp::Ordering; use std::fmt; +use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::str::FromStr; @@ -78,13 +80,37 @@ fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result /// /// This dynamic type on the other hand can be used in contexts where it is /// not known, at compile-time, whether a JID is full or bare. -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, Eq)] pub struct Jid { normalized: String, at: Option, slash: Option, } +impl PartialEq for Jid { + fn eq(&self, other: &Jid) -> bool { + self.normalized == other.normalized + } +} + +impl PartialOrd for Jid { + fn partial_cmp(&self, other: &Jid) -> Option { + self.normalized.partial_cmp(&other.normalized) + } +} + +impl Ord for Jid { + fn cmp(&self, other: &Jid) -> Ordering { + self.normalized.cmp(&other.normalized) + } +} + +impl Hash for Jid { + fn hash(&self, state: &mut H) { + self.normalized.hash(state) + } +} + impl FromStr for Jid { type Err = Error;