summaryrefslogtreecommitdiffstats
path: root/vendor/hashlink-0.6.0
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-05-17 19:51:09 +0100
committerEllie Huxtable <e@elm.sh>2021-05-17 19:51:09 +0100
commitd0215a937a7889a97e11778ee4b0f9a12de01278 (patch)
tree5a8fd5ad62e6b5a4c218746ff2d4bd97373a48de /vendor/hashlink-0.6.0
parent802a2258cbd839c5b82d24f74d7aebe4a27d8dc5 (diff)
Vendor dependenciesvendor
Just testing how CI works with this. I tend to prefer vendoring, as it means that if you have a copy of the code *you can always build it*. Even if you're 20 years in the future This is the output of ``` cargo vendor --versioned-dirs ```
Diffstat (limited to 'vendor/hashlink-0.6.0')
-rw-r--r--vendor/hashlink-0.6.0/.cargo-checksum.json1
-rw-r--r--vendor/hashlink-0.6.0/CHANGELOG.md51
-rw-r--r--vendor/hashlink-0.6.0/Cargo.toml37
-rw-r--r--vendor/hashlink-0.6.0/LICENSE-APACHE201
-rw-r--r--vendor/hashlink-0.6.0/LICENSE-MIT26
-rw-r--r--vendor/hashlink-0.6.0/README.md67
-rw-r--r--vendor/hashlink-0.6.0/src/lib.rs9
-rw-r--r--vendor/hashlink-0.6.0/src/linked_hash_map.rs2053
-rw-r--r--vendor/hashlink-0.6.0/src/linked_hash_set.rs753
-rw-r--r--vendor/hashlink-0.6.0/src/lru_cache.rs292
-rw-r--r--vendor/hashlink-0.6.0/src/serde.rs151
-rw-r--r--vendor/hashlink-0.6.0/tests/linked_hash_map.rs498
-rw-r--r--vendor/hashlink-0.6.0/tests/linked_hash_set.rs512
-rw-r--r--vendor/hashlink-0.6.0/tests/lru_cache.rs166
-rw-r--r--vendor/hashlink-0.6.0/tests/serde.rs59
15 files changed, 4876 insertions, 0 deletions
diff --git a/vendor/hashlink-0.6.0/.cargo-checksum.json b/vendor/hashlink-0.6.0/.cargo-checksum.json
new file mode 100644
index 00000000..49b02702
--- /dev/null
+++ b/vendor/hashlink-0.6.0/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"CHANGELOG.md":"e9109ae14db72bf03cf72b5de1b2ecd421fac08c9185287ee80752c60b3ab7fe","Cargo.toml":"bd45b0b99fddf93fe48bfe59e372d55e43c0528b25aac4710711be1d086645be","LICENSE-APACHE":"c144680885b29e4719e2a51f0aab5439a1e02d980692b5aaf086cae12727f28b","LICENSE-MIT":"e915669a595b11a200873df8286561881b0e04932f6412a585db6297ba0bc97c","README.md":"f2b040b9aa899d3bd9fbb6c2391054980b00e7f475b6066071c17dd59d614d1c","src/lib.rs":"12f7bf2e1bdc312fd9740b639983916a2e7a8582c588902af8c4ac71b9032fde","src/linked_hash_map.rs":"d80f3d692655d18eb2d8f65be5d4f000cc95219a44f9b8f2eac1746502401d63","src/linked_hash_set.rs":"d71243afc3932df593e8a031266e7b71358f9304ad38a2a9baab4fec91d762fd","src/lru_cache.rs":"d2322af90780a3ef192f25d213f5af38e1dc6492a323fb361757d53692058774","src/serde.rs":"cc6287e48d740671c16848c1cf206da32011daa04efdca79f528736b4d2cdf03","tests/linked_hash_map.rs":"576decc19ebdc54bfbf653c1bf395792d80f5f0fd1d1f1eb9fbf1d45b295389b","tests/linked_hash_set.rs":"91fb96a11d1935343208e258696354b1436d7e497d786ceb6c681d0039a499d1","tests/lru_cache.rs":"c0328001d53e2a0d1ef6fb36550e8cbb989ef1914cef3657f1832b280f4d4572","tests/serde.rs":"d4b9b38d9812f4926dd7effff43c0d51d27c07477f732e9a2feb18f796b39b22"},"package":"d99cf782f0dc4372d26846bec3de7804ceb5df083c2d4462c0b8d2330e894fa8"} \ No newline at end of file
diff --git a/vendor/hashlink-0.6.0/CHANGELOG.md b/vendor/hashlink-0.6.0/CHANGELOG.md
new file mode 100644
index 00000000..12963eab
--- /dev/null
+++ b/vendor/hashlink-0.6.0/CHANGELOG.md
@@ -0,0 +1,51 @@
+## [0.6.0]
+- API incompatible change: depend on hashbrown 0.9, re-export renamed
+ hashbrown::TryReserveError type.
+- Add a `Debug` impl to `LruCache` (thanks @thomcc!)
+- Adjust trait bounds for `LinkedHashMap::retain`, `LinkedHashSet::default` to
+ be less strict (to match hashbrown)
+- Adjust trait bounds for all `Debug` impls to be less strict (to match
+ hashbrown).
+- Adjust trait bounds for all `IntoIterator` impls to be less strict (to match
+ hashbrown).
+- Adjust trait bounds for `LruCache::with_hasher`, `LruCache::capacity`,
+ `LruCache::len`, `LruCache::is_empty`, `LruCache::clear`, `LruCache::iter`,
+ `LruCache::iter_mut`, and `LruCache::drain` to be less strict
+- Add optional serde support for `LinkedHashMap` and `LinkedHashSet`.
+- Add `to_back` and `to_front` methods for LinkedHashSet to control entry order.
+
+## [0.5.1]
+- Add `LinkedHashMap::remove_entry` and `LruCache::remove_entry`
+- Add `LruCache::new_unbounded` constructor that sets capacity to usize::MAX
+- Add `LruCache::get` method to go with `LruCache::get_mut`
+- Add `LruCache::peek` and `LruCache::peek_mut` to access the cache without
+ moving the entry in the LRU list
+
+## [0.5.0]
+- API incompatible change: depend on hashbrown 0.7
+
+## [0.4.0]
+- API incompatible change: depend on hashbrown 0.6
+- Passes miri
+
+## [0.3.0]
+- Add some *minimal* documentation for methods that change the internal ordering.
+- Decide on a pattern for methods that change the internal ordering: the word
+ "insert" means that it will move an existing entry to the back.
+- Some methods have been renamed to conform to the above system.
+
+## [0.2.1]
+- Fix variance for LinkedHashMap (now covariant where appropriate)
+- Add Debug impls to many more associated types
+- Add LinkedHashSet
+- Add `LinkedHashMap::retain`
+
+## [0.2.0]
+- Move `linked_hash_map` into its own module
+- Add `LruCache` type ported from `lru-cache` crate into its own module
+- Add `LruCache` entry and raw-entry API
+- Add `linked_hash_map` `IntoIter` iterator that is different from `Drain` iterator
+- Make `Drain` iterator recycle freed linked list nodes
+
+## [0.1.0]
+- Initial release
diff --git a/vendor/hashlink-0.6.0/Cargo.toml b/vendor/hashlink-0.6.0/Cargo.toml
new file mode 100644
index 00000000..6a5a2d41
--- /dev/null
+++ b/vendor/hashlink-0.6.0/Cargo.toml
@@ -0,0 +1,37 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+edition = "2018"
+name = "hashlink"
+version = "0.6.0"
+authors = ["kyren <kerriganw@gmail.com>"]
+description = "HashMap-like containers that hold their key-value pairs in a user controllable order"
+documentation = "https://docs.rs/hashlink"
+readme = "README.md"
+keywords = ["data-structures"]
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/kyren/hashlink"
+[dependencies.hashbrown]
+version = "0.9.0"
+
+[dependencies.serde]
+version = "1.0"
+optional = true
+[dev-dependencies.serde_test]
+version = "1.0"
+
+[features]
+serde_impl = ["serde"]
+[badges.circle-ci]
+branch = "master"
+repository = "kyren/hashlink"
diff --git a/vendor/hashlink-0.6.0/LICENSE-APACHE b/vendor/hashlink-0.6.0/LICENSE-APACHE
new file mode 100644
index 00000000..1b22bef9
--- /dev/null
+++ b/vendor/hashlink-0.6.0/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. \ No newline at end of file
diff --git a/vendor/hashlink-0.6.0/LICENSE-MIT b/vendor/hashlink-0.6.0/LICENSE-MIT
new file mode 100644
index 00000000..b3cad876
--- /dev/null
+++ b/vendor/hashlink-0.6.0/LICENSE-MIT
@@ -0,0 +1,26 @@
+This work is derived in part from the `linked-hash-map` crate, Copyright (c)
+2015 The Rust Project Developers
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/vendor/hashlink-0.6.0/README.md b/vendor/hashlink-0.6.0/README.md
new file mode 100644
index 00000000..9272b0d4
--- /dev/null
+++ b/vendor/hashlink-0.6.0/README.md
@@ -0,0 +1,67 @@
+# hashlink -- HashMap-like containers that hold their key-value pairs in a user controllable order
+
+[![Build Status](https://img.shields.io/circleci/project/github/kyren/hashlink.svg)](https://circleci.com/gh/kyren/hashlink)
+[![Latest Version](https://img.shields.io/crates/v/hashlink.svg)](https://crates.io/crates/hashlink)
+[![API Documentation](https://docs.rs/hashlink/badge.svg)](https://docs.rs/hashlink)
+
+This crate is a fork of
+[linked-hash-map](https://github.com/contain-rs/linked-hash-map) that builds on
+top of [hashbrown](https://github.com/rust-lang/hashbrown) to implement more up
+to date versions of `LinkedHashMap` `LinkedHashSet`, and `LruCache`.
+
+One important API change is that when a `LinkedHashMap` is used as a LRU cache,
+it allows you to easily retrieve an entry and move it to the back OR produce a
+new entry at the back without needlessly repeating key hashing and lookups:
+
+``` rust
+let mut lru_cache = LinkedHashMap::new();
+let key = "key".to_owned();
+// Try to find my expensive to construct and hash key
+let _cached_val = match lru_cache.raw_entry_mut().from_key(&key) {
+ RawEntryMut::Occupied(mut occupied) => {
+ // Cache hit, move entry to the back.
+ occupied.to_back();
+ occupied.into_mut()
+ }
+ RawEntryMut::Vacant(vacant) => {
+ // Insert expensive to construct key and expensive to compute value,
+ // automatically inserted at the back.
+ vacant.insert(key.clone(), 42).1
+ }
+};
+```
+
+Or, a simpler way to do the same thing:
+
+``` rust
+let mut lru_cache = LinkedHashMap::new();
+let key = "key".to_owned();
+let _cached_val = lru_cache
+ .raw_entry_mut()
+ .from_key(&key)
+ .or_insert_with(|| (key.clone(), 42));
+```
+
+This crate contains a decent amount of unsafe code from handling its internal
+linked list, and the unsafe code has diverged quite a lot from the original
+`linked-hash-map` implementation. It currently passes tests under miri and
+sanitizers, but it should probably still receive more review and testing, and
+check for test code coverage.
+
+## Credit
+
+There is a huge amount of code in this crate that is copied verbatim from
+`linked-hash-map` and `hashbrown`, especially tests, associated types like
+iterators, and things like `Debug` impls.
+
+## License
+
+This library is licensed the same as
+[linked-hash-map](https://github.com/contain-rs/linked-hash-map) and
+[hashbrown](https://github.com/rust-lang/hashbrown), it is licensed under either
+of:
+
+* MIT license [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
+* Apache License 2.0 [LICENSE-APACHE](LICENSE-APACHE) or https://opensource.org/licenses/Apache-2.0
+
+at your option.
diff --git a/vendor/hashlink-0.6.0/src/lib.rs b/vendor/hashlink-0.6.0/src/lib.rs
new file mode 100644
index 00000000..55bdcd2e
--- /dev/null
+++ b/vendor/hashlink-0.6.0/src/lib.rs
@@ -0,0 +1,9 @@
+pub mod linked_hash_map;
+pub mod linked_hash_set;
+pub mod lru_cache;
+#[cfg(feature = "serde_impl")]
+pub mod serde;
+
+pub use linked_hash_map::LinkedHashMap;
+pub use linked_hash_set::LinkedHashSet;
+pub use lru_cache::LruCache;
diff --git a/vendor/hashlink-0.6.0/src/linked_hash_map.rs b/vendor/hashlink-0.6.0/src/linked_hash_map.rs
new file mode 100644
index 00000000..32733ea2
--- /dev/null
+++ b/vendor/hashlink-0.6.0/src/linked_hash_map.rs
@@ -0,0 +1,2053 @@
+use std::{
+ borrow::Borrow,
+ cmp::Ordering,
+ fmt,
+ hash::{BuildHasher, Hash, Hasher},
+ iter::FromIterator,
+ marker::PhantomData,
+ mem::{self, MaybeUninit},
+ ops::{Index, IndexMut},
+ ptr::{self, NonNull},
+};
+
+use hashbrown::{hash_map, HashMap};
+
+pub type TryReserveError = hashbrown::TryReserveError;
+
+/// A version of `HashMap` that has a user controllable order for its entries.
+///
+/// It achieves this by keeping its entries in an internal linked list and using a `HashMap` to
+/// point at nodes in this linked list.
+///
+/// The order of entries defaults to "insertion order", but the user can also modify the order of
+/// existing entries by manually moving them to the front or back.
+///
+/// There are two kinds of methods that modify the order of the internal list:
+///
+/// * Methods that have names like `to_front` and `to_back` will unsurprisingly move an existing
+/// entry to the front or back
+/// * Methods that have the word `insert` will insert a new entry ot the back of the list, and if
+/// that method might replace an entry, that method will *also move that existing entry to the
+/// back*.
+pub struct LinkedHashMap<K, V, S = hash_map::DefaultHashBuilder> {
+ map: HashMap<NonNull<Node<K, V>>, (), NullHasher>,
+ // We need to keep any custom hash builder outside of the HashMap so we can access it alongside
+ // the entry API without mutable aliasing.
+ hash_builder: S,
+ // Circular linked list of nodes. If `values` is non-null, it will point to a "guard node"
+ // which will never have an initialized key or value, `values.prev` will contain the last key /
+ // value in the list, `values.next` will contain the first key / value in the list.
+ values: Option<NonNull<Node<K, V>>>,
+ // *Singly* linked list of free nodes. The `prev` pointers in the free list should be assumed
+ // invalid.
+ free: Option<NonNull<Node<K, V>>>,
+}
+
+impl<K, V> LinkedHashMap<K, V> {
+ #[inline]
+ pub fn new() -> Self {
+ Self {
+ hash_builder: hash_map::DefaultHashBuilder::default(),
+ map: HashMap::with_hasher(NullHasher),
+ values: None,
+ free: None,
+ }
+ }
+
+ #[inline]
+ pub fn with_capacity(capacity: usize) -> Self {
+ Self {
+ hash_builder: hash_map::DefaultHashBuilder::default(),
+ map: HashMap::with_capacity_and_hasher(capacity, NullHasher),
+ values: None,
+ free: None,
+ }
+ }
+}
+
+impl<K, V, S> LinkedHashMap<K, V, S> {
+ #[inline]
+ pub fn with_hasher(hash_builder: S) -> Self {
+ Self {
+ hash_builder,
+ map: HashMap::with_hasher(NullHasher),
+ values: None,
+ free: None,
+ }
+ }
+
+ #[inline]
+ pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
+ Self {
+ hash_builder,
+ map: HashMap::with_capacity_and_hasher(capacity, NullHasher),
+ values: None,
+ free: None,
+ }
+ }
+
+ #[inline]
+ pub fn reserve(&mut self, additional: usize) {
+ self.map.reserve(additional);
+ }
+
+ #[inline]
+ pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
+ self.map.try_reserve(additional)
+ }
+
+ #[inline]
+ pub fn shrink_to_fit(&mut self) {
+ self.map.shrink_to_fit();
+ unsafe { drop_free_nodes(self.free) };
+ self.free = None;
+ }
+
+ #[inline]
+ pub fn len(&self) -> usize {
+ self.map.len()
+ }
+
+ #[inline]
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+
+ #[inline]
+ pub fn clear(&mut self) {
+ self.map.clear();
+ if let Some(mut values) = self.values {
+ unsafe {
+ drop_value_nodes(values);
+ values.as_mut().links.value = ValueLinks {
+ prev: values,
+ next: values,
+ };
+ }
+ }
+ }
+
+ #[inline]
+ pub fn iter(&self) -> Iter<K, V> {
+ let (head, tail) = if let Some(values) = self.values {
+ unsafe {
+ let ValueLinks { next, prev } = values.as_ref().links.value;
+ (next.as_ptr(), prev.as_ptr())
+ }
+ } else {
+ (ptr::null_mut(), ptr::null_mut())
+ };
+
+ Iter {
+ head,
+ tail,
+ remaining: self.len(),
+ marker: PhantomData,
+ }
+ }
+
+ #[inline]
+ pub fn iter_mut(&mut self) -> IterMut<K, V> {
+ let (head, tail) = if let Some(values) = self.values {
+ unsafe {
+ let ValueLinks { next, prev } = values.as_ref().links.value;
+ (Some(next), Some(prev))
+ }
+ } else {
+ (None, None)
+ };
+
+ IterMut {
+ head,
+ tail,
+ remaining: self.len(),
+ marker: PhantomData,
+ }
+ }
+
+ #[inline]
+ pub fn drain(&mut self) -> Drain<'_, K, V> {
+ unsafe {
+ let (head, tail) = if let Some(mut values) = self.values {
+ let ValueLinks { next, prev } = values.as_ref().links.value;
+ values.as_mut().links.value = ValueLinks {
+ next: values,
+ prev: values,
+ };
+ (Some(next), Some(prev))
+ } else {
+ (None, None)
+ };
+ let len = self.len();
+
+ self.map.clear();
+
+ Drain {
+ free: (&mut self.free).into(),
+ head,
+ tail,
+ remaining: len,
+ marker: PhantomData,
+ }
+ }
+ }
+
+ #[inline]
+ pub fn keys(&self) -> Keys<K, V> {
+ Keys { inner: self.iter() }
+ }
+
+ #[inline]
+ pub fn values(&self) -> Values<K, V> {
+ Values { inner: self.iter() }
+ }
+
+ #[inline]
+ pub fn values_mut(&mut self) -> ValuesMut<K, V> {
+ ValuesMut {
+ inner: self.iter_mut(),
+ }
+ }
+
+ #[inline]
+ pub fn front(&self) -> Option<(&K, &V)> {
+ if self.is_empty() {
+ return None;
+ }
+ unsafe {
+ let front = (*self.values.as_ptr()).links.value.next.as_ptr();
+ let (key, value) = (*front).entry_ref();
+ Some((key, value))
+ }
+ }
+
+ #[inline]
+ pub fn back(&self) -> Option<(&K, &V)> {
+ if self.is_empty() {
+ return None;
+ }
+ unsafe {
+ let back = &*(*self.values.as_ptr()).links.value.prev.as_ptr();
+ let (key, value) = (*back).entry_ref();
+ Some((key, value))
+ }
+ }
+
+ #[inline]
+ pub fn retain<F>(&mut self, mut f: F)
+ where
+ F: FnMut(&K, &mut V) -> bool,
+ {
+ // We do not drop the key and value when a value is filtered from the map during the call to
+ // `retain`. We need to be very careful not to have a live `HashMap` entry pointing to
+ // either a dangling `Node` or a `Node` with dropped keys / values. Since the key and value
+ // types may panic on drop, they may short-circuit the entry in the map actually being
+ // removed. Instead, we push the removed nodes onto the free list eagerly, then try and
+ // drop the keys and values for any newly freed nodes *after* `HashMap::retain` has
+ // completely finished.
+ struct DropFilteredValues<'a, K, V> {
+ free: &'a mut Option<NonNull<Node<K, V>>>,
+ cur_free: Option<NonNull<Node<K, V>>>,
+ }
+
+ impl<'a, K, V> DropFilteredValues<'a, K, V> {
+ #[inline]
+ fn drop_later(&mut self, node: NonNull<Node<K, V>>) {
+ unsafe {
+ detach_node(node);
+ push_free(&mut self.cur_free, node);
+ }
+ }
+ }
+
+ impl<'a, K, V> Drop for DropFilteredValues<'a, K, V> {
+ fn drop(&mut self) {
+ unsafe {
+ let end_free = self.cur_free;
+ while self.cur_free != *self.free {
+ let cur_free = self.cur_free.as_ptr();
+ (*cur_free).take_entry();
+ self.cur_free = (*cur_free).links.free.next;
+ }
+ *self.free = end_free;
+ }
+ }
+ }
+
+ let free = self.free;
+ let mut drop_filtered_values = DropFilteredValues {
+ free: &mut self.free,
+ cur_free: free,
+ };
+
+ self.map.retain(|&node, _| unsafe {
+ let (k, v) = (*node.as_ptr()).entry_mut();
+ if f(k, v) {
+ true
+ } else {
+ drop_filtered_values.drop_later(node);
+ false
+ }
+ });
+ }
+
+ #[inline]
+ pub fn hasher(&self) -> &S {
+ &self.hash_builder
+ }
+
+ #[inline]
+ pub fn capacity(&self) -> usize {
+ self.map.capacity()
+ }
+}
+
+impl<K, V, S> LinkedHashMap<K, V, S>
+where
+ K: Eq + Hash,