|
5 | 5 |
|
6 | 6 | # Summary
|
7 | 7 |
|
8 |
| -Add element-recovery methods to the set types in `std`. Add key-recovery methods to the map types |
9 |
| -in `std` in order to facilitate this. |
| 8 | +Add element-recovery methods to the set types in `std`. |
10 | 9 |
|
11 | 10 | # Motivation
|
12 | 11 |
|
@@ -87,83 +86,21 @@ Add the following element-recovery methods to `std::collections::{BTreeSet, Hash
|
87 | 86 | ```rust
|
88 | 87 | impl<T> Set<T> {
|
89 | 88 | // Like `contains`, but returns a reference to the element if the set contains it.
|
90 |
| - fn element<Q: ?Sized>(&self, element: &Q) -> Option<&T>; |
| 89 | + fn get<Q: ?Sized>(&self, element: &Q) -> Option<&T>; |
91 | 90 |
|
92 | 91 | // Like `remove`, but returns the element if the set contained it.
|
93 |
| - fn remove_element<Q: ?Sized>(&mut self, element: &Q) -> Option<T>; |
| 92 | + fn take<Q: ?Sized>(&mut self, element: &Q) -> Option<T>; |
94 | 93 |
|
95 | 94 | // Like `insert`, but replaces the element with the given one and returns the previous element
|
96 | 95 | // if the set contained it.
|
97 | 96 | fn replace(&mut self, element: T) -> Option<T>;
|
98 | 97 | }
|
99 | 98 | ```
|
100 | 99 |
|
101 |
| -In order to implement the above methods, add the following key-recovery methods to |
102 |
| -`std::collections::{BTreeMap, HashMap}`: |
103 |
| - |
104 |
| -```rust |
105 |
| -impl<K, V> Map<K, V> { |
106 |
| - // Like `get`, but additionally returns a reference to the entry's key. |
107 |
| - fn key_value<Q: ?Sized>(&self, key: &Q) -> Option<(&K, &V)>; |
108 |
| - |
109 |
| - // Like `get_mut`, but additionally returns a reference to the entry's key. |
110 |
| - fn key_value_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<(&K, &mut V)>; |
111 |
| - |
112 |
| - // Like `remove`, but additionally returns the entry's key. |
113 |
| - fn remove_key_value<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>; |
114 |
| - |
115 |
| - // Like `insert`, but additionally replaces the key with the given one and returns the previous |
116 |
| - // key and value if the map contained it. |
117 |
| - fn replace(&mut self, key: K, value: V) -> Option<(K, V)>; |
118 |
| -} |
119 |
| -``` |
120 |
| - |
121 |
| -Add the following key-recovery methods to `std::collections::{btree_map, hash_map}::OccupiedEntry`: |
122 |
| - |
123 |
| -```rust |
124 |
| -impl<'a, K, V> OccupiedEntry<'a, K, V> { |
125 |
| - // Like `get`, but additionally returns a reference to the entry's key. |
126 |
| - fn key_value(&self) -> (&K, &V); |
127 |
| - |
128 |
| - // Like `get_mut`, but additionally returns a reference to the entry's key. |
129 |
| - fn key_value_mut(&mut self) -> (&K, &mut V); |
130 |
| - |
131 |
| - // Like `into_mut`, but additionally returns a reference to the entry's key. |
132 |
| - fn into_key_value_mut(self) -> (&'a K, &'a mut V); |
133 |
| - |
134 |
| - // Like `remove`, but additionally returns the entry's key. |
135 |
| - fn remove_key_value(self) -> (K, V); |
136 |
| -} |
137 |
| -``` |
138 |
| - |
139 |
| -Add the following key-recovery methods to `std::collections::{btree_map, hash_map}::VacantEntry`: |
140 |
| - |
141 |
| -```rust |
142 |
| -impl<'a, K, V> VacantEntry<'a, K, V> { |
143 |
| - /// Returns a reference to the entry's key. |
144 |
| - fn key(&self) -> &K; |
145 |
| - |
146 |
| - // Like `insert`, but additionally returns a reference to the entry's key. |
147 |
| - fn insert_key_value(self, value: V) -> (&'a K, &'a mut V); |
148 |
| - |
149 |
| - // Returns the entry's key without inserting it into the map. |
150 |
| - fn into_key(self) -> K; |
151 |
| -} |
152 |
| -``` |
153 |
| - |
154 | 100 | # Drawbacks
|
155 | 101 |
|
156 | 102 | This complicates the collection APIs.
|
157 | 103 |
|
158 |
| -The distinction between `insert` and `replace` may be confusing. It would be more consistent to |
159 |
| -call `Set::replace` `Set::insert_element` and `Map::replace` `Map::insert_key_value`, but |
160 |
| -`BTreeMap` and `HashMap` do not replace equivalent keys in their `insert` methods, so rather than |
161 |
| -have `insert` and `insert_key_value` behave differently in that respect, `replace` is used instead. |
162 |
| - |
163 | 104 | # Alternatives
|
164 | 105 |
|
165 | 106 | Do nothing.
|
166 |
| - |
167 |
| -# Unresolved questions |
168 |
| - |
169 |
| -Are these the best method names? |
0 commit comments