let Hash128 {
h1: lower,
h2: upper,
} = hasher.finish128();
Hashes {
g: (lower >> 32) as u32,
f1: lower as u32,
f2: upper as u32,
}
}
/// Return an index into `phf_generator::HashState::map`. /// /// * `hash` is from `hash()` in this crate. /// * `disps` is from `phf_generator::HashState::disps`. /// * `len` is the length of `phf_generator::HashState::map`. #[inline] pubfn get_index(hashes: &Hashes, disps: &[(u32, u32)], len: usize) -> u32 { let (d1, d2) = disps[(hashes.g % (disps.len() as u32)) as usize];
displace(hashes.f1, hashes.f2, d1, d2) % (len as u32)
}
/// A trait implemented by types which can be used in PHF data structures. /// /// This differs from the standard library's `Hash` trait in that `PhfHash`'s /// results must be architecture independent so that hashes will be consistent /// between the host and target when cross compiling. pubtrait PhfHash { /// Feeds the value into the state given, updating the hasher as necessary. fn phf_hash<H: Hasher>(&self, state: &mut H);
/// Feeds a slice of this type into the state provided. fn phf_hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: Sized,
{ for piece in data {
piece.phf_hash(state);
}
}
}
/// Trait for printing types with `const` constructors, used by `phf_codegen` and `phf_macros`. pubtrait FmtConst { /// Print a `const` expression representing this value. fn fmt_const(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
}
/// Identical to `std::borrow::Borrow` except omitting blanket impls to facilitate other /// borrowing patterns. /// /// The same semantic requirements apply: /// /// > In particular `Eq`, `Ord` and `Hash` must be equivalent for borrowed and owned values: /// `x.borrow() == y.borrow()` should give the same result as `x == y`. /// /// (This crate's API only requires `Eq` and `PhfHash`, however.) /// /// ### Motivation /// The conventional signature for lookup methods on collections looks something like this: /// /// ```ignore /// impl<K, V> Map<K, V> where K: PhfHash + Eq { /// fn get<T: ?Sized>(&self, key: &T) -> Option<&V> where T: PhfHash + Eq, K: Borrow<T> { /// ... /// } /// } /// ``` /// /// This allows the key type used for lookup to be different than the key stored in the map so for /// example you can use `&str` to look up a value in a `Map<String, _>`. However, this runs into /// a problem in the case where `T` and `K` are both a `Foo<_>` type constructor but /// the contained type is different (even being the same type with different lifetimes). /// /// The main issue for this crate's API is that, with this method signature, you cannot perform a /// lookup on a `Map<UniCase<&'static str>, _>` with a `UniCase<&'a str>` where `'a` is not /// `'static`; there is no impl of `Borrow` that resolves to /// `impl Borrow<UniCase<'a>> for UniCase<&'static str>` and one cannot be added either because of /// all the blanket impls. /// /// Instead, this trait is implemented conservatively, without blanket impls, so that impls like /// this may be added. This is feasible since the set of types that implement `PhfHash` is /// intentionally small. /// /// This likely won't be fixable with specialization alone but will require full support for lattice /// impls since we technically want to add overlapping blanket impls. pubtrait PhfBorrow<B: ?Sized> { /// Convert a reference to `self` to a reference to the borrowed type. fn borrow(&self) -> &B;
}
/// Create an impl of `FmtConst` delegating to `fmt::Debug` for types that can deal with it. /// /// Ideally with specialization this could be just one default impl and then specialized where /// it doesn't apply.
macro_rules! delegate_debug (
($ty:ty) => { impl FmtConst for $ty { fn fmt_const(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
}
);
#[cfg(feature = "uncased")] impl FmtConst for uncased::UncasedStr { fn fmt_const(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // transmute is not stable in const fns (rust-lang/rust#53605), so // `UncasedStr::new` can't be a const fn itself, but we can inline the // call to transmute here in the meantime.
f.write_str("unsafe { ::core::mem::transmute::<&'static str, &'static UncasedStr>(")?; self.as_str().fmt_const(f)?;
f.write_str(") }")
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.