| 1 | // Copyright (c) 2016 multimap developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 |
| 4 | // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT |
| 5 | // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. All files in the project carrying such notice may not be copied, |
| 7 | // modified, or distributed except according to those terms. |
| 8 | |
| 9 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::OccupiedEntryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/OccupiedEntry# as HashMapOccupiedEntryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/OccupiedEntry#; |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::VacantEntryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/VacantEntry# as HashMapVacantEntryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/VacantEntry#; |
| 11 | |
| 12 | /// A view into a single occupied location in a MultiMap. |
| 13 | pub struct OccupiedEntryrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#<'a, K: 'a, V: 'a> { |
| 14 | #[doc(hidden)] |
| 15 | pub innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner.: HashMapOccupiedEntryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/OccupiedEntry#<'a, K, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V>>, |
| 16 | } |
| 17 | |
| 18 | /// A view into a single empty location in a MultiMap. |
| 19 | pub struct VacantEntryrust-analyzer cargo multimap 0.10.1 entry/VacantEntry#<'a, K: 'a, V: 'a> { |
| 20 | #[doc(hidden)] |
| 21 | pub innerrust-analyzer cargo multimap 0.10.1 entry/VacantEntry#inner.: HashMapVacantEntryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/VacantEntry#<'a, K, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V>>, |
| 22 | } |
| 23 | |
| 24 | /// A view into a single location in a map, which may be vacant or occupied. |
| 25 | pub enum Entryrust-analyzer cargo multimap 0.10.1 entry/Entry#<'a, K: 'a, V: 'a> { |
| 26 | /// An occupied Entry. |
| 27 | Occupiedrust-analyzer cargo multimap 0.10.1 entry/Entry#Occupied#(OccupiedEntryrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#<'a, K, V>), |
| 28 | |
| 29 | /// A vacant Entry. |
| 30 | Vacantrust-analyzer cargo multimap 0.10.1 entry/Entry#Vacant#(VacantEntryrust-analyzer cargo multimap 0.10.1 entry/VacantEntry#<'a, K, V>), |
| 31 | } |
| 32 | |
| 33 | impl<'a, K: 'a, V: 'a> OccupiedEntryrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#<'a, K, V> { |
| 34 | /// Gets a reference to the first item in value in the vector corresponding to entry. |
| 35 | /// |
| 36 | /// # Panics |
| 37 | /// |
| 38 | /// This method will panic if the key has zero values. |
| 39 | pub fn getrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]get().(&self) -> &V { |
| 40 | self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]get().().firstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("no values in entry") |
| 41 | } |
| 42 | |
| 43 | /// Gets a reference to the values (vector) corresponding to entry. |
| 44 | pub fn get_vecrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]get_vec().(&self) -> &Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V> { |
| 45 | self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]get().() |
| 46 | } |
| 47 | |
| 48 | /// Gets a mut reference to the first item in value in the vector corresponding to entry. |
| 49 | /// |
| 50 | /// # Panics |
| 51 | /// |
| 52 | /// This method will panic if the key has zero values. |
| 53 | pub fn get_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]get_mut().(&mut self) -> &mut V { |
| 54 | self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner. |
| 55 | .get_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]get_mut().() |
| 56 | .first_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first_mut().() |
| 57 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("no values in entry") |
| 58 | } |
| 59 | |
| 60 | /// Gets a mut reference to the values (vector) corresponding to entry. |
| 61 | pub fn get_vec_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]get_vec_mut().(&mut self) -> &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V> { |
| 62 | self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner..get_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]get_mut().() |
| 63 | } |
| 64 | |
| 65 | /// Converts the OccupiedEntry into a mutable reference to the first item in value in the entry |
| 66 | /// with a lifetime bound to the map itself |
| 67 | pub fn into_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]into_mut().(self) -> &'a mut V { |
| 68 | &mut self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner..into_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]into_mut().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). |
| 69 | } |
| 70 | |
| 71 | /// Converts the OccupiedEntry into a mutable reference to the values (vector) in the entry |
| 72 | /// with a lifetime bound to the map itself |
| 73 | pub fn into_vec_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]into_vec_mut().(self) -> &'a mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V> { |
| 74 | self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner..into_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]into_mut().() |
| 75 | } |
| 76 | |
| 77 | /// Inserts a new value onto the vector of the entry. |
| 78 | pub fn insertrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]insert().(&mut self, value: V) { |
| 79 | self.get_vec_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]get_vec_mut().().pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(value); |
| 80 | } |
| 81 | |
| 82 | /// Extends the existing vector with the specified values. |
| 83 | pub fn insert_vecrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]insert_vec().(&mut self, values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V>) { |
| 84 | self.get_vec_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]get_vec_mut().().extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(values); |
| 85 | } |
| 86 | |
| 87 | /// Takes the values (vector) out of the entry, and returns it |
| 88 | pub fn removerust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]remove().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V> { |
| 89 | self.innerrust-analyzer cargo multimap 0.10.1 entry/OccupiedEntry#inner..removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`OccupiedEntry<'a, K, V, A>`]remove().() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl<'a, K: 'a, V: 'a> VacantEntryrust-analyzer cargo multimap 0.10.1 entry/VacantEntry#<'a, K, V> { |
| 94 | /// Sets the first value in the vector of the entry with the VacantEntry's key, |
| 95 | /// and returns a mutable reference to it. |
| 96 | pub fn insertrust-analyzer cargo multimap 0.10.1 entry/impl#[`VacantEntry<'a, K, V>`]insert().(self, value: V) -> &'a mut V { |
| 97 | &mut self.innerrust-analyzer cargo multimap 0.10.1 entry/VacantEntry#inner..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`VacantEntry<'a, K, V, A>`]insert().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![value])[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). |
| 98 | } |
| 99 | |
| 100 | /// Sets values in the entry with the VacantEntry's key, |
| 101 | /// and returns a mutable reference to it. |
| 102 | pub fn insert_vecrust-analyzer cargo multimap 0.10.1 entry/impl#[`VacantEntry<'a, K, V>`]insert_vec().(self, values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V>) -> &'a mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V> { |
| 103 | self.innerrust-analyzer cargo multimap 0.10.1 entry/VacantEntry#inner..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`VacantEntry<'a, K, V, A>`]insert().(values) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl<'a, K: 'a, V: 'a> Entryrust-analyzer cargo multimap 0.10.1 entry/Entry#<'a, K, V> { |
| 108 | /// Ensures a value is in the entry by inserting the default if empty, and returns |
| 109 | /// a mutable reference to the value in the entry. This will return a mutable reference to the |
| 110 | /// first value in the vector corresponding to the specified key. |
| 111 | pub fn or_insertrust-analyzer cargo multimap 0.10.1 entry/impl#[`Entry<'a, K, V>`]or_insert().(self, default: V) -> &'a mut V { |
| 112 | match self { |
| 113 | Entryrust-analyzer cargo multimap 0.10.1 entry/Entry#::Occupiedrust-analyzer cargo multimap 0.10.1 entry/Entry#Occupied#(entry) => entry.into_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]into_mut().(), |
| 114 | Entryrust-analyzer cargo multimap 0.10.1 entry/Entry#::Vacantrust-analyzer cargo multimap 0.10.1 entry/Entry#Vacant#(entry) => entry.insertrust-analyzer cargo multimap 0.10.1 entry/impl#[`VacantEntry<'a, K, V>`]insert().(default), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// Ensures a value is in the entry by inserting the default values if empty, and returns |
| 119 | /// a mutable reference to the values (the corresponding vector to the specified key) in |
| 120 | /// the entry. |
| 121 | pub fn or_insert_vecrust-analyzer cargo multimap 0.10.1 entry/impl#[`Entry<'a, K, V>`]or_insert_vec().(self, defaults: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V>) -> &'a mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<V> { |
| 122 | match self { |
| 123 | Entryrust-analyzer cargo multimap 0.10.1 entry/Entry#::Occupiedrust-analyzer cargo multimap 0.10.1 entry/Entry#Occupied#(entry) => entry.into_vec_mutrust-analyzer cargo multimap 0.10.1 entry/impl#[`OccupiedEntry<'a, K, V>`]into_vec_mut().(), |
| 124 | Entryrust-analyzer cargo multimap 0.10.1 entry/Entry#::Vacantrust-analyzer cargo multimap 0.10.1 entry/Entry#Vacant#(entry) => entry.insert_vecrust-analyzer cargo multimap 0.10.1 entry/impl#[`VacantEntry<'a, K, V>`]insert_vec().(defaults), |
| 125 | } |
| 126 | } |
| 127 | } |