From 2284c00417260c3aebb9f022629ded9a64a9ca7e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 16 Aug 2024 20:14:34 +0300 Subject: [PATCH] feat(tazjin/german-string): PartialEq implementation for GermanString This is where one of the advantages of this string representation starts to shine: For small strings there's no derefencing any heap memory at all, and for the long representation we can compare the prefix before moving on. Change-Id: Iac333a52e8d7c9dd09e33dbcf51754e321c880e6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12238 Reviewed-by: tazjin Autosubmit: tazjin Tested-by: BuildkiteCI --- users/tazjin/german-string/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/users/tazjin/german-string/src/lib.rs b/users/tazjin/german-string/src/lib.rs index eb6d02f29..cf315c9d5 100644 --- a/users/tazjin/german-string/src/lib.rs +++ b/users/tazjin/german-string/src/lib.rs @@ -100,6 +100,22 @@ impl Drop for GermanString { } } +impl PartialEq for GermanString { + fn eq(&self, other: &GermanString) -> bool { + if self.len() != other.len() { + return false; + } + + unsafe { + if self.len() <= 12 { + return self.0.small.data[..self.len()] == other.0.small.data[..other.len()]; + } + return self.0.large.prefix == other.0.large.prefix + && self.as_bytes() == other.as_bytes(); + } + } +} + #[cfg(test)] mod tests { use super::*;