Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1 @@
{"files":{"CHANGELOG.md":"7ddf30caa64a1fe8784c1a515dfc8760ca15096c3f675dff705504246d1591f0","Cargo.toml":"818242810576677a1c0b01b7301177385ae27285b52769f91205289d6e2bc003","LICENSE-APACHE":"c144680885b29e4719e2a51f0aab5439a1e02d980692b5aaf086cae12727f28b","LICENSE-MIT":"e915669a595b11a200873df8286561881b0e04932f6412a585db6297ba0bc97c","README.md":"f2b040b9aa899d3bd9fbb6c2391054980b00e7f475b6066071c17dd59d614d1c","src/lib.rs":"1de536f36f50b780db29d9695970c28ce77677cf2de6a7e27bea148c905b719f","src/linked_hash_map.rs":"3ad55fbc903b37d2e351ef9f401d85abe4ef1aa87bf95989d1cd5fba8e5a0c2b","src/linked_hash_set.rs":"e67bdbcf0626b2f8b8520691882aa06f8662582b134f81be3f705f0b5434fb7a","src/lru_cache.rs":"9fc56c3cfb9575378f8e92cb48b031b1eaec9f6c3bb21886f7fb636d32645c8e","src/serde.rs":"5b216ccd4b21f3093bb4baf18b9f3943f9ae6f49d2faad2c3b566e8a0cb99851","tests/linked_hash_map.rs":"c2d259c9d0325f4b73268dc686b8cca8fc3c778c757012825a82474026f28634","tests/linked_hash_set.rs":"bfaa3018a99c5c36cf0059bf7836142c2cc69be7f03a8c20bd52131f877e2eec","tests/lru_cache.rs":"c0328001d53e2a0d1ef6fb36550e8cbb989ef1914cef3657f1832b280f4d4572","tests/serde.rs":"4e0b1c19c3c542f0b9adac72f6ff32907da6bb58b4f1810dcdf9cd4e2eef34b1"},"package":"e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7"}

View file

@ -0,0 +1,81 @@
## [0.8.4]
- Now builds with `#![no_std]`.
## [0.8.3]
- bump hashbrown to 0.14
## [0.8.2]
- bump hashbrown to 0.13
## [0.8.1]
- Add `retain_with_order` methods, equivalent to `retain` but which iterate
through the map in the proper linked list order
## [0.8.0]
- API incompatible change: No longer re-export hashbrown types so that bumping
hashbrown is no longer an API compatible change.
- bump hashbrown to 0.12
- Fix implementation of `shrink_to_fit` to not panic when called on non-empty
containers.
## [0.7.0]
- API incompatible change: depend on hashbrown 0.11, changes re-exported types.
- Fix `LinkedHashSet::back` to take `&self` not `&mut self`.
- API incompatible change: equality tests on `LinkedHashSet` are now *ordered*,
similar to `LinkedHashMap`.
- Make the serde `Deserialize` implementations on `LinkedHashMap` and
`LinkedHashSet` generic on the `BuildHasher` type.
- Add `to_back` and `to_front` methods for `LinkedHashMap` to control entry
order.
## [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

46
third-party/vendor/hashlink/Cargo.toml vendored Normal file
View file

@ -0,0 +1,46 @@
# 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 are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
name = "hashlink"
version = "0.8.4"
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",
"no_std",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/kyren/hashlink"
[dependencies.hashbrown]
version = "0.14"
[dependencies.serde]
version = "1.0"
optional = true
default-features = false
[dev-dependencies.rustc-hash]
version = "1.1"
[dev-dependencies.serde_test]
version = "1.0"
[features]
serde_impl = ["serde"]
[badges.circle-ci]
branch = "master"
repository = "kyren/hashlink"

View file

@ -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.

26
third-party/vendor/hashlink/LICENSE-MIT vendored Normal file
View file

@ -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.

67
third-party/vendor/hashlink/README.md vendored Normal file
View file

@ -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.

12
third-party/vendor/hashlink/src/lib.rs vendored Normal file
View file

@ -0,0 +1,12 @@
#![no_std]
extern crate alloc;
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;

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,766 @@
use core::{
borrow::Borrow,
fmt,
hash::{BuildHasher, Hash, Hasher},
iter::{Chain, FromIterator},
ops::{BitAnd, BitOr, BitXor, Sub},
};
use hashbrown::hash_map::DefaultHashBuilder;
use crate::linked_hash_map::{self, LinkedHashMap, TryReserveError};
pub struct LinkedHashSet<T, S = DefaultHashBuilder> {
map: LinkedHashMap<T, (), S>,
}
impl<T: Hash + Eq> LinkedHashSet<T, DefaultHashBuilder> {
#[inline]
pub fn new() -> LinkedHashSet<T, DefaultHashBuilder> {
LinkedHashSet {
map: LinkedHashMap::new(),
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> LinkedHashSet<T, DefaultHashBuilder> {
LinkedHashSet {
map: LinkedHashMap::with_capacity(capacity),
}
}
}
impl<T, S> LinkedHashSet<T, S> {
#[inline]
pub fn capacity(&self) -> usize {
self.map.capacity()
}
#[inline]
pub fn iter(&self) -> Iter<'_, T> {
Iter {
iter: self.map.keys(),
}
}
#[inline]
pub fn len(&self) -> usize {
self.map.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
#[inline]
pub fn drain(&mut self) -> Drain<T> {
Drain {
iter: self.map.drain(),
}
}
#[inline]
pub fn clear(&mut self) {
self.map.clear()
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.map.retain(|k, _| f(k));
}
}
impl<T, S> LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
#[inline]
pub fn with_hasher(hasher: S) -> LinkedHashSet<T, S> {
LinkedHashSet {
map: LinkedHashMap::with_hasher(hasher),
}
}
#[inline]
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> LinkedHashSet<T, S> {
LinkedHashSet {
map: LinkedHashMap::with_capacity_and_hasher(capacity, hasher),
}
}
#[inline]
pub fn hasher(&self) -> &S {
self.map.hasher()
}
#[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()
}
#[inline]
pub fn difference<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Difference<'a, T, S> {
Difference {
iter: self.iter(),
other,
}
}
#[inline]
pub fn symmetric_difference<'a>(
&'a self,
other: &'a LinkedHashSet<T, S>,
) -> SymmetricDifference<'a, T, S> {
SymmetricDifference {
iter: self.difference(other).chain(other.difference(self)),
}
}
#[inline]
pub fn intersection<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Intersection<'a, T, S> {
Intersection {
iter: self.iter(),
other,
}
}
#[inline]
pub fn union<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Union<'a, T, S> {
Union {
iter: self.iter().chain(other.difference(self)),
}
}
#[inline]
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
self.map.contains_key(value)
}
#[inline]
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: Hash + Eq,
{
self.map.raw_entry().from_key(value).map(|p| p.0)
}
#[inline]
pub fn get_or_insert(&mut self, value: T) -> &T {
self.map
.raw_entry_mut()
.from_key(&value)
.or_insert(value, ())
.0
}
#[inline]
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
where
T: Borrow<Q>,
Q: Hash + Eq,
F: FnOnce(&Q) -> T,
{
self.map
.raw_entry_mut()
.from_key(value)
.or_insert_with(|| (f(value), ()))
.0
}
#[inline]
pub fn is_disjoint(&self, other: &LinkedHashSet<T, S>) -> bool {
self.iter().all(|v| !other.contains(v))
}
#[inline]
pub fn is_subset(&self, other: &LinkedHashSet<T, S>) -> bool {
self.iter().all(|v| other.contains(v))
}
#[inline]
pub fn is_superset(&self, other: &LinkedHashSet<T, S>) -> bool {
other.is_subset(self)
}
/// Inserts the given value into the set.
///
/// If the set did not have this value present, inserts it at the *back* of the internal linked
/// list and returns true, otherwise it moves the existing value to the *back* of the internal
/// linked list and returns false.
#[inline]
pub fn insert(&mut self, value: T) -> bool {
self.map.insert(value, ()).is_none()
}
/// Adds the given value to the set, replacing the existing value.
///
/// If a previous value existed, returns the replaced value. In this case, the value's position
/// in the internal linked list is *not* changed.
#[inline]
pub fn replace(&mut self, value: T) -> Option<T> {
match self.map.entry(value) {
linked_hash_map::Entry::Occupied(occupied) => Some(occupied.replace_key()),
linked_hash_map::Entry::Vacant(vacant) => {
vacant.insert(());
None
}
}
}
#[inline]
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
self.map.remove(value).is_some()
}
#[inline]
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self.map.raw_entry_mut().from_key(value) {
linked_hash_map::RawEntryMut::Occupied(occupied) => Some(occupied.remove_entry().0),
linked_hash_map::RawEntryMut::Vacant(_) => None,
}
}
#[inline]
pub fn front(&self) -> Option<&T> {
self.map.front().map(|(k, _)| k)
}
#[inline]
pub fn pop_front(&mut self) -> Option<T> {
self.map.pop_front().map(|(k, _)| k)
}
#[inline]
pub fn back(&self) -> Option<&T> {
self.map.back().map(|(k, _)| k)
}
#[inline]
pub fn pop_back(&mut self) -> Option<T> {
self.map.pop_back().map(|(k, _)| k)
}
#[inline]
pub fn to_front<Q: ?Sized>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self.map.raw_entry_mut().from_key(value) {
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
occupied.to_front();
true
}
linked_hash_map::RawEntryMut::Vacant(_) => false,
}
}
#[inline]
pub fn to_back<Q: ?Sized>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self.map.raw_entry_mut().from_key(value) {
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
occupied.to_back();
true
}
linked_hash_map::RawEntryMut::Vacant(_) => false,
}
}
#[inline]
pub fn retain_with_order<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.map.retain_with_order(|k, _| f(k));
}
}
impl<T: Hash + Eq + Clone, S: BuildHasher + Clone> Clone for LinkedHashSet<T, S> {
#[inline]
fn clone(&self) -> Self {
let map = self.map.clone();
Self { map }
}
}
impl<T, S> PartialEq for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.iter().eq(other)
}
}
impl<T, S> Hash for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
for e in self {
e.hash(state);
}
}
}
impl<T, S> Eq for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
}
impl<T, S> fmt::Debug for LinkedHashSet<T, S>
where
T: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl<T, S> FromIterator<T> for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> LinkedHashSet<T, S> {
let mut set = LinkedHashSet::with_hasher(Default::default());
set.extend(iter);
set
}
}
impl<T, S> Extend<T> for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.map.extend(iter.into_iter().map(|k| (k, ())));
}
}
impl<'a, T, S> Extend<&'a T> for LinkedHashSet<T, S>
where
T: 'a + Eq + Hash + Copy,
S: BuildHasher,
{
#[inline]
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}
impl<T, S> Default for LinkedHashSet<T, S>
where
S: Default,
{
#[inline]
fn default() -> LinkedHashSet<T, S> {
LinkedHashSet {
map: LinkedHashMap::default(),
}
}
}
impl<'a, 'b, T, S> BitOr<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = LinkedHashSet<T, S>;
#[inline]
fn bitor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
self.union(rhs).cloned().collect()
}
}
impl<'a, 'b, T, S> BitAnd<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = LinkedHashSet<T, S>;
#[inline]
fn bitand(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
self.intersection(rhs).cloned().collect()
}
}
impl<'a, 'b, T, S> BitXor<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = LinkedHashSet<T, S>;
#[inline]
fn bitxor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
self.symmetric_difference(rhs).cloned().collect()
}
}
impl<'a, 'b, T, S> Sub<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = LinkedHashSet<T, S>;
#[inline]
fn sub(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
self.difference(rhs).cloned().collect()
}
}
pub struct Iter<'a, K> {
iter: linked_hash_map::Keys<'a, K, ()>,
}
pub struct IntoIter<K> {
iter: linked_hash_map::IntoIter<K, ()>,
}
pub struct Drain<'a, K: 'a> {
iter: linked_hash_map::Drain<'a, K, ()>,
}
pub struct Intersection<'a, T, S> {
iter: Iter<'a, T>,
other: &'a LinkedHashSet<T, S>,
}
pub struct Difference<'a, T, S> {
iter: Iter<'a, T>,
other: &'a LinkedHashSet<T, S>,
}
pub struct SymmetricDifference<'a, T, S> {
iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
}
pub struct Union<'a, T, S> {
iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
}
impl<'a, T, S> IntoIterator for &'a LinkedHashSet<T, S> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
#[inline]
fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}
impl<T, S> IntoIterator for LinkedHashSet<T, S> {
type Item = T;
type IntoIter = IntoIter<T>;
#[inline]
fn into_iter(self) -> IntoIter<T> {
IntoIter {
iter: self.map.into_iter(),
}
}
}
impl<'a, K> Clone for Iter<'a, K> {
#[inline]
fn clone(&self) -> Iter<'a, K> {
Iter {
iter: self.iter.clone(),
}
}
}
impl<'a, K> Iterator for Iter<'a, K> {
type Item = &'a K;
#[inline]
fn next(&mut self) -> Option<&'a K> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K> ExactSizeIterator for Iter<'a, K> {}
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a T> {
self.iter.next_back()
}
}
impl<'a, K: fmt::Debug> fmt::Debug for Iter<'a, K> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<K> Iterator for IntoIter<K> {
type Item = K;
#[inline]
fn next(&mut self) -> Option<K> {
self.iter.next().map(|(k, _)| k)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<K> ExactSizeIterator for IntoIter<K> {}
impl<K> DoubleEndedIterator for IntoIter<K> {
#[inline]
fn next_back(&mut self) -> Option<K> {
self.iter.next_back().map(|(k, _)| k)
}
}
impl<'a, K> Iterator for Drain<'a, K> {
type Item = K;
#[inline]
fn next(&mut self) -> Option<K> {
self.iter.next().map(|(k, _)| k)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K> DoubleEndedIterator for Drain<'a, K> {
#[inline]
fn next_back(&mut self) -> Option<K> {
self.iter.next_back().map(|(k, _)| k)
}
}
impl<'a, K> ExactSizeIterator for Drain<'a, K> {}
impl<'a, T, S> Clone for Intersection<'a, T, S> {
#[inline]
fn clone(&self) -> Intersection<'a, T, S> {
Intersection {
iter: self.iter.clone(),
..*self
}
}
}
impl<'a, T, S> Iterator for Intersection<'a, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<&'a T> {
loop {
match self.iter.next() {
None => return None,
Some(elt) => {
if self.other.contains(elt) {
return Some(elt);
}
}
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper)
}
}
impl<'a, T, S> fmt::Debug for Intersection<'a, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<'a, T, S> Clone for Difference<'a, T, S> {
#[inline]
fn clone(&self) -> Difference<'a, T, S> {
Difference {
iter: self.iter.clone(),
..*self
}
}
}
impl<'a, T, S> Iterator for Difference<'a, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<&'a T> {
loop {
match self.iter.next() {
None => return None,
Some(elt) => {
if !self.other.contains(elt) {
return Some(elt);
}
}
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper)
}
}
impl<'a, T, S> fmt::Debug for Difference<'a, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> {
#[inline]
fn clone(&self) -> SymmetricDifference<'a, T, S> {
SymmetricDifference {
iter: self.iter.clone(),
}
}
}
impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<&'a T> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, T, S> fmt::Debug for SymmetricDifference<'a, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<'a, T, S> Clone for Union<'a, T, S> {
#[inline]
fn clone(&self) -> Union<'a, T, S> {
Union {
iter: self.iter.clone(),
}
}
}
impl<'a, T, S> fmt::Debug for Union<'a, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<'a, T, S> Iterator for Union<'a, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<&'a T> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

View file

@ -0,0 +1,292 @@
use core::{
borrow::Borrow,
fmt,
hash::{BuildHasher, Hash},
usize,
};
use hashbrown::hash_map;
use crate::linked_hash_map::{self, LinkedHashMap};
pub use crate::linked_hash_map::{
Drain, Entry, IntoIter, Iter, IterMut, OccupiedEntry, RawEntryBuilder, RawEntryBuilderMut,
RawOccupiedEntryMut, RawVacantEntryMut, VacantEntry,
};
pub struct LruCache<K, V, S = hash_map::DefaultHashBuilder> {
map: LinkedHashMap<K, V, S>,
max_size: usize,
}
impl<K: Eq + Hash, V> LruCache<K, V> {
#[inline]
pub fn new(capacity: usize) -> Self {
LruCache {
map: LinkedHashMap::new(),
max_size: capacity,
}
}
/// Create a new unbounded `LruCache` that does not automatically evict entries.
///
/// A simple convenience method that is equivalent to `LruCache::new(usize::MAX)`
#[inline]
pub fn new_unbounded() -> Self {
LruCache::new(usize::MAX)
}
}
impl<K, V, S> LruCache<K, V, S> {
#[inline]
pub fn with_hasher(capacity: usize, hash_builder: S) -> Self {
LruCache {
map: LinkedHashMap::with_hasher(hash_builder),
max_size: capacity,
}
}
#[inline]
pub fn capacity(&self) -> usize {
self.max_size
}
#[inline]
pub fn len(&self) -> usize {
self.map.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
#[inline]
pub fn clear(&mut self) {
self.map.clear();
}
#[inline]
pub fn iter(&self) -> Iter<K, V> {
self.map.iter()
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<K, V> {
self.map.iter_mut()
}
#[inline]
pub fn drain(&mut self) -> Drain<K, V> {
self.map.drain()
}
}
impl<K: Eq + Hash, V, S> LruCache<K, V, S>
where
S: BuildHasher,
{
#[inline]
pub fn contains_key<Q>(&mut self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.get_mut(key).is_some()
}
/// Insert a new value into the `LruCache`.
///
/// If necessary, will remove the value at the front of the LRU list to make room.
#[inline]
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
let old_val = self.map.insert(k, v);
if self.len() > self.capacity() {
self.remove_lru();
}
old_val
}
/// Get the value for the given key, *without* marking the value as recently used and moving it
/// to the back of the LRU list.
#[inline]
pub fn peek<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.get(k)
}
/// Get the value for the given key mutably, *without* marking the value as recently used and
/// moving it to the back of the LRU list.
#[inline]
pub fn peek_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.get_mut(k)
}
/// Retrieve the given key, marking it as recently used and moving it to the back of the LRU
/// list.
#[inline]
pub fn get<Q>(&mut self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.get_mut(k).map(|v| &*v)
}
/// Retrieve the given key, marking it as recently used and moving it to the back of the LRU
/// list.
#[inline]
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.map.raw_entry_mut().from_key(k) {
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
occupied.to_back();
Some(occupied.into_mut())
}
linked_hash_map::RawEntryMut::Vacant(_) => None,
}
}
/// If the returned entry is vacant, it will always have room to insert a single value. By
/// using the entry API, you can exceed the configured capacity by 1.
///
/// The returned entry is not automatically moved to the back of the LRU list. By calling
/// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
/// the LRU list.
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S> {
if self.len() > self.capacity() {
self.remove_lru();
}
self.map.entry(key)
}
/// The constructed raw entry is never automatically moved to the back of the LRU list. By
/// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
/// entry in the LRU list.
#[inline]
pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S> {
self.map.raw_entry()
}
/// If the constructed raw entry is vacant, it will always have room to insert a single value.
/// By using the raw entry API, you can exceed the configured capacity by 1.
///
/// The constructed raw entry is never automatically moved to the back of the LRU list. By
/// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
/// entry in the LRU list.
#[inline]
pub fn raw_entry_mut(&mut self) -> RawEntryBuilderMut<'_, K, V, S> {
if self.len() > self.capacity() {
self.remove_lru();
}
self.map.raw_entry_mut()
}
#[inline]
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.remove(k)
}
#[inline]
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.remove_entry(k)
}
/// Set the new cache capacity for the `LruCache`.
///
/// If there are more entries in the `LruCache` than the new capacity will allow, they are
/// removed.
#[inline]
pub fn set_capacity(&mut self, capacity: usize) {
for _ in capacity..self.len() {
self.remove_lru();
}
self.max_size = capacity;
}
/// Remove the least recently used entry and return it.
///
/// If the `LruCache` is empty this will return None.
#[inline]
pub fn remove_lru(&mut self) -> Option<(K, V)> {
self.map.pop_front()
}
}
impl<K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone> Clone for LruCache<K, V, S> {
#[inline]
fn clone(&self) -> Self {
LruCache {
map: self.map.clone(),
max_size: self.max_size,
}
}
}
impl<K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for LruCache<K, V, S> {
#[inline]
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (k, v) in iter {
self.insert(k, v);
}
}
}
impl<K, V, S> IntoIterator for LruCache<K, V, S> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
#[inline]
fn into_iter(self) -> IntoIter<K, V> {
self.map.into_iter()
}
}
impl<'a, K, V, S> IntoIterator for &'a LruCache<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
#[inline]
fn into_iter(self) -> Iter<'a, K, V> {
self.iter()
}
}
impl<'a, K, V, S> IntoIterator for &'a mut LruCache<K, V, S> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
#[inline]
fn into_iter(self) -> IterMut<'a, K, V> {
self.iter_mut()
}
}
impl<K, V, S> fmt::Debug for LruCache<K, V, S>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self.iter().rev()).finish()
}
}

161
third-party/vendor/hashlink/src/serde.rs vendored Normal file
View file

@ -0,0 +1,161 @@
use core::{
fmt::{self, Formatter},
hash::{BuildHasher, Hash},
marker::PhantomData,
};
use serde::{
de::{MapAccess, SeqAccess, Visitor},
ser::{SerializeMap, SerializeSeq},
Deserialize, Deserializer, Serialize, Serializer,
};
use crate::{LinkedHashMap, LinkedHashSet};
// LinkedHashMap impls
impl<K, V, S> Serialize for LinkedHashMap<K, V, S>
where
K: Serialize + Eq + Hash,
V: Serialize,
S: BuildHasher,
{
#[inline]
fn serialize<T: Serializer>(&self, serializer: T) -> Result<T::Ok, T::Error> {
let mut map_serializer = serializer.serialize_map(Some(self.len()))?;
for (k, v) in self {
map_serializer.serialize_key(k)?;
map_serializer.serialize_value(v)?;
}
map_serializer.end()
}
}
impl<'de, K, V, S> Deserialize<'de> for LinkedHashMap<K, V, S>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
S: BuildHasher + Default,
{
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Debug)]
pub struct LinkedHashMapVisitor<K, V, S> {
marker: PhantomData<LinkedHashMap<K, V, S>>,
}
impl<K, V, S> LinkedHashMapVisitor<K, V, S> {
fn new() -> Self {
LinkedHashMapVisitor {
marker: PhantomData,
}
}
}
impl<K, V, S> Default for LinkedHashMapVisitor<K, V, S> {
fn default() -> Self {
Self::new()
}
}
impl<'de, K, V, S> Visitor<'de> for LinkedHashMapVisitor<K, V, S>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
S: BuildHasher + Default,
{
type Value = LinkedHashMap<K, V, S>;
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "a map")
}
#[inline]
fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Self::Value, M::Error> {
let mut values = LinkedHashMap::with_capacity_and_hasher(
map.size_hint().unwrap_or(0),
S::default(),
);
while let Some((k, v)) = map.next_entry()? {
values.insert(k, v);
}
Ok(values)
}
}
deserializer.deserialize_map(LinkedHashMapVisitor::default())
}
}
// LinkedHashSet impls
impl<T, S> Serialize for LinkedHashSet<T, S>
where
T: Serialize + Eq + Hash,
S: BuildHasher,
{
#[inline]
fn serialize<U: Serializer>(&self, serializer: U) -> Result<U::Ok, U::Error> {
let mut seq_serializer = serializer.serialize_seq(Some(self.len()))?;
for v in self {
seq_serializer.serialize_element(v)?;
}
seq_serializer.end()
}
}
impl<'de, T, S> Deserialize<'de> for LinkedHashSet<T, S>
where
T: Deserialize<'de> + Eq + Hash,
S: BuildHasher + Default,
{
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Debug)]
pub struct LinkedHashSetVisitor<T, S> {
marker: PhantomData<LinkedHashSet<T, S>>,
}
impl<T, S> LinkedHashSetVisitor<T, S> {
fn new() -> Self {
LinkedHashSetVisitor {
marker: PhantomData,
}
}
}
impl<T, S> Default for LinkedHashSetVisitor<T, S> {
fn default() -> Self {
Self::new()
}
}
impl<'de, T, S> Visitor<'de> for LinkedHashSetVisitor<T, S>
where
T: Deserialize<'de> + Eq + Hash,
S: BuildHasher + Default,
{
type Value = LinkedHashSet<T, S>;
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "a sequence")
}
#[inline]
fn visit_seq<SA: SeqAccess<'de>>(self, mut seq: SA) -> Result<Self::Value, SA::Error> {
let mut values = LinkedHashSet::with_capacity_and_hasher(
seq.size_hint().unwrap_or(0),
S::default(),
);
while let Some(v) = seq.next_element()? {
values.insert(v);
}
Ok(values)
}
}
deserializer.deserialize_seq(LinkedHashSetVisitor::default())
}
}

View file

@ -0,0 +1,563 @@
use hashlink::{linked_hash_map, LinkedHashMap};
#[allow(dead_code)]
fn assert_covariance() {
fn set<'new>(v: LinkedHashMap<&'static str, ()>) -> LinkedHashMap<&'new str, ()> {
v
}
fn iter<'a, 'new>(
v: linked_hash_map::Iter<'a, &'static str, &'static str>,
) -> linked_hash_map::Iter<'a, &'new str, &'new str> {
v
}
fn iter_mut<'a, 'new>(
v: linked_hash_map::Iter<'a, &'static str, ()>,
) -> linked_hash_map::Iter<'a, &'new str, ()> {
v
}
fn into_iter<'new>(
v: linked_hash_map::IntoIter<&'static str, &'static str>,
) -> linked_hash_map::IntoIter<&'new str, &'new str> {
v
}
fn drain<'new>(
d: linked_hash_map::Drain<'static, &'static str, &'static str>,
) -> linked_hash_map::Drain<'new, &'new str, &'new str> {
d
}
fn raw_entry_builder<'a, 'new>(
v: linked_hash_map::RawEntryBuilder<'a, &'static str, &'static str, ()>,
) -> linked_hash_map::RawEntryBuilder<'a, &'new str, &'new str, ()> {
v
}
}
#[test]
fn test_index() {
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(10, map[&1]);
map[&2] = 22;
assert_eq!(22, map[&2]);
}
#[test]
fn test_insert_and_get() {
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.get(&1), Some(&10));
assert_eq!(map.get(&2), Some(&20));
assert_eq!(map.len(), 2);
}
#[test]
fn test_insert_update() {
let mut map = LinkedHashMap::new();
map.insert("1".to_string(), vec![10, 10]);
map.insert("1".to_string(), vec![10, 19]);
assert_eq!(map.get(&"1".to_string()), Some(&vec![10, 19]));
assert_eq!(map.len(), 1);
}
#[test]
fn test_entry_insert_vacant() {
let mut map = LinkedHashMap::new();
match map.entry("1".to_string()) {
linked_hash_map::Entry::Vacant(e) => {
assert_eq!(*e.insert(vec![10, 10]), vec![10, 10]);
}
_ => panic!("fail"),
}
assert!(map.contains_key("1"));
assert_eq!(map["1"], vec![10, 10]);
match map.entry("1".to_string()) {
linked_hash_map::Entry::Occupied(mut e) => {
assert_eq!(*e.get(), vec![10, 10]);
assert_eq!(e.insert(vec![10, 16]), vec![10, 10]);
}
_ => panic!("fail"),
}
assert!(map.contains_key("1"));
assert_eq!(map["1"], vec![10, 16]);
match map.entry("1".to_string()) {
linked_hash_map::Entry::Occupied(e) => {
assert_eq!(e.remove(), vec![10, 16]);
}
_ => panic!("fail"),
}
}
#[test]
fn test_remove() {
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.insert(3, 30);
map.insert(4, 40);
map.insert(5, 50);
map.remove(&3);
map.remove(&4);
assert!(map.get(&3).is_none());
assert!(map.get(&4).is_none());
map.insert(6, 60);
map.insert(7, 70);
map.insert(8, 80);
assert_eq!(map.get(&6), Some(&60));
assert_eq!(map.get(&7), Some(&70));
assert_eq!(map.get(&8), Some(&80));
}
#[test]
fn test_pop() {
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.insert(3, 30);
map.insert(4, 40);
map.insert(5, 50);
assert_eq!(map.pop_front(), Some((1, 10)));
assert!(map.get(&1).is_none());
assert_eq!(map.pop_back(), Some((5, 50)));
assert!(map.get(&5).is_none());
map.insert(6, 60);
map.insert(7, 70);
map.insert(8, 80);
assert_eq!(map.pop_front(), Some((2, 20)));
assert!(map.get(&2).is_none());
assert_eq!(map.pop_back(), Some((8, 80)));
assert!(map.get(&8).is_none());
map.insert(3, 30);
assert_eq!(map.pop_front(), Some((4, 40)));
assert!(map.get(&4).is_none());
assert_eq!(map.pop_back(), Some((3, 30)));
assert!(map.get(&3).is_none());
}
#[test]
fn test_move() {
let to_back = |map: &mut LinkedHashMap<_, _>, key| match map.entry(key) {
linked_hash_map::Entry::Occupied(mut occupied) => occupied.to_back(),
linked_hash_map::Entry::Vacant(_) => panic!(),
};
let to_front = |map: &mut LinkedHashMap<_, _>, key| match map.entry(key) {
linked_hash_map::Entry::Occupied(mut occupied) => occupied.to_front(),
linked_hash_map::Entry::Vacant(_) => panic!(),
};
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.insert(3, 30);
map.insert(4, 40);
map.insert(5, 50);
to_back(&mut map, 1);
assert_eq!(map.keys().copied().collect::<Vec<_>>(), vec![2, 3, 4, 5, 1]);
to_front(&mut map, 4);
assert_eq!(map.keys().copied().collect::<Vec<_>>(), vec![4, 2, 3, 5, 1]);
to_back(&mut map, 3);
assert_eq!(map.keys().copied().collect::<Vec<_>>(), vec![4, 2, 5, 1, 3]);
to_front(&mut map, 2);
assert_eq!(map.keys().copied().collect::<Vec<_>>(), vec![2, 4, 5, 1, 3]);
to_back(&mut map, 3);
assert_eq!(map.keys().copied().collect::<Vec<_>>(), vec![2, 4, 5, 1, 3]);
to_front(&mut map, 2);
assert_eq!(map.keys().copied().collect::<Vec<_>>(), vec![2, 4, 5, 1, 3]);
}
#[test]
fn test_clear() {
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.clear();
assert!(map.get(&1).is_none());
assert!(map.get(&2).is_none());
assert!(map.is_empty());
}
#[test]
fn test_iter() {
let mut map = LinkedHashMap::new();
// empty iter
assert_eq!(None, map.iter().next());
map.insert("a", 10);
map.insert("b", 20);
map.insert("c", 30);
// regular iter
let mut iter = map.iter();
assert_eq!((&"a", &10), iter.next().unwrap());
assert_eq!((&"b", &20), iter.next().unwrap());
assert_eq!((&"c", &30), iter.next().unwrap());
assert_eq!(None, iter.next());
assert_eq!(None, iter.next());
let mut iter = map.iter();
assert_eq!((&"a", &10), iter.next().unwrap());
let mut iclone = iter.clone();
assert_eq!((&"b", &20), iter.next().unwrap());
assert_eq!((&"b", &20), iclone.next().unwrap());
assert_eq!((&"c", &30), iter.next().unwrap());
assert_eq!((&"c", &30), iclone.next().unwrap());
// reversed iter
let mut rev_iter = map.iter().rev();
assert_eq!((&"c", &30), rev_iter.next().unwrap());
assert_eq!((&"b", &20), rev_iter.next().unwrap());
assert_eq!((&"a", &10), rev_iter.next().unwrap());
assert_eq!(None, rev_iter.next());
assert_eq!(None, rev_iter.next());
// mixed
let mut mixed_iter = map.iter();
assert_eq!((&"a", &10), mixed_iter.next().unwrap());
assert_eq!((&"c", &30), mixed_iter.next_back().unwrap());
assert_eq!((&"b", &20), mixed_iter.next().unwrap());
assert_eq!(None, mixed_iter.next());
assert_eq!(None, mixed_iter.next_back());
}
#[test]
fn test_borrow() {
#[derive(PartialEq, Eq, Hash)]
struct Foo(Bar);
#[derive(PartialEq, Eq, Hash)]
struct Bar(i32);
impl ::std::borrow::Borrow<Bar> for Foo {
fn borrow(&self) -> &Bar {
&self.0
}
}
let mut map = LinkedHashMap::new();
map.insert(Foo(Bar(1)), "a");
map.insert(Foo(Bar(2)), "b");
assert!(map.contains_key(&Bar(1)));
assert!(map.contains_key(&Bar(2)));
assert!(map.contains_key(&Foo(Bar(1))));
assert!(map.contains_key(&Foo(Bar(2))));
assert_eq!(map.get(&Bar(1)), Some(&"a"));
assert_eq!(map.get(&Bar(2)), Some(&"b"));
assert_eq!(map.get(&Foo(Bar(1))), Some(&"a"));
assert_eq!(map.get(&Foo(Bar(2))), Some(&"b"));
assert_eq!(map.get_mut(&Bar(1)), Some(&mut "a"));
assert_eq!(map.get_mut(&Bar(2)), Some(&mut "b"));
assert_eq!(map.get_mut(&Foo(Bar(1))), Some(&mut "a"));
assert_eq!(map.get_mut(&Foo(Bar(2))), Some(&mut "b"));
assert_eq!(map[&Bar(1)], "a");
assert_eq!(map[&Bar(2)], "b");
assert_eq!(map[&Foo(Bar(1))], "a");
assert_eq!(map[&Foo(Bar(2))], "b");
assert_eq!(map.remove(&Bar(1)), Some("a"));
assert_eq!(map.remove(&Bar(2)), Some("b"));
assert_eq!(map.remove(&Foo(Bar(1))), None);
assert_eq!(map.remove(&Foo(Bar(2))), None);
}
#[test]
fn test_iter_mut() {
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
{
let mut iter = map.iter_mut();
let entry = iter.next().unwrap();
assert_eq!("a", *entry.0);
*entry.1 = 17;
assert_eq!(format!("{:?}", iter), "[(\"c\", 30), (\"b\", 20)]");
// reverse iterator
let mut iter = iter.rev();
let entry = iter.next().unwrap();
assert_eq!("b", *entry.0);
*entry.1 = 23;
let entry = iter.next().unwrap();
assert_eq!("c", *entry.0);
assert_eq!(None, iter.next());
assert_eq!(None, iter.next());
}
assert_eq!(17, map[&"a"]);
assert_eq!(23, map[&"b"]);
}
#[test]
fn test_consuming_iter() {
let map = {
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
map
};
let mut iter = map.into_iter();
assert_eq!(Some(("a", 10)), iter.next());
assert_eq!(Some(("b", 20)), iter.next_back());
assert_eq!(iter.len(), 1);
assert_eq!(format!("{:?}", iter), "[(\"c\", 30)]");
assert_eq!(Some(("c", 30)), iter.next());
assert_eq!(None, iter.next());
}
#[test]
fn test_consuming_iter_empty() {
let map = LinkedHashMap::<&str, i32>::new();
let mut iter = map.into_iter();
assert_eq!(None, iter.next());
}
#[test]
fn test_consuming_iter_with_free_list() {
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
map.remove("a");
map.remove("b");
let mut iter = map.into_iter();
assert_eq!(Some(("c", 30)), iter.next());
assert_eq!(None, iter.next());
}
#[test]
fn test_into_iter_drop() {
struct Counter<'a>(&'a mut usize);
impl<'a> Drop for Counter<'a> {
fn drop(&mut self) {
*self.0 += 1;
}
}
let mut a = 0;
let mut b = 0;
let mut c = 0;
{
let mut map = LinkedHashMap::new();
map.insert("a", Counter(&mut a));
map.insert("b", Counter(&mut b));
map.insert("c", Counter(&mut c));
let mut iter = map.into_iter();
assert_eq!(iter.next().map(|p| p.0), Some("a"));
assert_eq!(iter.next_back().map(|p| p.0), Some("c"));
}
assert_eq!(a, 1);
assert_eq!(b, 1);
assert_eq!(c, 1);
}
#[test]
fn test_drain() {
use std::{cell::Cell, rc::Rc};
struct Counter(Rc<Cell<u32>>);
impl<'a> Drop for Counter {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let mut map = LinkedHashMap::new();
let a = Rc::new(Cell::new(0));
let b = Rc::new(Cell::new(0));
let c = Rc::new(Cell::new(0));
map.insert("a", Counter(a.clone()));
map.insert("b", Counter(b.clone()));
map.insert("c", Counter(c.clone()));
let mut iter = map.drain();
assert_eq!(iter.next().map(|p| p.0), Some("a"));
assert_eq!(iter.next_back().map(|p| p.0), Some("c"));
assert_eq!(iter.next_back().map(|p| p.0), Some("b"));
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
drop(iter);
assert_eq!(map.len(), 0);
assert_eq!(a.get(), 1);
assert_eq!(b.get(), 1);
assert_eq!(c.get(), 1);
map.insert("a", Counter(a.clone()));
map.insert("b", Counter(b.clone()));
map.insert("c", Counter(c.clone()));
let mut iter = map.drain();
assert_eq!(iter.next().map(|p| p.0), Some("a"));
assert_eq!(iter.next().map(|p| p.0), Some("b"));
assert_eq!(iter.next_back().map(|p| p.0), Some("c"));
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
drop(iter);
assert_eq!(map.len(), 0);
assert_eq!(a.get(), 2);
assert_eq!(b.get(), 2);
assert_eq!(c.get(), 2);
map.insert("a", Counter(a.clone()));
map.insert("b", Counter(b.clone()));
map.insert("c", Counter(c.clone()));
map.drain();
assert_eq!(map.len(), 0);
assert_eq!(a.get(), 3);
assert_eq!(b.get(), 3);
assert_eq!(c.get(), 3);
}
#[test]
fn test_send_sync() {
fn is_send_sync<T: Send + Sync>() {}
is_send_sync::<LinkedHashMap<u32, i32>>();
is_send_sync::<linked_hash_map::Entry<u32, i32, ()>>();
is_send_sync::<linked_hash_map::RawEntryBuilder<u32, i32, ()>>();
is_send_sync::<linked_hash_map::RawEntryBuilderMut<u32, i32, ()>>();
is_send_sync::<linked_hash_map::RawEntryMut<u32, i32, ()>>();
is_send_sync::<linked_hash_map::Iter<u32, i32>>();
is_send_sync::<linked_hash_map::IterMut<u32, i32>>();
is_send_sync::<linked_hash_map::Drain<u32, i32>>();
is_send_sync::<linked_hash_map::Keys<u32, i32>>();
is_send_sync::<linked_hash_map::Values<u32, i32>>();
}
#[test]
fn test_retain() {
use std::{cell::Cell, rc::Rc};
let xs = [1, 2, 3, 4, 5, 6];
let mut map: LinkedHashMap<String, i32> = xs.iter().map(|i| (i.to_string(), *i)).collect();
map.retain(|_, v| *v % 2 == 0);
assert_eq!(map.len(), 3);
assert!(map.contains_key("2"));
assert!(map.contains_key("4"));
assert!(map.contains_key("6"));
struct Counter(Rc<Cell<u32>>);
impl<'a> Drop for Counter {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let c = Rc::new(Cell::new(0));
let mut map = LinkedHashMap::new();
map.insert(1, Counter(Rc::clone(&c)));
map.insert(2, Counter(Rc::clone(&c)));
map.insert(3, Counter(Rc::clone(&c)));
map.insert(4, Counter(Rc::clone(&c)));
map.retain(|k, _| *k % 2 == 0);
assert!(c.get() == 2);
drop(map);
assert!(c.get() == 4);
}
#[test]
fn test_order_equality() {
let xs = [1, 2, 3, 4, 5, 6];
let mut map1: LinkedHashMap<String, i32> = xs.iter().map(|i| (i.to_string(), *i)).collect();
let mut map2: LinkedHashMap<String, i32> = xs.iter().map(|i| (i.to_string(), *i)).collect();
assert_eq!(map1, map2);
map1.to_front("4");
assert_ne!(map1, map2);
map2.to_front("4");
assert_eq!(map1, map2);
}
#[test]
fn test_replace() {
let mut map = LinkedHashMap::new();
map.insert(1, 1);
map.insert(2, 2);
map.insert(3, 3);
map.insert(4, 4);
assert!(map
.iter()
.map(|(k, v)| (*k, *v))
.eq([(1, 1), (2, 2), (3, 3), (4, 4)].iter().copied()));
map.insert(3, 5);
assert!(map
.iter()
.map(|(k, v)| (*k, *v))
.eq([(1, 1), (2, 2), (4, 4), (3, 5)].iter().copied()));
map.replace(2, 6);
assert!(map
.iter()
.map(|(k, v)| (*k, *v))
.eq([(1, 1), (2, 6), (4, 4), (3, 5)].iter().copied()));
}
#[test]
fn test_shrink_to_fit_resize() {
let mut map = LinkedHashMap::new();
map.shrink_to_fit();
for i in 0..100 {
map.insert(i, i);
}
map.shrink_to_fit();
for _ in 0..50 {
map.pop_front();
map.shrink_to_fit();
}
assert_eq!(map.len(), 50);
for i in 50..100 {
assert_eq!(map.get(&i).unwrap(), &i);
}
}

View file

@ -0,0 +1,543 @@
use hashbrown::hash_map::DefaultHashBuilder;
use hashlink::linked_hash_set::{self, LinkedHashSet};
#[allow(dead_code)]
fn assert_covariance() {
fn set<'new>(v: LinkedHashSet<&'static str>) -> LinkedHashSet<&'new str> {
v
}
fn iter<'a, 'new>(
v: linked_hash_set::Iter<'a, &'static str>,
) -> linked_hash_set::Iter<'a, &'new str> {
v
}
fn into_iter<'new>(
v: linked_hash_set::IntoIter<&'static str>,
) -> linked_hash_set::IntoIter<&'new str> {
v
}
fn difference<'a, 'new>(
v: linked_hash_set::Difference<'a, &'static str, DefaultHashBuilder>,
) -> linked_hash_set::Difference<'a, &'new str, DefaultHashBuilder> {
v
}
fn symmetric_difference<'a, 'new>(
v: linked_hash_set::SymmetricDifference<'a, &'static str, DefaultHashBuilder>,
) -> linked_hash_set::SymmetricDifference<'a, &'new str, DefaultHashBuilder> {
v
}
fn intersection<'a, 'new>(
v: linked_hash_set::Intersection<'a, &'static str, DefaultHashBuilder>,
) -> linked_hash_set::Intersection<'a, &'new str, DefaultHashBuilder> {
v
}
fn union<'a, 'new>(
v: linked_hash_set::Union<'a, &'static str, DefaultHashBuilder>,
) -> linked_hash_set::Union<'a, &'new str, DefaultHashBuilder> {
v
}
fn drain<'new>(
d: linked_hash_set::Drain<'static, &'static str>,
) -> linked_hash_set::Drain<'new, &'new str> {
d
}
}
#[test]
fn test_zero_capacities() {
type HS = LinkedHashSet<i32>;
let s = HS::new();
assert_eq!(s.capacity(), 0);
let s = HS::default();
assert_eq!(s.capacity(), 0);
let s = HS::with_hasher(DefaultHashBuilder::default());
assert_eq!(s.capacity(), 0);
let s = HS::with_capacity(0);
assert_eq!(s.capacity(), 0);
let s = HS::with_capacity_and_hasher(0, DefaultHashBuilder::default());
assert_eq!(s.capacity(), 0);
let mut s = HS::new();
s.insert(1);
s.insert(2);
s.remove(&1);
s.remove(&2);
s.shrink_to_fit();
assert_eq!(s.capacity(), 0);
let mut s = HS::new();
s.reserve(0);
assert_eq!(s.capacity(), 0);
}
#[test]
fn test_disjoint() {
let mut xs = LinkedHashSet::new();
let mut ys = LinkedHashSet::new();
assert!(xs.is_disjoint(&ys));
assert!(ys.is_disjoint(&xs));
assert!(xs.insert(5));
assert!(ys.insert(11));
assert!(xs.is_disjoint(&ys));
assert!(ys.is_disjoint(&xs));
assert!(xs.insert(7));
assert!(xs.insert(19));
assert!(xs.insert(4));
assert!(ys.insert(2));
assert!(ys.insert(-11));
assert!(xs.is_disjoint(&ys));
assert!(ys.is_disjoint(&xs));
assert!(ys.insert(7));
assert!(!xs.is_disjoint(&ys));
assert!(!ys.is_disjoint(&xs));
}
#[test]
fn test_subset_and_superset() {
let mut a = LinkedHashSet::new();
assert!(a.insert(0));
assert!(a.insert(5));
assert!(a.insert(11));
assert!(a.insert(7));
let mut b = LinkedHashSet::new();
assert!(b.insert(0));
assert!(b.insert(7));
assert!(b.insert(19));
assert!(b.insert(250));
assert!(b.insert(11));
assert!(b.insert(200));
assert!(!a.is_subset(&b));
assert!(!a.is_superset(&b));
assert!(!b.is_subset(&a));
assert!(!b.is_superset(&a));
assert!(b.insert(5));
assert!(a.is_subset(&b));
assert!(!a.is_superset(&b));
assert!(!b.is_subset(&a));
assert!(b.is_superset(&a));
}
#[test]
fn test_iterate() {
let mut a = LinkedHashSet::new();
for i in 0..32 {
assert!(a.insert(i));
}
let mut observed: u32 = 0;
for k in &a {
observed |= 1 << *k;
}
assert_eq!(observed, 0xFFFF_FFFF);
}
#[test]
fn test_intersection() {
let mut a = LinkedHashSet::new();
let mut b = LinkedHashSet::new();
assert!(a.insert(11));
assert!(a.insert(1));
assert!(a.insert(3));
assert!(a.insert(77));
assert!(a.insert(103));
assert!(a.insert(5));
assert!(a.insert(-5));
assert!(b.insert(2));
assert!(b.insert(11));
assert!(b.insert(77));
assert!(b.insert(-9));
assert!(b.insert(-42));
assert!(b.insert(5));
assert!(b.insert(3));
let mut i = 0;
let expected = [3, 5, 11, 77];
for x in a.intersection(&b) {
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
}
#[test]
fn test_difference() {
let mut a = LinkedHashSet::new();
let mut b = LinkedHashSet::new();
assert!(a.insert(1));
assert!(a.insert(3));
assert!(a.insert(5));
assert!(a.insert(9));
assert!(a.insert(11));
assert!(b.insert(3));
assert!(b.insert(9));
let mut i = 0;
let expected = [1, 5, 11];
for x in a.difference(&b) {
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
}
#[test]
fn test_symmetric_difference() {
let mut a = LinkedHashSet::new();
let mut b = LinkedHashSet::new();
assert!(a.insert(1));
assert!(a.insert(3));
assert!(a.insert(5));
assert!(a.insert(9));
assert!(a.insert(11));
assert!(b.insert(-2));
assert!(b.insert(3));
assert!(b.insert(9));
assert!(b.insert(14));
assert!(b.insert(22));
let mut i = 0;
let expected = [-2, 1, 5, 11, 14, 22];
for x in a.symmetric_difference(&b) {
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
}
#[test]
fn test_union() {
let mut a = LinkedHashSet::new();
let mut b = LinkedHashSet::new();
assert!(a.insert(1));
assert!(a.insert(3));
assert!(a.insert(5));
assert!(a.insert(9));
assert!(a.insert(11));
assert!(a.insert(16));
assert!(a.insert(19));
assert!(a.insert(24));
assert!(b.insert(-2));
assert!(b.insert(1));
assert!(b.insert(5));
assert!(b.insert(9));
assert!(b.insert(13));
assert!(b.insert(19));
let mut i = 0;
let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
for x in a.union(&b) {
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
}
#[test]
fn test_from_iter() {
let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let set: LinkedHashSet<_> = xs.iter().cloned().collect();
for x in &xs {
assert!(set.contains(x));
}
}
#[test]
fn test_move_iter() {
let hs = {
let mut hs = LinkedHashSet::new();
hs.insert('a');
hs.insert('b');
hs
};
let v = hs.into_iter().collect::<Vec<char>>();
assert!(v == ['a', 'b'] || v == ['b', 'a']);
}
#[test]
fn test_eq() {
let mut s1 = LinkedHashSet::new();
s1.insert(1);
s1.insert(2);
s1.insert(3);
let mut s2 = LinkedHashSet::new();
s2.insert(1);
s2.insert(2);
assert!(s1 != s2);
s2.insert(3);
assert_eq!(s1, s2);
}
#[test]
fn test_show() {
let mut set = LinkedHashSet::new();
let empty = LinkedHashSet::<i32>::new();
set.insert(1);
set.insert(2);
let set_str = format!("{:?}", set);
assert!(set_str == "{1, 2}" || set_str == "{2, 1}");
assert_eq!(format!("{:?}", empty), "{}");
}
#[test]
fn test_trivial_drain() {
let mut s = LinkedHashSet::<i32>::new();
for _ in s.drain() {}
assert!(s.is_empty());
drop(s);
let mut s = LinkedHashSet::<i32>::new();
drop(s.drain());
assert!(s.is_empty());
}
#[test]
fn test_drain() {
let mut s: LinkedHashSet<_> = (1..100).collect();
for _ in 0..20 {
assert_eq!(s.len(), 99);
{
let mut last_i = 0;
let mut d = s.drain();
for (i, x) in d.by_ref().take(50).enumerate() {
last_i = i;
assert!(x != 0);
}
assert_eq!(last_i, 49);
}
for _ in &s {
panic!("s should be empty!");
}
s.extend(1..100);
}
}
#[test]
fn test_replace() {
use core::hash;
#[derive(Debug)]
struct Foo(&'static str, i32);
impl PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for Foo {}
impl hash::Hash for Foo {
fn hash<H: hash::Hasher>(&self, h: &mut H) {
self.0.hash(h);
}
}
let mut s = LinkedHashSet::new();
assert_eq!(s.replace(Foo("a", 1)), None);
assert_eq!(s.len(), 1);
assert_eq!(s.replace(Foo("a", 2)), Some(Foo("a", 1)));
assert_eq!(s.len(), 1);
let mut it = s.iter();
assert_eq!(it.next(), Some(&Foo("a", 2)));
assert_eq!(it.next(), None);
}
#[test]
fn test_extend_ref() {
let mut a = LinkedHashSet::new();
a.insert(1);
a.extend(&[2, 3, 4]);
assert_eq!(a.len(), 4);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(a.contains(&3));
assert!(a.contains(&4));
let mut b = LinkedHashSet::new();
b.insert(5);
b.insert(6);
a.extend(&b);
assert_eq!(a.len(), 6);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(a.contains(&3));
assert!(a.contains(&4));
assert!(a.contains(&5));
assert!(a.contains(&6));
}
#[test]
fn test_retain() {
let xs = [1, 2, 3, 4, 5, 6];
let mut set: LinkedHashSet<i32> = xs.iter().cloned().collect();
set.retain(|&k| k % 2 == 0);
assert_eq!(set.len(), 3);
assert!(set.contains(&2));
assert!(set.contains(&4));
assert!(set.contains(&6));
}
#[test]
fn test_retain_with_order() {
let xs = [1, 2, 3, 4, 5, 6];
let mut set: LinkedHashSet<i32> = xs.iter().cloned().collect();
let mut vec = Vec::new();
set.retain_with_order(|&k| {
if k % 2 == 0 {
true
} else {
vec.push(k);
false
}
});
assert_eq!(vec![1, 3, 5], vec);
}
#[test]
fn insert_order() {
let mut set = LinkedHashSet::new();
set.insert(1);
set.insert(2);
set.insert(3);
set.insert(4);
assert_eq!(
set.clone().into_iter().collect::<Vec<_>>(),
vec![1, 2, 3, 4]
);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![1, 2, 3, 4]);
}
#[test]
fn front_back() {
let mut set = LinkedHashSet::new();
set.insert(1);
set.insert(2);
set.insert(3);
set.insert(4);
assert_eq!(set.front(), Some(&1));
assert_eq!(set.back(), Some(&4));
assert_eq!(set.pop_back(), Some(4));
assert_eq!(set.back(), Some(&3));
assert_eq!(set.pop_front(), Some(1));
assert_eq!(set.front(), Some(&2));
}
#[test]
fn double_ended_iter() {
let mut set = LinkedHashSet::new();
set.insert(1);
set.insert(2);
set.insert(3);
set.insert(4);
let mut iter = set.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next_back(), Some(&4));
assert_eq!(iter.next_back(), Some(&3));
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
drop(iter);
let mut iter = set.drain();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next_back(), Some(4));
assert_eq!(iter.next_back(), Some(3));
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
drop(iter);
set.insert(1);
set.insert(2);
set.insert(3);
set.insert(4);
let mut iter = set.into_iter();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next_back(), Some(4));
assert_eq!(iter.next_back(), Some(3));
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
}
#[test]
fn to_back_front_order() {
let mut set = LinkedHashSet::new();
set.insert(1);
set.insert(2);
set.insert(3);
set.insert(4);
assert_eq!(set.back().copied(), Some(4));
assert_eq!(set.front().copied(), Some(1));
set.to_back(&2);
assert_eq!(set.back().copied(), Some(2));
set.to_front(&3);
assert_eq!(set.front().copied(), Some(3));
}
#[test]
fn test_order_equality() {
let xs = [1, 2, 3, 4, 5, 6];
let mut set1: LinkedHashSet<i32> = xs.iter().copied().collect();
let mut set2: LinkedHashSet<i32> = xs.iter().copied().collect();
assert_eq!(set1, set2);
set1.to_front(&4);
assert_ne!(set1, set2);
set2.to_front(&4);
assert_eq!(set1, set2);
}

View file

@ -0,0 +1,166 @@
use hashlink::LruCache;
#[test]
fn test_put_and_get() {
let mut cache = LruCache::new(2);
cache.insert(1, 10);
cache.insert(2, 20);
assert_eq!(cache.get_mut(&1), Some(&mut 10));
assert_eq!(cache.get_mut(&2), Some(&mut 20));
assert_eq!(cache.len(), 2);
}
#[test]
fn test_put_update() {
let mut cache = LruCache::new(1);
cache.insert("1", 10);
cache.insert("1", 19);
assert_eq!(cache.get_mut("1"), Some(&mut 19));
assert_eq!(cache.len(), 1);
}
#[test]
fn test_contains_key() {
let mut cache = LruCache::new(1);
cache.insert("1", 10);
assert_eq!(cache.contains_key("1"), true);
}
#[test]
fn test_expire_lru() {
let mut cache = LruCache::new(2);
cache.insert("foo1", "bar1");
cache.insert("foo2", "bar2");
cache.insert("foo3", "bar3");
assert!(cache.get_mut("foo1").is_none());
cache.insert("foo2", "bar2update");
cache.insert("foo4", "bar4");
assert!(cache.get_mut("foo3").is_none());
}
#[test]
fn test_pop() {
let mut cache = LruCache::new(2);
cache.insert(1, 10);
cache.insert(2, 20);
assert_eq!(cache.len(), 2);
let opt1 = cache.remove(&1);
assert!(opt1.is_some());
assert_eq!(opt1.unwrap(), 10);
assert!(cache.get_mut(&1).is_none());
assert_eq!(cache.len(), 1);
}
#[test]
fn test_change_capacity() {
let mut cache = LruCache::new(2);
assert_eq!(cache.capacity(), 2);
cache.insert(1, 10);
cache.insert(2, 20);
cache.set_capacity(1);
assert!(cache.get_mut(&1).is_none());
assert_eq!(cache.capacity(), 1);
}
#[test]
fn test_remove() {
let mut cache = LruCache::new(3);
cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
cache.insert(4, 40);
cache.insert(5, 50);
cache.remove(&3);
cache.remove(&4);
assert!(cache.get_mut(&3).is_none());
assert!(cache.get_mut(&4).is_none());
cache.insert(6, 60);
cache.insert(7, 70);
cache.insert(8, 80);
assert!(cache.get_mut(&5).is_none());
assert_eq!(cache.get_mut(&6), Some(&mut 60));
assert_eq!(cache.get_mut(&7), Some(&mut 70));
assert_eq!(cache.get_mut(&8), Some(&mut 80));
}
#[test]
fn test_clear() {
let mut cache = LruCache::new(2);
cache.insert(1, 10);
cache.insert(2, 20);
cache.clear();
assert!(cache.get_mut(&1).is_none());
assert!(cache.get_mut(&2).is_none());
assert!(cache.is_empty())
}
#[test]
fn test_iter() {
let mut cache = LruCache::new(3);
cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
cache.insert(4, 40);
cache.insert(5, 50);
assert_eq!(
cache.iter().collect::<Vec<_>>(),
[(&3, &30), (&4, &40), (&5, &50)]
);
assert_eq!(
cache.iter_mut().collect::<Vec<_>>(),
[(&3, &mut 30), (&4, &mut 40), (&5, &mut 50)]
);
assert_eq!(
cache.iter().rev().collect::<Vec<_>>(),
[(&5, &50), (&4, &40), (&3, &30)]
);
assert_eq!(
cache.iter_mut().rev().collect::<Vec<_>>(),
[(&5, &mut 50), (&4, &mut 40), (&3, &mut 30)]
);
}
#[test]
fn test_peek() {
let mut cache = LruCache::new_unbounded();
cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
cache.insert(4, 40);
cache.insert(5, 50);
cache.insert(6, 60);
assert_eq!(cache.remove_lru(), Some((1, 10)));
assert_eq!(cache.peek(&2), Some(&20));
assert_eq!(cache.remove_lru(), Some((2, 20)));
assert_eq!(cache.peek_mut(&3), Some(&mut 30));
assert_eq!(cache.remove_lru(), Some((3, 30)));
assert_eq!(cache.get(&4), Some(&40));
assert_eq!(cache.remove_lru(), Some((5, 50)));
}
#[test]
fn test_entry() {
let mut cache = LruCache::new(4);
cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
cache.insert(4, 40);
cache.insert(5, 50);
cache.insert(6, 60);
assert_eq!(cache.len(), 4);
cache.entry(7).or_insert(70);
cache.entry(8).or_insert(80);
cache.entry(9).or_insert(90);
assert!(cache.len() <= 5);
cache.raw_entry_mut().from_key(&10).or_insert(10, 100);
cache.raw_entry_mut().from_key(&11).or_insert(11, 110);
cache.raw_entry_mut().from_key(&12).or_insert(12, 120);
assert!(cache.len() <= 5);
}

View file

@ -0,0 +1,110 @@
#![cfg(feature = "serde_impl")]
use std::hash::BuildHasherDefault;
use hashlink::{LinkedHashMap, LinkedHashSet};
use rustc_hash::FxHasher;
use serde_test::{assert_tokens, Token};
#[test]
fn map_serde_tokens_empty() {
let map = LinkedHashMap::<char, u32>::new();
assert_tokens(&map, &[Token::Map { len: Some(0) }, Token::MapEnd]);
}
#[test]
fn map_serde_tokens() {
let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('b', 20);
map.insert('c', 30);
assert_tokens(
&map,
&[
Token::Map { len: Some(3) },
Token::Char('a'),
Token::I32(10),
Token::Char('b'),
Token::I32(20),
Token::Char('c'),
Token::I32(30),
Token::MapEnd,
],
);
}
#[test]
fn map_serde_tokens_empty_generic() {
let map = LinkedHashMap::<char, u32, BuildHasherDefault<FxHasher>>::default();
assert_tokens(&map, &[Token::Map { len: Some(0) }, Token::MapEnd]);
}
#[test]
fn map_serde_tokens_generic() {
let mut map = LinkedHashMap::<char, i32, BuildHasherDefault<FxHasher>>::default();
map.insert('a', 10);
map.insert('b', 20);
map.insert('c', 30);
assert_tokens(
&map,
&[
Token::Map { len: Some(3) },
Token::Char('a'),
Token::I32(10),
Token::Char('b'),
Token::I32(20),
Token::Char('c'),
Token::I32(30),
Token::MapEnd,
],
);
}
#[test]
fn set_serde_tokens_empty() {
let set = LinkedHashSet::<u32>::new();
assert_tokens(&set, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
}
#[test]
fn set_serde_tokens() {
let mut set = LinkedHashSet::new();
set.insert(10);
set.insert(20);
set.insert(30);
assert_tokens(
&set,
&[
Token::Seq { len: Some(3) },
Token::I32(10),
Token::I32(20),
Token::I32(30),
Token::SeqEnd,
],
);
}
#[test]
fn set_serde_tokens_generic() {
let mut set = LinkedHashSet::<char, BuildHasherDefault<FxHasher>>::default();
set.insert('a');
set.insert('b');
set.insert('c');
assert_tokens(
&set,
&[
Token::Seq { len: Some(3) },
Token::Char('a'),
Token::Char('b'),
Token::Char('c'),
Token::SeqEnd,
],
);
}