Debian Codegraph @

src/entry.rs

raw symbols SCIP
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 std::collections::hash_map::OccupiedEntry as HashMapOccupiedEntry;
10 use std::collections::hash_map::VacantEntry as HashMapVacantEntry;
11
12 /// A view into a single occupied location in a MultiMap.
13 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
14 #[doc(hidden)]
15 pub inner: HashMapOccupiedEntry<'a, K, Vec<V>>,
16 }
17
18 /// A view into a single empty location in a MultiMap.
19 pub struct VacantEntry<'a, K: 'a, V: 'a> {
20 #[doc(hidden)]
21 pub inner: HashMapVacantEntry<'a, K, Vec<V>>,
22 }
23
24 /// A view into a single location in a map, which may be vacant or occupied.
25 pub enum Entry<'a, K: 'a, V: 'a> {
26 /// An occupied Entry.
27 Occupied(OccupiedEntry<'a, K, V>),
28
29 /// A vacant Entry.
30 Vacant(VacantEntry<'a, K, V>),
31 }
32
33 impl<'a, K: 'a, V: 'a> 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 get(&self) -> &V {
40 self.inner.get().first().expect("no values in entry")
41 }
42
43 /// Gets a reference to the values (vector) corresponding to entry.
44 pub fn get_vec(&self) -> &Vec<V> {
45 self.inner.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_mut(&mut self) -> &mut V {
54 self.inner
55 .get_mut()
56 .first_mut()
57 .expect("no values in entry")
58 }
59
60 /// Gets a mut reference to the values (vector) corresponding to entry.
61 pub fn get_vec_mut(&mut self) -> &mut Vec<V> {
62 self.inner.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_mut(self) -> &'a mut V {
68 &mut self.inner.into_mut()[0]
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_mut(self) -> &'a mut Vec<V> {
74 self.inner.into_mut()
75 }
76
77 /// Inserts a new value onto the vector of the entry.
78 pub fn insert(&mut self, value: V) {
79 self.get_vec_mut().push(value);
80 }
81
82 /// Extends the existing vector with the specified values.
83 pub fn insert_vec(&mut self, values: Vec<V>) {
84 self.get_vec_mut().extend(values);
85 }
86
87 /// Takes the values (vector) out of the entry, and returns it
88 pub fn remove(self) -> Vec<V> {
89 self.inner.remove()
90 }
91 }
92
93 impl<'a, K: 'a, V: 'a> 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 insert(self, value: V) -> &'a mut V {
97 &mut self.inner.insert(vec![value])[0]
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_vec(self, values: Vec<V>) -> &'a mut Vec<V> {
103 self.inner.insert(values)
104 }
105 }
106
107 impl<'a, K: 'a, V: 'a> 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_insert(self, default: V) -> &'a mut V {
112 match self {
113 Entry::Occupied(entry) => entry.into_mut(),
114 Entry::Vacant(entry) => entry.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_vec(self, defaults: Vec<V>) -> &'a mut Vec<V> {
122 match self {
123 Entry::Occupied(entry) => entry.into_vec_mut(),
124 Entry::Vacant(entry) => entry.insert_vec(defaults),
125 }
126 }
127 }