Unverified Commit e3b14676 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

auto-hash-map: use GATs for borrowed storage iterators, move aliases to lib.rs

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 128b0cc0
...@@ -3,11 +3,37 @@ pub mod set; ...@@ -3,11 +3,37 @@ pub mod set;
pub mod storage; pub mod storage;
pub mod tiny_vec; pub mod tiny_vec;
pub use map::{AutoIndexMap, AutoMap, RawAutoMap}; use std::hash::BuildHasherDefault;
pub use set::{AutoIndexSet, AutoSet, RawAutoSet};
pub use storage::{BackingListStorage, BackingMapStorage}; use hashbrown::HashMap;
use indexmap::IndexMap;
pub use map::RawAutoMap;
use rustc_hash::FxHasher;
pub use set::RawAutoSet;
pub use storage::{BackingListStorage, BackingMapIter, BackingMapStorage};
pub use tiny_vec::TinyVec; pub use tiny_vec::TinyVec;
/// Unordered [`RawAutoMap`] backed by a [`HashMap`] and an `I`-inline [`TinyVec`].
pub type AutoMap<K, V, H = BuildHasherDefault<FxHasher>, const I: usize = 0> =
RawAutoMap<K, V, HashMap<K, V, H>, TinyVec<(K, V), I, MAX_USEFUL_LINEAR_SCAN>>;
/// Insertion ordered [`RawAutoMap`] backed by an [`IndexMap`] and an `I`-inline [`TinyVec`].
///
/// Equality is order sensitive and removals preserve the order of the remaining entries.
pub type AutoIndexMap<K, V, H = BuildHasherDefault<FxHasher>, const I: usize = 0> =
RawAutoMap<K, V, IndexMap<K, V, H>, TinyVec<(K, V), I, MAX_USEFUL_LINEAR_SCAN>>;
/// Unordered [`RawAutoSet`] backed by a [`HashMap`] and an `I`-inline [`TinyVec`].
pub type AutoSet<K, H = BuildHasherDefault<FxHasher>, const I: usize = 0> =
RawAutoSet<K, HashMap<K, (), H>, TinyVec<(K, ()), I, MAX_USEFUL_LINEAR_SCAN>>;
/// Insertion ordered [`RawAutoSet`] backed by an [`IndexMap`] and an `I`-inline [`TinyVec`].
///
/// Equality is order sensitive (`{a, b} != {b, a}`) and removals preserve the order of the
/// remaining items.
pub type AutoIndexSet<K, H = BuildHasherDefault<FxHasher>, const I: usize = 0> =
RawAutoSet<K, IndexMap<K, (), H>, TinyVec<(K, ()), I, MAX_USEFUL_LINEAR_SCAN>>;
// Values based on data from https://github.com/yegor256/micromap#benchmark // Values based on data from https://github.com/yegor256/micromap#benchmark
/// Maximum size of list variant. Must convert to HashMap when bigger. /// Maximum size of list variant. Must convert to HashMap when bigger.
......
...@@ -18,7 +18,7 @@ use shrink_to_fit::ShrinkToFit; ...@@ -18,7 +18,7 @@ use shrink_to_fit::ShrinkToFit;
use crate::{ use crate::{
MAX_USEFUL_LINEAR_SCAN, RawAutoMap, TinyVec, MAX_USEFUL_LINEAR_SCAN, RawAutoMap, TinyVec,
storage::{BackingListStorage, BackingMapStorage}, storage::{BackingListStorage, BackingMapIter, BackingMapStorage},
}; };
/// A set that stores small numbers of items in a linear list `L` and switches to a real map `M` /// A set that stores small numbers of items in a linear list `L` and switches to a real map `M`
...@@ -31,16 +31,8 @@ pub struct RawAutoSet<K, M, L> { ...@@ -31,16 +31,8 @@ pub struct RawAutoSet<K, M, L> {
map: RawAutoMap<K, (), M, L>, map: RawAutoMap<K, (), M, L>,
} }
/// Unordered [`RawAutoSet`] backed by a [`HashMap`] and an `I`-inline [`TinyVec`]. // The aliases are defined in the crate root. Re-exported here to keep the `set::` paths working.
pub type AutoSet<K, H = BuildHasherDefault<FxHasher>, const I: usize = 0> = pub use crate::{AutoIndexSet, AutoSet};
RawAutoSet<K, HashMap<K, (), H>, TinyVec<(K, ()), I, MAX_USEFUL_LINEAR_SCAN>>;
/// Insertion ordered [`RawAutoSet`] backed by an [`IndexMap`] and an `I`-inline [`TinyVec`].
///
/// Equality is order sensitive (`{a, b} != {b, a}`) and removals preserve the order of the
/// remaining items.
pub type AutoIndexSet<K, H = BuildHasherDefault<FxHasher>, const I: usize = 0> =
RawAutoSet<K, IndexMap<K, (), H>, TinyVec<(K, ()), I, MAX_USEFUL_LINEAR_SCAN>>;
impl<K, M, L: BackingListStorage<(K, ())>> Default for RawAutoSet<K, M, L> { impl<K, M, L: BackingListStorage<(K, ())>> Default for RawAutoSet<K, M, L> {
fn default() -> Self { fn default() -> Self {
...@@ -166,7 +158,7 @@ impl<K, M: BackingMapStorage<K, ()>, L: BackingListStorage<(K, ())>> RawAutoSet< ...@@ -166,7 +158,7 @@ impl<K, M: BackingMapStorage<K, ()>, L: BackingListStorage<(K, ())>> RawAutoSet<
} }
/// see [HashSet::iter](https://doc.rust-lang.org/std/collections/hash_set/struct.HashSet.html#method.iter) /// see [HashSet::iter](https://doc.rust-lang.org/std/collections/hash_set/struct.HashSet.html#method.iter)
pub fn iter(&self) -> Iter<'_, K> { pub fn iter(&self) -> Iter<M::Iter<'_>> {
Iter(self.map.iter()) Iter(self.map.iter())
} }
} }
...@@ -186,20 +178,36 @@ impl<'a, K, M: BackingMapStorage<K, ()>, L: BackingListStorage<(K, ())>> IntoIte ...@@ -186,20 +178,36 @@ impl<'a, K, M: BackingMapStorage<K, ()>, L: BackingListStorage<(K, ())>> IntoIte
for &'a RawAutoSet<K, M, L> for &'a RawAutoSet<K, M, L>
{ {
type Item = &'a K; type Item = &'a K;
type IntoIter = Iter<'a, K>; type IntoIter = Iter<M::Iter<'a>>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.iter() self.iter()
} }
} }
pub struct Iter<'a, K>(crate::map::Iter<'a, K, ()>); /// A borrowed iterator of a [`RawAutoSet`]. `MI` is [`BackingMapStorage::Iter`] of the map
/// storage.
pub struct Iter<MI: BackingMapIter>(crate::map::Iter<MI>);
impl<'a, K> Iterator for Iter<'a, K> { /// Extracts the key from a borrowed map entry. Like [`BackingMapIter`], the impl doesn't relate the
type Item = &'a K; /// lifetimes of key and value, so the set iterator's trait impls are free of such where clauses.
pub trait SetEntry {
type Key;
fn key(self) -> Self::Key;
}
impl<'a, K> SetEntry for (&'a K, &()) {
type Key = &'a K;
fn key(self) -> Self::Key {
self.0
}
}
impl<MI: BackingMapIter<Item: SetEntry>> Iterator for Iter<MI> {
type Item = <MI::Item as SetEntry>::Key;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, _)| k) self.0.next().map(SetEntry::key)
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
...@@ -207,7 +215,7 @@ impl<'a, K> Iterator for Iter<'a, K> { ...@@ -207,7 +215,7 @@ impl<'a, K> Iterator for Iter<'a, K> {
} }
} }
impl<K> Clone for Iter<'_, K> { impl<MI: BackingMapIter<List: Clone> + Clone> Clone for Iter<MI> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone())
} }
......
...@@ -18,15 +18,53 @@ use std::{ ...@@ -18,15 +18,53 @@ use std::{
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use hashbrown::HashMap; use hashbrown::{HashMap, hash_map as hashbrown_map};
use indexmap::{IndexMap, map::RawEntryApiV1}; use indexmap::{IndexMap, map as indexmap_map, map::RawEntryApiV1};
use crate::{ use crate::{
MAX_USEFUL_LINEAR_SCAN, RawAutoMap, TinyVec, MAX_USEFUL_LINEAR_SCAN, RawAutoMap, TinyVec,
map::{ExtractIfIter, Iter, IterMut, Values, ValuesMut}, map::{ExtractIfIter, ListIter, ListIterMut, ListValues, ListValuesMut},
tiny_vec, tiny_vec,
}; };
/// A borrowed iterator of a [`BackingMapStorage`] (e.g. [`BackingMapStorage::Iter`]), together
/// with the iterator type ([`BackingMapIter::List`]) yielding the same items from the list
/// variant of a [`RawAutoMap`].
///
/// The [`RawAutoMap`] iterators (e.g. [`crate::map::Iter`]) are generic over this trait and take
/// their `Item` type from it. That keeps their trait impls free of where clauses that relate
/// lifetimes, like `MI: Iterator<Item = (&'a K, &'a V)>`. Such where clauses can't be proven
/// when checking whether a future holding the iterator across an `.await` is `Send`, because all
/// lifetimes in the future's state are erased and treated as unrelated there. The compiler then
/// fails with "lifetime bound not satisfied" (rust-lang/rust#100013), e.g. for
/// `maps.iter().flat_map(|m| m.iter())`. Implementations of this trait shouldn't have such where
/// clauses either.
pub trait BackingMapIter: Iterator {
/// The iterator over the list variant, yielding the same items as this iterator.
type List: Iterator<Item = Self::Item>;
}
macro_rules! impl_backing_map_iter {
($($ty:ident :: $iter:ident => $list:ident),* $(,)?) => {
$(
impl<'a, K, V> BackingMapIter for $ty::$iter<'a, K, V> {
type List = $list<'a, K, V>;
}
)*
};
}
impl_backing_map_iter!(
hashbrown_map::Iter => ListIter,
hashbrown_map::IterMut => ListIterMut,
hashbrown_map::Values => ListValues,
hashbrown_map::ValuesMut => ListValuesMut,
indexmap_map::Iter => ListIter,
indexmap_map::IterMut => ListIterMut,
indexmap_map::Values => ListValues,
indexmap_map::ValuesMut => ListValuesMut,
);
/// The result of looking up a key in a [`BackingMapStorage`] via /// The result of looking up a key in a [`BackingMapStorage`] via
/// [`BackingMapStorage::entry`] or [`BackingMapStorage::raw_entry_mut`]. /// [`BackingMapStorage::entry`] or [`BackingMapStorage::raw_entry_mut`].
pub enum MapEntry<O, V> { pub enum MapEntry<O, V> {
...@@ -191,6 +229,26 @@ pub trait BackingMapStorage<K, V>: Sized { ...@@ -191,6 +229,26 @@ pub trait BackingMapStorage<K, V>: Sized {
type IntoIter: Iterator<Item = (K, V)>; type IntoIter: Iterator<Item = (K, V)>;
type IntoValues: Iterator<Item = V>; type IntoValues: Iterator<Item = V>;
type Iter<'a>: BackingMapIter<Item = (&'a K, &'a V), List = ListIter<'a, K, V>> + Clone
where
Self: 'a,
K: 'a,
V: 'a;
type IterMut<'a>: BackingMapIter<Item = (&'a K, &'a mut V), List = ListIterMut<'a, K, V>>
where
Self: 'a,
K: 'a,
V: 'a;
type Values<'a>: BackingMapIter<Item = &'a V, List = ListValues<'a, K, V>>
where
Self: 'a,
K: 'a,
V: 'a;
type ValuesMut<'a>: BackingMapIter<Item = &'a mut V, List = ListValuesMut<'a, K, V>>
where
Self: 'a,
K: 'a,
V: 'a;
type Drain<'a>: Iterator<Item = (K, V)> type Drain<'a>: Iterator<Item = (K, V)>
where where
Self: 'a; Self: 'a;
...@@ -214,25 +272,10 @@ pub trait BackingMapStorage<K, V>: Sized { ...@@ -214,25 +272,10 @@ pub trait BackingMapStorage<K, V>: Sized {
} }
fn capacity(&self) -> usize; fn capacity(&self) -> usize;
fn clear(&mut self); fn clear(&mut self);
// The borrowed iterators are concrete enums (instead of generic associated types), as fn iter<'a>(&'a self) -> Self::Iter<'a>;
// projections of generic associated types break `Send` inference for futures holding them fn iter_mut<'a>(&'a mut self) -> Self::IterMut<'a>;
// across an `.await` (rust-lang/rust#100013). fn values<'a>(&'a self) -> Self::Values<'a>;
fn iter<'a>(&'a self) -> Iter<'a, K, V> fn values_mut<'a>(&'a mut self) -> Self::ValuesMut<'a>;
where
K: 'a,
V: 'a;
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V>
where
K: 'a,
V: 'a;
fn values<'a>(&'a self) -> Values<'a, K, V>
where
K: 'a,
V: 'a;
fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V>
where
K: 'a,
V: 'a;
fn into_entries(self) -> Self::IntoIter; fn into_entries(self) -> Self::IntoIter;
fn into_values(self) -> Self::IntoValues; fn into_values(self) -> Self::IntoValues;
/// Removes and yields all entries (in order for ordered storages). /// Removes and yields all entries (in order for ordered storages).
...@@ -332,6 +375,30 @@ impl<K, V, H> BackingMapStorage<K, V> for HashMap<K, V, H> { ...@@ -332,6 +375,30 @@ impl<K, V, H> BackingMapStorage<K, V> for HashMap<K, V, H> {
type IntoIter = hashbrown::hash_map::IntoIter<K, V>; type IntoIter = hashbrown::hash_map::IntoIter<K, V>;
type IntoValues = hashbrown::hash_map::IntoValues<K, V>; type IntoValues = hashbrown::hash_map::IntoValues<K, V>;
type Iter<'a>
= hashbrown::hash_map::Iter<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type IterMut<'a>
= hashbrown::hash_map::IterMut<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type Values<'a>
= hashbrown::hash_map::Values<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type ValuesMut<'a>
= hashbrown::hash_map::ValuesMut<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type Drain<'a> type Drain<'a>
= hashbrown::hash_map::Drain<'a, K, V> = hashbrown::hash_map::Drain<'a, K, V>
where where
...@@ -365,33 +432,17 @@ impl<K, V, H> BackingMapStorage<K, V> for HashMap<K, V, H> { ...@@ -365,33 +432,17 @@ impl<K, V, H> BackingMapStorage<K, V> for HashMap<K, V, H> {
fn clear(&mut self) { fn clear(&mut self) {
HashMap::clear(self) HashMap::clear(self)
} }
fn iter<'a>(&'a self) -> Iter<'a, K, V> fn iter<'a>(&'a self) -> Self::Iter<'a> {
where HashMap::iter(self)
K: 'a,
V: 'a,
{
Iter::HashMap(HashMap::iter(self))
} }
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V> fn iter_mut<'a>(&'a mut self) -> Self::IterMut<'a> {
where HashMap::iter_mut(self)
K: 'a,
V: 'a,
{
IterMut::HashMap(HashMap::iter_mut(self))
} }
fn values<'a>(&'a self) -> Values<'a, K, V> fn values<'a>(&'a self) -> Self::Values<'a> {
where HashMap::values(self)
K: 'a,
V: 'a,
{
Values::HashMap(HashMap::values(self))
} }
fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V> fn values_mut<'a>(&'a mut self) -> Self::ValuesMut<'a> {
where HashMap::values_mut(self)
K: 'a,
V: 'a,
{
ValuesMut::HashMap(HashMap::values_mut(self))
} }
fn into_entries(self) -> Self::IntoIter { fn into_entries(self) -> Self::IntoIter {
IntoIterator::into_iter(self) IntoIterator::into_iter(self)
...@@ -578,6 +629,30 @@ impl<K, V, H> BackingMapStorage<K, V> for IndexMap<K, V, H> { ...@@ -578,6 +629,30 @@ impl<K, V, H> BackingMapStorage<K, V> for IndexMap<K, V, H> {
type IntoIter = indexmap::map::IntoIter<K, V>; type IntoIter = indexmap::map::IntoIter<K, V>;
type IntoValues = indexmap::map::IntoValues<K, V>; type IntoValues = indexmap::map::IntoValues<K, V>;
type Iter<'a>
= indexmap::map::Iter<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type IterMut<'a>
= indexmap::map::IterMut<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type Values<'a>
= indexmap::map::Values<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type ValuesMut<'a>
= indexmap::map::ValuesMut<'a, K, V>
where
Self: 'a,
K: 'a,
V: 'a;
type Drain<'a> type Drain<'a>
= indexmap::map::Drain<'a, K, V> = indexmap::map::Drain<'a, K, V>
where where
...@@ -611,33 +686,17 @@ impl<K, V, H> BackingMapStorage<K, V> for IndexMap<K, V, H> { ...@@ -611,33 +686,17 @@ impl<K, V, H> BackingMapStorage<K, V> for IndexMap<K, V, H> {
fn clear(&mut self) { fn clear(&mut self) {
IndexMap::clear(self) IndexMap::clear(self)
} }
fn iter<'a>(&'a self) -> Iter<'a, K, V> fn iter<'a>(&'a self) -> Self::Iter<'a> {
where IndexMap::iter(self)
K: 'a,
V: 'a,
{
Iter::IndexMap(IndexMap::iter(self))
} }
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V> fn iter_mut<'a>(&'a mut self) -> Self::IterMut<'a> {
where IndexMap::iter_mut(self)
K: 'a,
V: 'a,
{
IterMut::IndexMap(IndexMap::iter_mut(self))
} }
fn values<'a>(&'a self) -> Values<'a, K, V> fn values<'a>(&'a self) -> Self::Values<'a> {
where IndexMap::values(self)
K: 'a,
V: 'a,
{
Values::IndexMap(IndexMap::values(self))
} }
fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V> fn values_mut<'a>(&'a mut self) -> Self::ValuesMut<'a> {
where IndexMap::values_mut(self)
K: 'a,
V: 'a,
{
ValuesMut::IndexMap(IndexMap::values_mut(self))
} }
fn into_entries(self) -> Self::IntoIter { fn into_entries(self) -> Self::IntoIter {
IntoIterator::into_iter(self) IntoIterator::into_iter(self)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment