//! An immutable map constructed at compile time. use core::fmt; use core::iter::FusedIterator; use core::iter::IntoIterator; use core::ops::Index; use core::slice; use phf_shared::{self, HashKey, PhfBorrow, PhfHash}; #[cfg(feature = "serde")] use serde::ser::{Serialize, SerializeMap, Serializer};
/// An immutable map constructed at compile time. /// /// ## Note /// /// The fields of this struct are public so that they may be initialized by the /// `phf_map!` macro and code generation. They are subject to change at any /// time and should never be accessed directly. pubstruct Map<K: 'static, V: 'static> { #[doc(hidden)] pub key: HashKey, #[doc(hidden)] pub disps: &'static [(u32, u32)], #[doc(hidden)] pub entries: &'static [(K, V)],
}
/// Returns the number of entries in the `Map`. #[inline] pubconstfn len(&self) -> usize { self.entries.len()
}
/// Returns true if the `Map` is empty. #[inline] pubconstfn is_empty(&self) -> bool { self.len() == 0
}
/// Determines if `key` is in the `Map`. pubfn contains_key<T: ?Sized>(&self, key: &T) -> bool where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{ self.get(key).is_some()
}
/// Returns a reference to the value that `key` maps to. pubfn get<T: ?Sized>(&self, key: &T) -> Option<&V> where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{ self.get_entry(key).map(|e| e.1)
}
/// Returns a reference to the map's internal static instance of the given /// key. /// /// This can be useful for interning schemes. pubfn get_key<T: ?Sized>(&self, key: &T) -> Option<&K> where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{ self.get_entry(key).map(|e| e.0)
}
/// Like `get`, but returns both the key and the value. pubfn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)> where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{ ifself.disps.is_empty() { return None;
} //Prevent panic on empty map let hashes = phf_shared::hash(key, &self.key); let index = phf_shared::get_index(&hashes, self.disps, self.entries.len()); let entry = &self.entries[index as usize]; let b: &T = entry.0.borrow(); if b == key {
Some((&entry.0, &entry.1))
} else {
None
}
}
/// Returns an iterator over the key/value pairs in the map. /// /// Entries are returned in an arbitrary but fixed order. pubfn entries(&self) -> Entries<'_, K, V> {
Entries {
iter: self.entries.iter(),
}
}
/// Returns an iterator over the keys in the map. /// /// Keys are returned in an arbitrary but fixed order. pubfn keys(&self) -> Keys<'_, K, V> {
Keys {
iter: self.entries(),
}
}
/// Returns an iterator over the values in the map. /// /// Values are returned in an arbitrary but fixed order. pubfn values(&self) -> Values<'_, K, V> {
Values {
iter: self.entries(),
}
}
}
impl<'a, K, V> IntoIterator for &'a Map<K, V> { type Item = (&'a K, &'a V); type IntoIter = Entries<'a, K, V>;
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.