diff options
Diffstat (limited to 'oryxc/src/unistr.rs')
| -rw-r--r-- | oryxc/src/unistr.rs | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/oryxc/src/unistr.rs b/oryxc/src/unistr.rs index 5dc8160..1d8463d 100644 --- a/oryxc/src/unistr.rs +++ b/oryxc/src/unistr.rs @@ -1,13 +1,20 @@ -use std::hash::{Hash, Hasher}; +use std::hash::{ + Hash, + Hasher, +}; -use unicode_normalization::{self, IsNormalized, UnicodeNormalization}; +use unicode_normalization::{ + self, + IsNormalized, + UnicodeNormalization, +}; #[repr(transparent)] #[derive(Copy, Clone, Debug, Eq)] pub struct UniStr<'a>(pub &'a str); impl Hash for UniStr<'_> { - fn hash<H: Hasher>(&self, state: &mut H) { + fn hash<H: Hasher>(&self, state: &mut H) { /* In the ASCII common case we use .bytes() to avoid decoding * every codepoint (a no-op in ASCII) */ if self.0.is_ascii() { @@ -19,7 +26,7 @@ impl Hash for UniStr<'_> { } else { self.0.nfkd().for_each(|c| c.hash(state)); } - } + } } impl PartialEq for UniStr<'_> { @@ -52,33 +59,37 @@ impl PartialEq for UniStr<'_> { #[cfg(test)] mod tests { - use std::hash::{DefaultHasher, Hash, Hasher}; + use std::hash::{ + DefaultHasher, + Hash, + Hasher, + }; - use super::UniStr; + use super::UniStr; - #[test] - fn test_unistr_eq() { - assert_eq!(UniStr("fishi"), UniStr("fishᵢ")); - assert_eq!(UniStr("fishi"), UniStr("fishi")); - assert_eq!(UniStr("fishi"), UniStr("fishᵢ")); - assert_eq!(UniStr("fishᵢ"), UniStr("fishᵢ")); - assert_eq!(UniStr("corné"), UniStr("corné")); - } + #[test] + fn test_unistr_eq() { + assert_eq!(UniStr("fishi"), UniStr("fishᵢ")); + assert_eq!(UniStr("fishi"), UniStr("fishi")); + assert_eq!(UniStr("fishi"), UniStr("fishᵢ")); + assert_eq!(UniStr("fishᵢ"), UniStr("fishᵢ")); + assert_eq!(UniStr("corné"), UniStr("corné")); + } - #[test] - fn test_unistr_hash() { - for (lhs, rhs) in &[ - (UniStr("fishi"), UniStr("fishᵢ")), - (UniStr("fishi"), UniStr("fishi")), - (UniStr("fishi"), UniStr("fishᵢ")), - (UniStr("fishᵢ"), UniStr("fishᵢ")), - (UniStr("corné"), UniStr("corné")), - ] { - let mut hashl = DefaultHasher::new(); - let mut hashr = DefaultHasher::new(); - lhs.hash(&mut hashl); - rhs.hash(&mut hashr); - assert_eq!(hashl.finish(), hashr.finish()); - } - } + #[test] + fn test_unistr_hash() { + for (lhs, rhs) in &[ + (UniStr("fishi"), UniStr("fishᵢ")), + (UniStr("fishi"), UniStr("fishi")), + (UniStr("fishi"), UniStr("fishᵢ")), + (UniStr("fishᵢ"), UniStr("fishᵢ")), + (UniStr("corné"), UniStr("corné")), + ] { + let mut hashl = DefaultHasher::new(); + let mut hashr = DefaultHasher::new(); + lhs.hash(&mut hashl); + rhs.hash(&mut hashr); + assert_eq!(hashl.finish(), hashr.finish()); + } + } } |