X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc_data_structures%2Fstable_hasher.rs;h=a98e77cebd88aaa2249c170e5084895caf7aa4fd;hb=6fc73bc10a72d0e4475f8ab60e9902bddc03c1f6;hp=70492d499222fe89f547ede56f948f4dfd012369;hpb=617b07e7302065d8aaed18ab0baf88cfb9e8e054;p=rust.git diff --git a/src/librustc_data_structures/stable_hasher.rs b/src/librustc_data_structures/stable_hasher.rs index 70492d49922..a98e77cebd8 100644 --- a/src/librustc_data_structures/stable_hasher.rs +++ b/src/librustc_data_structures/stable_hasher.rs @@ -1,9 +1,9 @@ -use std::hash::{Hash, Hasher, BuildHasher}; -use std::mem; -use smallvec::SmallVec; use crate::sip128::SipHasher128; -use rustc_index::vec; use rustc_index::bit_set; +use rustc_index::vec; +use smallvec::SmallVec; +use std::hash::{BuildHasher, Hash, Hasher}; +use std::mem; /// When hashing something that ends up affecting properties like symbol names, /// we want these symbol names to be calculated independently of other factors @@ -27,10 +27,9 @@ pub trait StableHasherResult: Sized { } impl StableHasher { + #[inline] pub fn new() -> Self { - StableHasher { - state: SipHasher128::new_with_keys(0, 0), - } + StableHasher { state: SipHasher128::new_with_keys(0, 0) } } pub fn finish(self) -> W { @@ -177,18 +176,14 @@ pub trait ToStableHashKey { // self-contained values that don't depend on the hashing context `CTX`. #[macro_export] macro_rules! impl_stable_hash_via_hash { - ($t:ty) => ( + ($t:ty) => { impl $crate::stable_hasher::HashStable for $t { #[inline] - fn hash_stable( - &self, - _: &mut CTX, - hasher: &mut $crate::stable_hasher::StableHasher - ) { + fn hash_stable(&self, _: &mut CTX, hasher: &mut $crate::stable_hasher::StableHasher) { ::std::hash::Hash::hash(self, hasher); } } - ); + }; } impl_stable_hash_via_hash!(i8); @@ -217,18 +212,14 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { impl HashStable for f32 { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - let val: u32 = unsafe { - ::std::mem::transmute(*self) - }; + let val: u32 = unsafe { ::std::mem::transmute(*self) }; val.hash_stable(ctx, hasher); } } impl HashStable for f64 { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - let val: u64 = unsafe { - ::std::mem::transmute(*self) - }; + let val: u64 = unsafe { ::std::mem::transmute(*self) }; val.hash_stable(ctx, hasher); } } @@ -255,9 +246,10 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for (T1, T2, T3) - where T1: HashStable, - T2: HashStable, - T3: HashStable, +where + T1: HashStable, + T2: HashStable, + T3: HashStable, { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { let (ref _0, ref _1, ref _2) = *self; @@ -268,10 +260,11 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for (T1, T2, T3, T4) - where T1: HashStable, - T2: HashStable, - T3: HashStable, - T4: HashStable, +where + T1: HashStable, + T2: HashStable, + T3: HashStable, + T4: HashStable, { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { let (ref _0, ref _1, ref _2, ref _3) = *self; @@ -299,9 +292,10 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for indexmap::IndexMap - where K: HashStable + Eq + Hash, - V: HashStable, - R: BuildHasher, +where + K: HashStable + Eq + Hash, + V: HashStable, + R: BuildHasher, { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { @@ -313,8 +307,9 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for indexmap::IndexSet - where K: HashStable + Eq + Hash, - R: BuildHasher, +where + K: HashStable + Eq + Hash, + R: BuildHasher, { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { @@ -325,7 +320,10 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } } -impl HashStable for SmallVec<[A; 1]> where A: HashStable { +impl HashStable for SmallVec<[A; 1]> +where + A: HashStable, +{ #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { (&self[..]).hash_stable(ctx, hasher); @@ -361,7 +359,6 @@ fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) { } } - impl HashStable for String { #[inline] fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { @@ -384,9 +381,9 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } } - impl HashStable for Option - where T: HashStable +where + T: HashStable, { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { @@ -400,8 +397,9 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for Result - where T1: HashStable, - T2: HashStable, +where + T1: HashStable, + T2: HashStable, { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { @@ -414,7 +412,8 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl<'a, T, CTX> HashStable for &'a T - where T: HashStable + ?Sized +where + T: HashStable + ?Sized, { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { @@ -430,7 +429,8 @@ fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for ::std::ops::RangeInclusive - where T: HashStable +where + T: HashStable, { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { @@ -440,7 +440,8 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } impl HashStable for vec::IndexVec - where T: HashStable, +where + T: HashStable, { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { self.len().hash_stable(ctx, hasher); @@ -450,17 +451,13 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { } } - -impl HashStable for bit_set::BitSet -{ +impl HashStable for bit_set::BitSet { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { self.words().hash_stable(ctx, hasher); } } -impl HashStable -for bit_set::BitMatrix -{ +impl HashStable for bit_set::BitMatrix { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { self.words().hash_stable(ctx, hasher); } @@ -470,9 +467,10 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { impl_stable_hash_via_hash!(::std::path::PathBuf); impl HashStable for ::std::collections::HashMap - where K: ToStableHashKey + Eq, - V: HashStable, - R: BuildHasher, +where + K: ToStableHashKey + Eq, + V: HashStable, + R: BuildHasher, { #[inline] fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { @@ -481,38 +479,36 @@ fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { } impl HashStable for ::std::collections::HashSet - where K: ToStableHashKey + Eq, - R: BuildHasher, +where + K: ToStableHashKey + Eq, + R: BuildHasher, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - let mut keys: Vec<_> = self.iter() - .map(|k| k.to_stable_hash_key(hcx)) - .collect(); + let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect(); keys.sort_unstable(); keys.hash_stable(hcx, hasher); } } impl HashStable for ::std::collections::BTreeMap - where K: ToStableHashKey, - V: HashStable, +where + K: ToStableHashKey, + V: HashStable, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - let mut entries: Vec<_> = self.iter() - .map(|(k, v)| (k.to_stable_hash_key(hcx), v)) - .collect(); + let mut entries: Vec<_> = + self.iter().map(|(k, v)| (k.to_stable_hash_key(hcx), v)).collect(); entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2)); entries.hash_stable(hcx, hasher); } } impl HashStable for ::std::collections::BTreeSet - where K: ToStableHashKey, +where + K: ToStableHashKey, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - let mut keys: Vec<_> = self.iter() - .map(|k| k.to_stable_hash_key(hcx)) - .collect(); + let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect(); keys.sort_unstable(); keys.hash_stable(hcx, hasher); } @@ -522,21 +518,19 @@ pub fn hash_stable_hashmap( hcx: &mut HCX, hasher: &mut StableHasher, map: &::std::collections::HashMap, - to_stable_hash_key: F) - where K: Eq, - V: HashStable, - R: BuildHasher, - SK: HashStable + Ord, - F: Fn(&K, &HCX) -> SK, + to_stable_hash_key: F, +) where + K: Eq, + V: HashStable, + R: BuildHasher, + SK: HashStable + Ord, + F: Fn(&K, &HCX) -> SK, { - let mut entries: Vec<_> = map.iter() - .map(|(k, v)| (to_stable_hash_key(k, hcx), v)) - .collect(); + let mut entries: Vec<_> = map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect(); entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2)); entries.hash_stable(hcx, hasher); } - /// A vector container that makes sure that its items are hashed in a stable /// order. pub struct StableVec(Vec); @@ -556,14 +550,13 @@ fn deref(&self) -> &Vec { } impl HashStable for StableVec - where T: HashStable + ToStableHashKey +where + T: HashStable + ToStableHashKey, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { let StableVec(ref v) = *self; - let mut sorted: Vec<_> = v.iter() - .map(|x| x.to_stable_hash_key(hcx)) - .collect(); + let mut sorted: Vec<_> = v.iter().map(|x| x.to_stable_hash_key(hcx)).collect(); sorted.sort_unstable(); sorted.hash_stable(hcx, hasher); }