summaryrefslogtreecommitdiffstats
path: root/third_party/rust/phf/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/phf/src
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/phf/src')
-rw-r--r--third_party/rust/phf/src/lib.rs124
-rw-r--r--third_party/rust/phf/src/map.rs199
-rw-r--r--third_party/rust/phf/src/set.rs113
3 files changed, 436 insertions, 0 deletions
diff --git a/third_party/rust/phf/src/lib.rs b/third_party/rust/phf/src/lib.rs
new file mode 100644
index 0000000000..b5b587d445
--- /dev/null
+++ b/third_party/rust/phf/src/lib.rs
@@ -0,0 +1,124 @@
+//! Compile-time generated maps and sets.
+//!
+//! The `phf::Map` and `phf::Set` types have roughly comparable performance to
+//! a standard hash table, but can be generated as compile-time static values.
+//!
+//! # Usage
+//!
+//! If the `macros` Cargo feature is enabled, the `phf_map`, `phf_set`,
+//! `phf_ordered_map`, and `phf_ordered_set` macros can be used to construct
+//! the PHF type. This method can be used with a stable compiler
+//! (`rustc` version 1.30+)
+//!
+//! ```toml
+//! [dependencies]
+//! phf = { version = "0.7.24", features = ["macros"] }
+//! ```
+//!
+//! ```
+//! use phf::{phf_map, phf_set};
+//!
+//! static MY_MAP: phf::Map<&'static str, u32> = phf_map! {
+//! "hello" => 1,
+//! "world" => 2,
+//! };
+//!
+//! static MY_SET: phf::Set<&'static str> = phf_set! {
+//! "hello world",
+//! "hola mundo",
+//! };
+//!
+//! fn main() {
+//! assert_eq!(MY_MAP["hello"], 1);
+//! assert!(MY_SET.contains("hello world"));
+//! }
+//! ```
+//!
+//! (Alternatively, you can use the phf_codegen crate to generate PHF datatypes
+//! in a build script)
+#![doc(html_root_url="https://docs.rs/phf/0.7")]
+#![warn(missing_docs)]
+#![cfg_attr(not(feature = "std"), no_std)]
+
+#[cfg(feature = "std")]
+extern crate std as core;
+
+#[cfg(feature = "macros")]
+/// Macro to create a `static` (compile-time) [`Map`].
+///
+/// Requires the `"macros"` feature.
+///
+/// # Example
+///
+/// ```rust,edition2018
+/// use ::phf::{phf_map, Map};
+///
+/// static MY_MAP: Map<&'static str, u32> = phf_map! {
+/// "hello" => 1,
+/// "world" => 2,
+/// };
+///
+/// fn main ()
+/// {
+/// assert_eq!(MY_MAP["hello"], 1);
+/// }
+/// ```
+#[::proc_macro_hack::proc_macro_hack]
+pub use phf_macros:: phf_map;
+
+#[cfg(feature = "macros")]
+/// Macro to create a `static` (compile-time) [`Set`].
+///
+/// Requires the `"macros"` feature.
+///
+/// # Example
+///
+/// ```rust,edition2018
+/// use ::phf::{phf_set, Set};
+///
+/// static MY_SET: Set<&'static str> = phf_set! {
+/// "hello world",
+/// "hola mundo",
+/// };
+///
+/// fn main ()
+/// {
+/// assert!(MY_SET.contains("hello world"));
+/// }
+/// ```
+#[::proc_macro_hack::proc_macro_hack]
+pub use phf_macros::phf_set;
+
+use core::ops::Deref;
+
+pub use phf_shared::PhfHash;
+#[doc(inline)]
+pub use self::map::Map;
+#[doc(inline)]
+pub use self::set::Set;
+
+pub mod map;
+pub mod set;
+
+// WARNING: this is not considered part of phf's public API and is subject to
+// change at any time.
+//
+// Basically Cow, but with the Owned version conditionally compiled
+#[doc(hidden)]
+pub enum Slice<T: 'static> {
+ Static(&'static [T]),
+ #[cfg(feature = "std")]
+ Dynamic(Vec<T>),
+}
+
+impl<T> Deref for Slice<T> {
+ type Target = [T];
+
+ fn deref(&self) -> &[T] {
+ match *self {
+ Slice::Static(t) => t,
+ #[cfg(feature = "std")]
+ Slice::Dynamic(ref t) => t,
+ }
+ }
+}
diff --git a/third_party/rust/phf/src/map.rs b/third_party/rust/phf/src/map.rs
new file mode 100644
index 0000000000..30fc26487e
--- /dev/null
+++ b/third_party/rust/phf/src/map.rs
@@ -0,0 +1,199 @@
+//! An immutable map constructed at compile time.
+use core::borrow::Borrow;
+use core::ops::Index;
+use core::slice;
+use core::fmt;
+use core::iter::IntoIterator;
+use phf_shared::{self, PhfHash, HashKey};
+use crate::Slice;
+
+/// 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.
+pub struct Map<K: 'static, V: 'static> {
+ #[doc(hidden)]
+ pub key: HashKey,
+ #[doc(hidden)]
+ pub disps: Slice<(u32, u32)>,
+ #[doc(hidden)]
+ pub entries: Slice<(K, V)>,
+}
+
+impl<K, V> fmt::Debug for Map<K, V> where K: fmt::Debug, V: fmt::Debug {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.debug_map().entries(self.entries()).finish()
+ }
+}
+
+impl<'a, K, V, T: ?Sized> Index<&'a T> for Map<K, V> where T: Eq + PhfHash, K: Borrow<T> {
+ type Output = V;
+
+ fn index(&self, k: &'a T) -> &V {
+ self.get(k).expect("invalid key")
+ }
+}
+
+impl<K, V> Map<K, V> {
+ /// Returns true if the `Map` is empty.
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+
+ /// Returns the number of entries in the `Map`.
+ pub fn len(&self) -> usize {
+ self.entries.len()
+ }
+
+ /// Determines if `key` is in the `Map`.
+ pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
+ where T: Eq + PhfHash,
+ K: Borrow<T>
+ {
+ self.get(key).is_some()
+ }
+
+ /// Returns a reference to the value that `key` maps to.
+ pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
+ where T: Eq + PhfHash,
+ K: Borrow<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.
+ pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K>
+ where T: Eq + PhfHash,
+ K: Borrow<T>
+ {
+ self.get_entry(key).map(|e| e.0)
+ }
+
+ /// Like `get`, but returns both the key and the value.
+ pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
+ where T: Eq + PhfHash,
+ K: Borrow<T>
+ {
+ if self.disps.len() == 0 { 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.
+ pub fn entries<'a>(&'a self) -> Entries<'a, 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.
+ pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
+ Keys { iter: self.entries() }
+ }
+
+ /// Returns an iterator over the values in the map.
+ ///
+ /// Values are returned in an arbitrary but fixed order.
+ pub fn values<'a>(&'a self) -> Values<'a, 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>;
+
+ fn into_iter(self) -> Entries<'a, K, V> {
+ self.entries()
+ }
+}
+
+/// An iterator over the key/value pairs in a `Map`.
+pub struct Entries<'a, K: 'a, V: 'a> {
+ iter: slice::Iter<'a, (K, V)>,
+}
+
+impl<'a, K, V> Iterator for Entries<'a, K, V> {
+ type Item = (&'a K, &'a V);
+
+ fn next(&mut self) -> Option<(&'a K, &'a V)> {
+ self.iter.next().map(|&(ref k, ref v)| (k, v))
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.iter.size_hint()
+ }
+}
+
+impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {
+ fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
+ self.iter.next_back().map(|e| (&e.0, &e.1))
+ }
+}
+
+impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}
+
+/// An iterator over the keys in a `Map`.
+pub struct Keys<'a, K: 'a, V: 'a> {
+ iter: Entries<'a, K, V>,
+}
+
+impl<'a, K, V> Iterator for Keys<'a, K, V> {
+ type Item = &'a K;
+
+ fn next(&mut self) -> Option<&'a K> {
+ self.iter.next().map(|e| e.0)
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.iter.size_hint()
+ }
+}
+
+impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
+ fn next_back(&mut self) -> Option<&'a K> {
+ self.iter.next_back().map(|e| e.0)
+ }
+}
+
+impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}
+
+/// An iterator over the values in a `Map`.
+pub struct Values<'a, K: 'a, V: 'a> {
+ iter: Entries<'a, K, V>,
+}
+
+impl<'a, K, V> Iterator for Values<'a, K, V> {
+ type Item = &'a V;
+
+ fn next(&mut self) -> Option<&'a V> {
+ self.iter.next().map(|e| e.1)
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.iter.size_hint()
+ }
+}
+
+impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
+ fn next_back(&mut self) -> Option<&'a V> {
+ self.iter.next_back().map(|e| e.1)
+ }
+}
+
+impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}
diff --git a/third_party/rust/phf/src/set.rs b/third_party/rust/phf/src/set.rs
new file mode 100644
index 0000000000..9588d4a747
--- /dev/null
+++ b/third_party/rust/phf/src/set.rs
@@ -0,0 +1,113 @@
+//! An immutable set constructed at compile time.
+use core::borrow::Borrow;
+use core::iter::IntoIterator;
+use core::fmt;
+
+use crate::{map, Map, PhfHash};
+
+/// An immutable set constructed at compile time.
+///
+/// ## Note
+///
+/// The fields of this struct are public so that they may be initialized by the
+/// `phf_set!` macro and code generation. They are subject to change at any
+/// time and should never be accessed directly.
+pub struct Set<T: 'static> {
+ #[doc(hidden)]
+ pub map: Map<T, ()>,
+}
+
+impl<T> fmt::Debug for Set<T> where T: fmt::Debug {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.debug_set().entries(self).finish()
+ }
+}
+
+impl<T> Set<T> {
+ /// Returns the number of elements in the `Set`.
+ pub fn len(&self) -> usize {
+ self.map.len()
+ }
+
+ /// Returns true if the `Set` contains no elements.
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+
+ /// Returns a reference to the set's internal static instance of the given
+ /// key.
+ ///
+ /// This can be useful for interning schemes.
+ pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T>
+ where U: Eq + PhfHash,
+ T: Borrow<U>
+ {
+ self.map.get_key(key)
+ }
+
+ /// Returns true if `value` is in the `Set`.
+ pub fn contains<U: ?Sized>(&self, value: &U) -> bool
+ where U: Eq + PhfHash,
+ T: Borrow<U>
+ {
+ self.map.contains_key(value)
+ }
+
+ /// Returns an iterator over the values in the set.
+ ///
+ /// Values are returned in an arbitrary but fixed order.
+ pub fn iter<'a>(&'a self) -> Iter<'a, T> {
+ Iter { iter: self.map.keys() }
+ }
+}
+
+impl<T> Set<T> where T: Eq + PhfHash {
+ /// Returns true if `other` shares no elements with `self`.
+ pub fn is_disjoint(&self, other: &Set<T>) -> bool {
+ !self.iter().any(|value| other.contains(value))
+ }
+
+ /// Returns true if `other` contains all values in `self`.
+ pub fn is_subset(&self, other: &Set<T>) -> bool {
+ self.iter().all(|value| other.contains(value))
+ }
+
+ /// Returns true if `self` contains all values in `other`.
+ pub fn is_superset(&self, other: &Set<T>) -> bool {
+ other.is_subset(self)
+ }
+}
+
+impl<'a, T> IntoIterator for &'a Set<T> {
+ type Item = &'a T;
+ type IntoIter = Iter<'a, T>;
+
+ fn into_iter(self) -> Iter<'a, T> {
+ self.iter()
+ }
+}
+
+/// An iterator over the values in a `Set`.
+pub struct Iter<'a, T: 'static> {
+ iter: map::Keys<'a, T, ()>,
+}
+
+impl<'a, T> Iterator for Iter<'a, T> {
+ type Item = &'a T;
+
+ fn next(&mut self) -> Option<&'a T> {
+ self.iter.next()
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.iter.size_hint()
+ }
+}
+
+impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
+ fn next_back(&mut self) -> Option<&'a T> {
+ self.iter.next_back()
+ }
+}
+
+impl<'a, T> ExactSizeIterator for Iter<'a, T> {}