From: Ian Jackson Date: Fri, 12 Mar 2021 00:40:03 +0000 (+0000) Subject: impl Hash for ZCoord X-Git-Tag: otter-0.4.0~139 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=48045ad794581895949202cd2c2d99733952e3e6;p=otter.git impl Hash for ZCoord Signed-off-by: Ian Jackson --- diff --git a/base/zcoord.rs b/base/zcoord.rs index b9133372..1c5f34ee 100644 --- a/base/zcoord.rs +++ b/base/zcoord.rs @@ -70,6 +70,7 @@ use std::cmp::{max, Ordering}; use std::convert::{TryFrom, TryInto}; use std::fmt::{self, Debug, Display, Formatter}; +use std::hash::{Hash, Hasher}; use std::iter; use std::num::{TryFromIntError, Wrapping}; use std::str; @@ -766,6 +767,12 @@ mod innards { } } + impl Hash for ZCoord { + fn hash(&self, state: &mut H) { + self.tail().hash(state) + } + } + } //---------- tests ---------- @@ -774,6 +781,7 @@ mod innards { mod test { use crate::misc::default; use super::*; + use std::collections::hash_map::DefaultHasher; use std::mem; fn bf(s: &str) -> ZCoord { @@ -845,6 +853,13 @@ mod test { let got = self.addsub(&aso).unwrap(); assert_eq!(got.to_string(), exp); assert_eq!(got.cmp(&before), exp_ord); + + fn h(z: &ZCoord) -> u64 { + let mut h = DefaultHasher::new(); + z.hash(&mut h); + h.finish() + } + assert_ne!(h(&got), h(&before)); self } fn tinc(self, e: &str) -> Self { self.tincdec(e, Increment, Greater) }