| 1 |
//! An ordered map based on a B-Tree that keeps insertion order of elements. |
| 2 |
|
| 3 |
#[cfg(not(feature = "no-hash-maps"))] |
| 4 |
mod impls { |
| 5 |
use crate::collections::hash; |
| 6 |
use indexmap::IndexMap; |
| 7 |
|
| 8 |
pub type IndexMapImpl<K, V> = IndexMap<K, V, hash::RandomState>; |
| 9 |
pub type EntryImpl<'a, K, V> = indexmap::map::Entry<'a, K, V>; |
| 10 |
pub type OccupiedEntryImpl<'a, K, V> = indexmap::map::OccupiedEntry<'a, K, V>; |
| 11 |
pub type VacantEntryImpl<'a, K, V> = indexmap::map::VacantEntry<'a, K, V>; |
| 12 |
pub type IterImpl<'a, K, V> = indexmap::map::Iter<'a, K, V>; |
| 13 |
pub type IterMutImpl<'a, K, V> = indexmap::map::IterMut<'a, K, V>; |
| 14 |
pub type IntoIterImpl<K, V> = indexmap::map::IntoIter<K, V>; |
| 15 |
pub type KeysImpl<'a, K, V> = indexmap::map::Keys<'a, K, V>; |
| 16 |
pub type ValuesImpl<'a, K, V> = indexmap::map::Values<'a, K, V>; |
| 17 |
pub type ValuesMutImpl<'a, K, V> = indexmap::map::ValuesMut<'a, K, V>; |
| 18 |
} |
| 19 |
|
| 20 |
#[cfg(feature = "no-hash-maps")] |
| 21 |
mod impls { |
| 22 |
pub type IndexMapImpl<K, V> = super::IndexMap<K, V>; |
| 23 |
pub type EntryImpl<'a, K, V> = super::Entry<'a, K, V>; |
| 24 |
pub type OccupiedEntryImpl<'a, K, V> = super::OccupiedEntry<'a, K, V>; |
| 25 |
pub type VacantEntryImpl<'a, K, V> = super::VacantEntry<'a, K, V>; |
| 26 |
pub type IterImpl<'a, K, V> = super::Iter<'a, K, V>; |
| 27 |
pub type IterMutImpl<'a, K, V> = super::IterMut<'a, K, V>; |
| 28 |
pub type IntoIterImpl<K, V> = super::IntoIter<K, V>; |
| 29 |
pub type KeysImpl<'a, K, V> = super::Keys<'a, K, V>; |
| 30 |
pub type ValuesImpl<'a, K, V> = super::Values<'a, K, V>; |
| 31 |
pub type ValuesMutImpl<'a, K, V> = super::ValuesMut<'a, K, V>; |
| 32 |
} |
| 33 |
|
| 34 |
pub use self::impls::*; |
| 35 |
|
| 36 |
use alloc::collections::{btree_map, BTreeMap}; |
| 37 |
use alloc::vec::IntoIter as VecIntoIter; |
| 38 |
use alloc::vec::Vec; |
| 39 |
use core::borrow::Borrow; |
| 40 |
use core::fmt; |
| 41 |
use core::iter::FusedIterator; |
| 42 |
use core::mem::replace; |
| 43 |
use core::ops::{Index, IndexMut}; |
| 44 |
use core::slice::Iter as SliceIter; |
| 45 |
use core::slice::IterMut as SliceIterMut; |
| 46 |
|
| 47 |
/// A slot index referencing a slot in an [`IndexMap`]. |
| 48 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 49 |
struct SlotIndex(usize); |
| 50 |
|
| 51 |
impl SlotIndex { |
| 52 |
/// Returns the raw `usize` index of the [`SlotIndex`]. |
| 53 |
pub fn index(self) -> usize { |
| 54 |
self.0 |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 59 |
struct Slot<K, V> { |
| 60 |
/// The key of the [`Slot`]. |
| 61 |
key: K, |
| 62 |
/// The value of the [`Slot`]. |
| 63 |
value: V, |
| 64 |
} |
| 65 |
|
| 66 |
impl<K, V> Slot<K, V> { |
| 67 |
/// Creates a new [`Slot`] from the given `key` and `value`. |
| 68 |
pub fn new(key: K, value: V) -> Self { |
| 69 |
Self { key, value } |
| 70 |
} |
| 71 |
|
| 72 |
/// Returns the [`Slot`] as a pair of references to its `key` and `value`. |
| 73 |
pub fn as_pair(&self) -> (&K, &V) { |
| 74 |
(&self.key, &self.value) |
| 75 |
} |
| 76 |
|
| 77 |
/// Returns the [`Slot`] as a pair of references to its `key` and `value`. |
| 78 |
pub fn as_pair_mut(&mut self) -> (&K, &mut V) { |
| 79 |
(&self.key, &mut self.value) |
| 80 |
} |
| 81 |
|
| 82 |
/// Converts the [`Slot`] into a pair of its `key` and `value`. |
| 83 |
pub fn into_pair(self) -> (K, V) { |
| 84 |
(self.key, self.value) |
| 85 |
} |
| 86 |
|
| 87 |
/// Returns a shared reference to the key of the [`Slot`]. |
| 88 |
pub fn key(&self) -> &K { |
| 89 |
&self.key |
| 90 |
} |
| 91 |
|
| 92 |
/// Returns a shared reference to the value of the [`Slot`]. |
| 93 |
pub fn value(&self) -> &V { |
| 94 |
&self.value |
| 95 |
} |
| 96 |
|
| 97 |
/// Returns an exclusive reference to the value of the [`Slot`]. |
| 98 |
pub fn value_mut(&mut self) -> &mut V { |
| 99 |
&mut self.value |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/// A b-tree map where the iteration order of the key-value |
| 104 |
/// pairs is independent of the ordering of the keys. |
| 105 |
/// |
| 106 |
/// The interface is closely compatible with the [`indexmap` crate] |
| 107 |
/// and a subset of the features that is relevant for the |
| 108 |
/// [`wasmparser-nostd` crate]. |
| 109 |
/// |
| 110 |
/// # Differences to original `IndexMap` |
| 111 |
/// |
| 112 |
/// Since the goal of this crate was to maintain a simple |
| 113 |
/// `no_std` compatible fork of the [`indexmap` crate] there are some |
| 114 |
/// downsides and differences. |
| 115 |
/// |
| 116 |
/// - Some operations such as `IndexMap::insert` now require `K: Clone`. |
| 117 |
/// - It is to be expected that this fork performs worse than the original |
| 118 |
/// [`indexmap` crate] implementation. |
| 119 |
/// - The implementation is based on `BTreeMap` internally instead of |
| 120 |
/// `HashMap` which has the effect that methods no longer require `K: Hash` |
| 121 |
/// but `K: Ord` instead. |
| 122 |
/// |
| 123 |
/// [`indexmap` crate]: https://crates.io/crates/indexmap |
| 124 |
/// [`wasmparser-nostd` crate]: https://crates.io/crates/wasmparser-nostd |
| 125 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 126 |
pub struct IndexMap<K, V> { |
| 127 |
/// A mapping from keys to slot indices. |
| 128 |
key2slot: BTreeMap<K, SlotIndex>, |
| 129 |
/// A vector holding all slots of key value pairs. |
| 130 |
slots: Vec<Slot<K, V>>, |
| 131 |
} |
| 132 |
|
| 133 |
impl<K, V> Default for IndexMap<K, V> { |
| 134 |
fn default() -> Self { |
| 135 |
Self::new() |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl<K, V> IndexMap<K, V> { |
| 140 |
/// Makes a new, empty [`IndexMap`]. |
| 141 |
/// |
| 142 |
/// Does not allocate anything on its own. |
| 143 |
pub fn new() -> Self { |
| 144 |
Self { |
| 145 |
key2slot: BTreeMap::new(), |
| 146 |
slots: Vec::new(), |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/// Constructs a new, empty [`IndexMap`] with at least the specified capacity. |
| 151 |
/// |
| 152 |
/// Does not allocate if `capacity` is zero. |
| 153 |
pub fn with_capacity(capacity: usize) -> Self { |
| 154 |
Self { |
| 155 |
key2slot: BTreeMap::new(), |
| 156 |
slots: Vec::with_capacity(capacity), |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// Reserve capacity for at least `additional` more key-value pairs. |
| 161 |
pub fn reserve(&mut self, additional: usize) { |
| 162 |
self.slots.reserve(additional); |
| 163 |
} |
| 164 |
|
| 165 |
/// Returns the number of elements in the map. |
| 166 |
pub fn len(&self) -> usize { |
| 167 |
self.slots.len() |
| 168 |
} |
| 169 |
|
| 170 |
/// Returns `true` if the map contains no elements. |
| 171 |
pub fn is_empty(&self) -> bool { |
| 172 |
self.len() == 0 |
| 173 |
} |
| 174 |
|
| 175 |
/// Returns true if the map contains a value for the specified key. |
| 176 |
/// |
| 177 |
/// The key may be any borrowed form of the map’s key type, |
| 178 |
/// but the ordering on the borrowed form must match the ordering on the key type. |
| 179 |
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool |
| 180 |
where |
| 181 |
K: Borrow<Q> + Ord, |
| 182 |
Q: Ord, |
| 183 |
{ |
| 184 |
self.key2slot.contains_key(key) |
| 185 |
} |
| 186 |
|
| 187 |
/// Inserts a key-value pair into the map. |
| 188 |
/// |
| 189 |
/// If the map did not have this key present, `None` is returned. |
| 190 |
/// |
| 191 |
/// If the map did have this key present, the value is updated, and the old |
| 192 |
/// value is returned. The key is not updated, though; this matters for |
| 193 |
/// types that can be `==` without being identical. |
| 194 |
pub fn insert(&mut self, key: K, value: V) -> Option<V> |
| 195 |
where |
| 196 |
K: Ord + Clone, |
| 197 |
{ |
| 198 |
self.insert_full(key, value).1 |
| 199 |
} |
| 200 |
|
| 201 |
/// Inserts a key-value pair into the map. |
| 202 |
/// |
| 203 |
/// Returns the unique index to the key-value pair alongside the previous value. |
| 204 |
/// |
| 205 |
/// If the map did not have this key present, `None` is returned. |
| 206 |
/// |
| 207 |
/// If the map did have this key present, the value is updated, and the old |
| 208 |
/// value is returned. The key is not updated, though; this matters for |
| 209 |
/// types that can be `==` without being identical. |
| 210 |
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) |
| 211 |
where |
| 212 |
K: Ord + Clone, |
| 213 |
{ |
| 214 |
match self.key2slot.entry(key.clone()) { |
| 215 |
btree_map::Entry::Vacant(entry) => { |
| 216 |
let index = self.slots.len(); |
| 217 |
entry.insert(SlotIndex(index)); |
| 218 |
self.slots.push(Slot::new(key, value)); |
| 219 |
(index, None) |
| 220 |
} |
| 221 |
btree_map::Entry::Occupied(entry) => { |
| 222 |
let index = entry.get().index(); |
| 223 |
let new_slot = Slot::new(key, value); |
| 224 |
let old_slot = replace(&mut self.slots[index], new_slot); |
| 225 |
(index, Some(old_slot.value)) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/// Remove the key-value pair equivalent to `key` and return it and |
| 231 |
/// the index it had. |
| 232 |
/// |
| 233 |
/// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
| 234 |
/// last element of the map and popping it off. **This perturbs |
| 235 |
/// the position of what used to be the last element!** |
| 236 |
/// |
| 237 |
/// Return `None` if `key` is not in map. |
| 238 |
pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V> |
| 239 |
where |
| 240 |
K: Borrow<Q> + Ord, |
| 241 |
Q: ?Sized + Ord, |
| 242 |
{ |
| 243 |
self.swap_remove_full(key) |
| 244 |
.map(|(_index, _key, value)| value) |
| 245 |
} |
| 246 |
|
| 247 |
/// Remove and return the key-value pair equivalent to `key`. |
| 248 |
/// |
| 249 |
/// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
| 250 |
/// last element of the map and popping it off. **This perturbs |
| 251 |
/// the position of what used to be the last element!** |
| 252 |
/// |
| 253 |
/// Return `None` if `key` is not in map. |
| 254 |
/// |
| 255 |
/// [`Vec::swap_remove`]: alloc::vec::Vec::swap_remove |
| 256 |
pub fn swap_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> |
| 257 |
where |
| 258 |
K: Borrow<Q> + Ord, |
| 259 |
Q: ?Sized + Ord, |
| 260 |
{ |
| 261 |
self.swap_remove_full(key) |
| 262 |
.map(|(_index, key, value)| (key, value)) |
| 263 |
} |
| 264 |
|
| 265 |
/// Remove the key-value pair equivalent to `key` and return it and |
| 266 |
/// the index it had. |
| 267 |
/// |
| 268 |
/// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
| 269 |
/// last element of the map and popping it off. **This perturbs |
| 270 |
/// the position of what used to be the last element!** |
| 271 |
/// |
| 272 |
/// Return `None` if `key` is not in map. |
| 273 |
pub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> |
| 274 |
where |
| 275 |
K: Borrow<Q> + Ord, |
| 276 |
Q: ?Sized + Ord, |
| 277 |
{ |
| 278 |
let index = self.key2slot.remove(key)?.0; |
| 279 |
let removed = self.slots.swap_remove(index); |
| 280 |
if index != self.len() { |
| 281 |
// If the index was referring the last element |
| 282 |
// `swap_remove` would not swap any other element |
| 283 |
// thus adjustments are only needed if this was not the case. |
| 284 |
let swapped = self.slots[index].key.borrow(); |
| 285 |
let swapped_index = self |
| 286 |
.key2slot |
| 287 |
.get_mut(swapped) |
| 288 |
.expect("the swapped entry's key must be present"); |
| 289 |
*swapped_index = SlotIndex(index); |
| 290 |
} |
| 291 |
Some((index, removed.key, removed.value)) |
| 292 |
} |
| 293 |
|
| 294 |
/// Gets the given key’s corresponding entry in the map for in-place manipulation. |
| 295 |
pub fn entry(&mut self, key: K) -> Entry<K, V> |
| 296 |
where |
| 297 |
K: Ord + Clone, |
| 298 |
{ |
| 299 |
match self.key2slot.entry(key) { |
| 300 |
btree_map::Entry::Vacant(entry) => Entry::Vacant(VacantEntry { |
| 301 |
vacant: entry, |
| 302 |
slots: &mut self.slots, |
| 303 |
}), |
| 304 |
btree_map::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry { |
| 305 |
occupied: entry, |
| 306 |
slots: &mut self.slots, |
| 307 |
}), |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/// Returns a reference to the value corresponding to the key. |
| 312 |
/// |
| 313 |
/// The key may be any borrowed form of the map’s key type, |
| 314 |
/// but the ordering on the borrowed form must match the ordering on the key type. |
| 315 |
pub fn get<Q>(&self, key: &Q) -> Option<&V> |
| 316 |
where |
| 317 |
K: Borrow<Q> + Ord, |
| 318 |
Q: ?Sized + Ord, |
| 319 |
{ |
| 320 |
self.key2slot |
| 321 |
.get(key) |
| 322 |
.map(|slot| &self.slots[slot.index()].value) |
| 323 |
} |
| 324 |
|
| 325 |
/// Returns a mutable reference to the value corresponding to the key. |
| 326 |
/// |
| 327 |
/// The key may be any borrowed form of the map’s key type, |
| 328 |
/// but the ordering on the borrowed form must match the ordering on the key type. |
| 329 |
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> |
| 330 |
where |
| 331 |
K: Borrow<Q> + Ord, |
| 332 |
Q: Ord, |
| 333 |
{ |
| 334 |
self.key2slot |
| 335 |
.get(key) |
| 336 |
.map(|slot| &mut self.slots[slot.index()].value) |
| 337 |
} |
| 338 |
|
| 339 |
/// Returns the key-value pair corresponding to the supplied key. |
| 340 |
/// |
| 341 |
/// The supplied key may be any borrowed form of the map's key type, |
| 342 |
/// but the ordering on the borrowed form *must* match the ordering |
| 343 |
/// on the key type. |
| 344 |
pub fn get_key_value<Q: ?Sized>(&self, key: &Q) -> Option<(&K, &V)> |
| 345 |
where |
| 346 |
K: Borrow<Q> + Ord, |
| 347 |
Q: Ord, |
| 348 |
{ |
| 349 |
self.key2slot |
| 350 |
.get_key_value(key) |
| 351 |
.map(|(key, slot)| (key, &self.slots[slot.index()].value)) |
| 352 |
} |
| 353 |
|
| 354 |
/// Returns the key-value pair corresponding to the supplied key |
| 355 |
/// as well as the unique index of the returned key-value pair. |
| 356 |
/// |
| 357 |
/// The supplied key may be any borrowed form of the map's key type, |
| 358 |
/// but the ordering on the borrowed form *must* match the ordering |
| 359 |
/// on the key type. |
| 360 |
pub fn get_full<Q: ?Sized>(&self, key: &Q) -> Option<(usize, &K, &V)> |
| 361 |
where |
| 362 |
K: Borrow<Q> + Ord, |
| 363 |
Q: Ord, |
| 364 |
{ |
| 365 |
self.key2slot.get_key_value(key).map(|(key, slot)| { |
| 366 |
let index = slot.index(); |
| 367 |
let value = &self.slots[index].value; |
| 368 |
(index, key, value) |
| 369 |
}) |
| 370 |
} |
| 371 |
|
| 372 |
/// Returns the unique index corresponding to the supplied key. |
| 373 |
/// |
| 374 |
/// The supplied key may be any borrowed form of the map's key type, |
| 375 |
/// but the ordering on the borrowed form *must* match the ordering |
| 376 |
/// on the key type. |
| 377 |
pub fn get_index_of<Q: ?Sized>(&self, key: &Q) -> Option<usize> |
| 378 |
where |
| 379 |
K: Borrow<Q> + Ord, |
| 380 |
Q: Ord, |
| 381 |
{ |
| 382 |
self.key2slot.get(key).copied().map(SlotIndex::index) |
| 383 |
} |
| 384 |
|
| 385 |
/// Returns a shared reference to the key-value pair at the given index. |
| 386 |
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> { |
| 387 |
self.slots.get(index).map(Slot::as_pair) |
| 388 |
} |
| 389 |
|
| 390 |
/// Returns an exclusive reference to the key-value pair at the given index. |
| 391 |
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { |
| 392 |
self.slots.get_mut(index).map(Slot::as_pair_mut) |
| 393 |
} |
| 394 |
|
| 395 |
/// Gets an iterator over the entries of the map, sorted by key. |
| 396 |
pub fn iter(&self) -> Iter<K, V> { |
| 397 |
Iter { |
| 398 |
iter: self.slots.iter(), |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/// Gets a mutable iterator over the entries of the map, sorted by key. |
| 403 |
pub fn iter_mut(&mut self) -> IterMut<K, V> { |
| 404 |
IterMut { |
| 405 |
iter: self.slots.iter_mut(), |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
/// Gets an iterator over the values of the map, in order by key. |
| 410 |
pub fn keys(&self) -> Keys<K, V> { |
| 411 |
Keys { |
| 412 |
iter: self.slots.iter(), |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
/// Gets an iterator over the values of the map, in order by key. |
| 417 |
pub fn values(&self) -> Values<K, V> { |
| 418 |
Values { |
| 419 |
iter: self.slots.iter(), |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/// Gets a mutable iterator over the values of the map, in order by key. |
| 424 |
pub fn values_mut(&mut self) -> ValuesMut<K, V> { |
| 425 |
ValuesMut { |
| 426 |
iter: self.slots.iter_mut(), |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/// Clears the map, removing all elements. |
| 431 |
pub fn clear(&mut self) { |
| 432 |
self.key2slot.clear(); |
| 433 |
self.slots.clear(); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
impl<'a, K, Q, V> Index<&'a Q> for IndexMap<K, V> |
| 438 |
where |
| 439 |
K: Borrow<Q> + Ord, |
| 440 |
Q: ?Sized + Ord, |
| 441 |
{ |
| 442 |
type Output = V; |
| 443 |
|
| 444 |
fn index(&self, key: &'a Q) -> &Self::Output { |
| 445 |
self.get(key).expect("no entry found for key") |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
impl<K, V> Index<usize> for IndexMap<K, V> { |
| 450 |
type Output = V; |
| 451 |
|
| 452 |
fn index(&self, index: usize) -> &Self::Output { |
| 453 |
let (_key, value) = self |
| 454 |
.get_index(index) |
| 455 |
.expect("IndexMap: index out of bounds"); |
| 456 |
value |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
impl<K, V> IndexMut<usize> for IndexMap<K, V> { |
| 461 |
fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 462 |
let (_key, value) = self |
| 463 |
.get_index_mut(index) |
| 464 |
.expect("IndexMap: index out of bounds"); |
| 465 |
value |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
impl<'a, K, V> Extend<(&'a K, &'a V)> for IndexMap<K, V> |
| 470 |
where |
| 471 |
K: Ord + Copy, |
| 472 |
V: Copy, |
| 473 |
{ |
| 474 |
fn extend<T>(&mut self, iter: T) |
| 475 |
where |
| 476 |
T: IntoIterator<Item = (&'a K, &'a V)>, |
| 477 |
{ |
| 478 |
self.extend(iter.into_iter().map(|(key, value)| (*key, *value))) |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
impl<K, V> Extend<(K, V)> for IndexMap<K, V> |
| 483 |
where |
| 484 |
K: Ord + Clone, |
| 485 |
{ |
| 486 |
fn extend<T>(&mut self, iter: T) |
| 487 |
where |
| 488 |
T: IntoIterator<Item = (K, V)>, |
| 489 |
{ |
| 490 |
iter.into_iter().for_each(move |(k, v)| { |
| 491 |
self.insert(k, v); |
| 492 |
}); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
impl<K, V> FromIterator<(K, V)> for IndexMap<K, V> |
| 497 |
where |
| 498 |
K: Ord + Clone, |
| 499 |
{ |
| 500 |
fn from_iter<T>(iter: T) -> Self |
| 501 |
where |
| 502 |
T: IntoIterator<Item = (K, V)>, |
| 503 |
{ |
| 504 |
let mut map = IndexMap::new(); |
| 505 |
map.extend(iter); |
| 506 |
map |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
impl<K, V, const N: usize> From<[(K, V); N]> for IndexMap<K, V> |
| 511 |
where |
| 512 |
K: Ord + Clone, |
| 513 |
{ |
| 514 |
fn from(items: [(K, V); N]) -> Self { |
| 515 |
items.into_iter().collect() |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
impl<'a, K, V> IntoIterator for &'a IndexMap<K, V> { |
| 520 |
type Item = (&'a K, &'a V); |
| 521 |
type IntoIter = Iter<'a, K, V>; |
| 522 |
|
| 523 |
fn into_iter(self) -> Self::IntoIter { |
| 524 |
self.iter() |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
impl<'a, K, V> IntoIterator for &'a mut IndexMap<K, V> { |
| 529 |
type Item = (&'a K, &'a mut V); |
| 530 |
type IntoIter = IterMut<'a, K, V>; |
| 531 |
|
| 532 |
fn into_iter(self) -> Self::IntoIter { |
| 533 |
self.iter_mut() |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
impl<K, V> IntoIterator for IndexMap<K, V> { |
| 538 |
type Item = (K, V); |
| 539 |
type IntoIter = IntoIter<K, V>; |
| 540 |
|
| 541 |
fn into_iter(self) -> Self::IntoIter { |
| 542 |
IntoIter { |
| 543 |
iter: self.slots.into_iter(), |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/// An iterator over the entries of an [`IndexMap`]. |
| 549 |
/// |
| 550 |
/// This `struct` is created by the [`iter`] method on [`IndexMap`]. See its |
| 551 |
/// documentation for more. |
| 552 |
/// |
| 553 |
/// [`iter`]: IndexMap::iter |
| 554 |
#[derive(Debug, Clone)] |
| 555 |
pub struct Iter<'a, K, V> { |
| 556 |
iter: SliceIter<'a, Slot<K, V>>, |
| 557 |
} |
| 558 |
|
| 559 |
impl<'a, K, V> Iterator for Iter<'a, K, V> { |
| 560 |
type Item = (&'a K, &'a V); |
| 561 |
|
| 562 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 563 |
self.iter.size_hint() |
| 564 |
} |
| 565 |
|
| 566 |
fn count(self) -> usize { |
| 567 |
self.iter.count() |
| 568 |
} |
| 569 |
|
| 570 |
fn next(&mut self) -> Option<Self::Item> { |
| 571 |
self.iter.next().map(Slot::as_pair) |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> { |
| 576 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 577 |
self.iter.next_back().map(Slot::as_pair) |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> { |
| 582 |
fn len(&self) -> usize { |
| 583 |
self.iter.len() |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
impl<'a, K, V> FusedIterator for Iter<'a, K, V> {} |
| 588 |
|
| 589 |
/// A mutable iterator over the entries of an [`IndexMap`]. |
| 590 |
/// |
| 591 |
/// This `struct` is created by the [`iter_mut`] method on [`IndexMap`]. See its |
| 592 |
/// documentation for more. |
| 593 |
/// |
| 594 |
/// [`iter_mut`]: IndexMap::iter_mut |
| 595 |
#[derive(Debug)] |
| 596 |
pub struct IterMut<'a, K, V> { |
| 597 |
iter: SliceIterMut<'a, Slot<K, V>>, |
| 598 |
} |
| 599 |
|
| 600 |
impl<'a, K, V> Iterator for IterMut<'a, K, V> { |
| 601 |
type Item = (&'a K, &'a mut V); |
| 602 |
|
| 603 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 604 |
self.iter.size_hint() |
| 605 |
} |
| 606 |
|
| 607 |
fn count(self) -> usize { |
| 608 |
self.iter.count() |
| 609 |
} |
| 610 |
|
| 611 |
fn next(&mut self) -> Option<Self::Item> { |
| 612 |
self.iter.next().map(Slot::as_pair_mut) |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> { |
| 617 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 618 |
self.iter.next_back().map(Slot::as_pair_mut) |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> { |
| 623 |
fn len(&self) -> usize { |
| 624 |
self.iter.len() |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {} |
| 629 |
|
| 630 |
/// An owning iterator over the entries of a [`IndexMap`]. |
| 631 |
/// |
| 632 |
/// This `struct` is created by the [`into_iter`] method on [`IndexMap`] |
| 633 |
/// (provided by the [`IntoIterator`] trait). See its documentation for more. |
| 634 |
/// |
| 635 |
/// [`into_iter`]: IntoIterator::into_iter |
| 636 |
/// [`IntoIterator`]: core::iter::IntoIterator |
| 637 |
#[derive(Debug)] |
| 638 |
pub struct IntoIter<K, V> { |
| 639 |
iter: VecIntoIter<Slot<K, V>>, |
| 640 |
} |
| 641 |
|
| 642 |
impl<K, V> Iterator for IntoIter<K, V> { |
| 643 |
type Item = (K, V); |
| 644 |
|
| 645 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 646 |
self.iter.size_hint() |
| 647 |
} |
| 648 |
|
| 649 |
fn count(self) -> usize { |
| 650 |
self.iter.count() |
| 651 |
} |
| 652 |
|
| 653 |
fn next(&mut self) -> Option<Self::Item> { |
| 654 |
self.iter.next().map(Slot::into_pair) |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
impl<K, V> DoubleEndedIterator for IntoIter<K, V> { |
| 659 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 660 |
self.iter.next_back().map(Slot::into_pair) |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
impl<K, V> ExactSizeIterator for IntoIter<K, V> { |
| 665 |
fn len(&self) -> usize { |
| 666 |
self.iter.len() |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
impl<K, V> FusedIterator for IntoIter<K, V> {} |
| 671 |
|
| 672 |
/// An iterator over the keys of an [`IndexMap`]. |
| 673 |
/// |
| 674 |
/// This `struct` is created by the [`keys`] method on [`IndexMap`]. See its |
| 675 |
/// documentation for more. |
| 676 |
/// |
| 677 |
/// [`keys`]: IndexMap::keys |
| 678 |
#[derive(Debug, Clone)] |
| 679 |
pub struct Keys<'a, K, V> { |
| 680 |
iter: SliceIter<'a, Slot<K, V>>, |
| 681 |
} |
| 682 |
|
| 683 |
impl<'a, K, V> Iterator for Keys<'a, K, V> { |
| 684 |
type Item = &'a K; |
| 685 |
|
| 686 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 687 |
self.iter.size_hint() |
| 688 |
} |
| 689 |
|
| 690 |
fn count(self) -> usize { |
| 691 |
self.iter.count() |
| 692 |
} |
| 693 |
|
| 694 |
fn next(&mut self) -> Option<Self::Item> { |
| 695 |
self.iter.next().map(Slot::key) |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> { |
| 700 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 701 |
self.iter.next_back().map(Slot::key) |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> { |
| 706 |
fn len(&self) -> usize { |
| 707 |
self.iter.len() |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
impl<'a, K, V> FusedIterator for Keys<'a, K, V> {} |
| 712 |
|
| 713 |
/// An iterator over the values of an [`IndexMap`]. |
| 714 |
/// |
| 715 |
/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its |
| 716 |
/// documentation for more. |
| 717 |
/// |
| 718 |
/// [`values`]: IndexMap::values |
| 719 |
#[derive(Debug, Clone)] |
| 720 |
pub struct Values<'a, K, V> { |
| 721 |
iter: SliceIter<'a, Slot<K, V>>, |
| 722 |
} |
| 723 |
|
| 724 |
impl<'a, K, V> Iterator for Values<'a, K, V> { |
| 725 |
type Item = &'a V; |
| 726 |
|
| 727 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 728 |
self.iter.size_hint() |
| 729 |
} |
| 730 |
|
| 731 |
fn count(self) -> usize { |
| 732 |
self.iter.count() |
| 733 |
} |
| 734 |
|
| 735 |
fn next(&mut self) -> Option<Self::Item> { |
| 736 |
self.iter.next().map(Slot::value) |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> { |
| 741 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 742 |
self.iter.next_back().map(Slot::value) |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> { |
| 747 |
fn len(&self) -> usize { |
| 748 |
self.iter.len() |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
impl<'a, K, V> FusedIterator for Values<'a, K, V> {} |
| 753 |
|
| 754 |
/// An iterator over the values of an [`IndexMap`]. |
| 755 |
/// |
| 756 |
/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its |
| 757 |
/// documentation for more. |
| 758 |
/// |
| 759 |
/// [`values`]: IndexMap::values |
| 760 |
#[derive(Debug)] |
| 761 |
pub struct ValuesMut<'a, K, V> { |
| 762 |
iter: SliceIterMut<'a, Slot<K, V>>, |
| 763 |
} |
| 764 |
|
| 765 |
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { |
| 766 |
type Item = &'a mut V; |
| 767 |
|
| 768 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 769 |
self.iter.size_hint() |
| 770 |
} |
| 771 |
|
| 772 |
fn count(self) -> usize { |
| 773 |
self.iter.count() |
| 774 |
} |
| 775 |
|
| 776 |
fn next(&mut self) -> Option<Self::Item> { |
| 777 |
self.iter.next().map(Slot::value_mut) |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
impl<'a, K, V> DoubleEndedIterator for ValuesMut<'a, K, V> { |
| 782 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 783 |
self.iter.next_back().map(Slot::value_mut) |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> { |
| 788 |
fn len(&self) -> usize { |
| 789 |
self.iter.len() |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
impl<'a, K, V> FusedIterator for ValuesMut<'a, K, V> {} |
| 794 |
|
| 795 |
/// A view into a single entry in a map, which may either be vacant or occupied. |
| 796 |
/// |
| 797 |
/// This `enum` is constructed from the [`entry`] method on [`IndexMap`]. |
| 798 |
/// |
| 799 |
/// [`entry`]: IndexMap::entry |
| 800 |
pub enum Entry<'a, K, V> { |
| 801 |
/// A vacant entry. |
| 802 |
Vacant(VacantEntry<'a, K, V>), |
| 803 |
/// An occupied entry. |
| 804 |
Occupied(OccupiedEntry<'a, K, V>), |
| 805 |
} |
| 806 |
|
| 807 |
impl<'a, K: Ord, V> Entry<'a, K, V> { |
| 808 |
/// Ensures a value is in the entry by inserting the default if empty, |
| 809 |
/// and returns a mutable reference to the value in the entry. |
| 810 |
pub fn or_insert(self, default: V) -> &'a mut V |
| 811 |
where |
| 812 |
K: Clone, |
| 813 |
{ |
| 814 |
match self { |
| 815 |
Self::Occupied(entry) => entry.into_mut(), |
| 816 |
Self::Vacant(entry) => entry.insert(default), |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
/// Ensures a value is in the entry by inserting the result |
| 821 |
/// of the default function if empty, |
| 822 |
/// and returns a mutable reference to the value in the entry. |
| 823 |
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V |
| 824 |
where |
| 825 |
K: Clone, |
| 826 |
{ |
| 827 |
match self { |
| 828 |
Self::Occupied(entry) => entry.into_mut(), |
| 829 |
Self::Vacant(entry) => entry.insert(default()), |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
/// Ensures a value is in the entry by inserting, |
| 834 |
/// if empty, the result of the default function. |
| 835 |
/// |
| 836 |
/// This method allows for generating key-derived values for |
| 837 |
/// insertion by providing the default function a reference |
| 838 |
/// to the key that was moved during the `.entry(key)` method call. |
| 839 |
/// |
| 840 |
/// The reference to the moved key is provided |
| 841 |
/// so that cloning or copying the key is |
| 842 |
/// unnecessary, unlike with `.or_insert_with(|| ... )`. |
| 843 |
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V |
| 844 |
where |
| 845 |
K: Clone, |
| 846 |
{ |
| 847 |
match self { |
| 848 |
Self::Occupied(entry) => entry.into_mut(), |
| 849 |
Self::Vacant(entry) => { |
| 850 |
let value = default(entry.key()); |
| 851 |
entry.insert(value) |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
/// Returns a reference to this entry’s key. |
| 857 |
pub fn key(&self) -> &K { |
| 858 |
match *self { |
| 859 |
Self::Occupied(ref entry) => entry.key(), |
| 860 |
Self::Vacant(ref entry) => entry.key(), |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
/// Provides in-place mutable access to an occupied entry |
| 865 |
/// before any potential inserts into the map. |
| 866 |
pub fn and_modify<F>(self, f: F) -> Self |
| 867 |
where |
| 868 |
F: FnOnce(&mut V), |
| 869 |
{ |
| 870 |
match self { |
| 871 |
Self::Occupied(mut entry) => { |
| 872 |
f(entry.get_mut()); |
| 873 |
Self::Occupied(entry) |
| 874 |
} |
| 875 |
Self::Vacant(entry) => Self::Vacant(entry), |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
impl<'a, K, V> Entry<'a, K, V> |
| 881 |
where |
| 882 |
K: Ord + Clone, |
| 883 |
V: Default, |
| 884 |
{ |
| 885 |
/// Ensures a value is in the entry by inserting the default value if empty, |
| 886 |
/// and returns a mutable reference to the value in the entry. |
| 887 |
pub fn or_default(self) -> &'a mut V { |
| 888 |
match self { |
| 889 |
Self::Occupied(entry) => entry.into_mut(), |
| 890 |
Self::Vacant(entry) => entry.insert(Default::default()), |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
impl<'a, K, V> fmt::Debug for Entry<'a, K, V> |
| 896 |
where |
| 897 |
K: fmt::Debug + Ord, |
| 898 |
V: fmt::Debug, |
| 899 |
{ |
| 900 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 901 |
match self { |
| 902 |
Entry::Vacant(entry) => entry.fmt(f), |
| 903 |
Entry::Occupied(entry) => entry.fmt(f), |
| 904 |
} |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
/// A view into a vacant entry in an [`IndexMap`]. It is part of the [`Entry`] `enum`. |
| 909 |
pub struct VacantEntry<'a, K, V> { |
| 910 |
/// The underlying vacant entry. |
| 911 |
vacant: btree_map::VacantEntry<'a, K, SlotIndex>, |
| 912 |
/// The vector that stores all slots. |
| 913 |
slots: &'a mut Vec<Slot<K, V>>, |
| 914 |
} |
| 915 |
|
| 916 |
impl<'a, K, V> VacantEntry<'a, K, V> |
| 917 |
where |
| 918 |
K: Ord, |
| 919 |
{ |
| 920 |
/// Gets a reference to the key that would be used when inserting a value through the VacantEntry. |
| 921 |
pub fn key(&self) -> &K { |
| 922 |
self.vacant.key() |
| 923 |
} |
| 924 |
|
| 925 |
/// Take ownership of the key. |
| 926 |
pub fn into_key(self) -> K { |
| 927 |
self.vacant.into_key() |
| 928 |
} |
| 929 |
|
| 930 |
/// Sets the value of the entry with the `VacantEntry`’s key, |
| 931 |
/// and returns a mutable reference to it. |
| 932 |
pub fn insert(self, value: V) -> &'a mut V |
| 933 |
where |
| 934 |
K: Clone, |
| 935 |
{ |
| 936 |
let index = self.slots.len(); |
| 937 |
let key = self.vacant.key().clone(); |
| 938 |
self.vacant.insert(SlotIndex(index)); |
| 939 |
self.slots.push(Slot::new(key, value)); |
| 940 |
&mut self.slots[index].value |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
impl<'a, K, V> fmt::Debug for VacantEntry<'a, K, V> |
| 945 |
where |
| 946 |
K: fmt::Debug + Ord, |
| 947 |
{ |
| 948 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 949 |
f.debug_struct("VacantEntry") |
| 950 |
.field("key", self.key()) |
| 951 |
.finish() |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
/// A view into an occupied entry in a [`IndexMap`]. It is part of the [`Entry`] `enum`. |
| 956 |
pub struct OccupiedEntry<'a, K, V> { |
| 957 |
/// The underlying occupied entry. |
| 958 |
occupied: btree_map::OccupiedEntry<'a, K, SlotIndex>, |
| 959 |
/// The vector that stores all slots. |
| 960 |
slots: &'a mut Vec<Slot<K, V>>, |
| 961 |
} |
| 962 |
|
| 963 |
impl<'a, K, V> OccupiedEntry<'a, K, V> |
| 964 |
where |
| 965 |
K: Ord, |
| 966 |
{ |
| 967 |
/// Gets a reference to the key in the entry. |
| 968 |
pub fn key(&self) -> &K { |
| 969 |
self.occupied.key() |
| 970 |
} |
| 971 |
|
| 972 |
/// Gets a reference to the value in the entry. |
| 973 |
pub fn get(&self) -> &V { |
| 974 |
let index = self.occupied.get().index(); |
| 975 |
&self.slots[index].value |
| 976 |
} |
| 977 |
|
| 978 |
/// Gets a mutable reference to the value in the entry. |
| 979 |
/// |
| 980 |
/// If you need a reference to the `OccupiedEntry` that may outlive the |
| 981 |
/// destruction of the `Entry` value, see [`into_mut`]. |
| 982 |
/// |
| 983 |
/// [`into_mut`]: OccupiedEntry::into_mut |
| 984 |
pub fn get_mut(&mut self) -> &mut V { |
| 985 |
let index = self.occupied.get().index(); |
| 986 |
&mut self.slots[index].value |
| 987 |
} |
| 988 |
|
| 989 |
/// Converts the entry into a mutable reference to its value. |
| 990 |
/// |
| 991 |
/// If you need multiple references to the `OccupiedEntry`, see [`get_mut`]. |
| 992 |
/// |
| 993 |
/// [`get_mut`]: OccupiedEntry::get_mut |
| 994 |
pub fn into_mut(self) -> &'a mut V { |
| 995 |
let index = self.occupied.get().index(); |
| 996 |
&mut self.slots[index].value |
| 997 |
} |
| 998 |
|
| 999 |
/// Sets the value of the entry with the `OccupiedEntry`’s key, |
| 1000 |
/// and returns the entry’s old value. |
| 1001 |
pub fn insert(&mut self, value: V) -> V |
| 1002 |
where |
| 1003 |
K: Clone, |
| 1004 |
{ |
| 1005 |
let index = self.occupied.get().index(); |
| 1006 |
let key = self.key().clone(); |
| 1007 |
let new_slot = Slot::new(key, value); |
| 1008 |
let old_slot = replace(&mut self.slots[index], new_slot); |
| 1009 |
old_slot.value |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
impl<'a, K, V> fmt::Debug for OccupiedEntry<'a, K, V> |
| 1014 |
where |
| 1015 |
K: fmt::Debug + Ord, |
| 1016 |
V: fmt::Debug, |
| 1017 |
{ |
| 1018 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1019 |
f.debug_struct("OccupiedEntry") |
| 1020 |
.field("key", self.key()) |
| 1021 |
.field("value", self.get()) |
| 1022 |
.finish() |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
#[cfg(feature = "serde")] |
| 1027 |
mod serde_impls { |
| 1028 |
use super::IndexMap; |
| 1029 |
use core::fmt; |
| 1030 |
use core::marker::PhantomData; |
| 1031 |
use serde::de::{Deserialize, MapAccess, Visitor}; |
| 1032 |
use serde::ser::{Serialize, SerializeMap, Serializer}; |
| 1033 |
|
| 1034 |
impl<K, V> Serialize for IndexMap<K, V> |
| 1035 |
where |
| 1036 |
K: Serialize + Ord, |
| 1037 |
V: Serialize, |
| 1038 |
{ |
| 1039 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 1040 |
where |
| 1041 |
S: Serializer, |
| 1042 |
{ |
| 1043 |
let mut map = serializer.serialize_map(Some(self.len()))?; |
| 1044 |
for (k, v) in self.iter() { |
| 1045 |
map.serialize_entry(k, v)?; |
| 1046 |
} |
| 1047 |
map.end() |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
impl<'a, K, V> Deserialize<'a> for IndexMap<K, V> |
| 1052 |
where |
| 1053 |
K: Deserialize<'a> + Clone + Ord, |
| 1054 |
V: Deserialize<'a>, |
| 1055 |
{ |
| 1056 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 1057 |
where |
| 1058 |
D: serde::de::Deserializer<'a>, |
| 1059 |
{ |
| 1060 |
deserializer.deserialize_map(IndexMapVisitor { |
| 1061 |
_marker: PhantomData, |
| 1062 |
}) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
struct IndexMapVisitor<K, V> { |
| 1067 |
_marker: PhantomData<fn() -> IndexMap<K, V>>, |
| 1068 |
} |
| 1069 |
|
| 1070 |
impl<'de, K, V> Visitor<'de> for IndexMapVisitor<K, V> |
| 1071 |
where |
| 1072 |
K: Deserialize<'de> + Clone + Ord, |
| 1073 |
V: Deserialize<'de>, |
| 1074 |
{ |
| 1075 |
type Value = IndexMap<K, V>; |
| 1076 |
|
| 1077 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 1078 |
formatter.write_str("a map") |
| 1079 |
} |
| 1080 |
|
| 1081 |
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error> |
| 1082 |
where |
| 1083 |
M: MapAccess<'de>, |
| 1084 |
{ |
| 1085 |
let mut map = IndexMap::with_capacity(access.size_hint().unwrap_or(0)); |
| 1086 |
|
| 1087 |
while let Some((key, value)) = access.next_entry()? { |
| 1088 |
map.insert(key, value); |
| 1089 |
} |
| 1090 |
|
| 1091 |
Ok(map) |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |