@@ -1278,69 +1278,6 @@ where
1278
1278
}
1279
1279
}
1280
1280
1281
- impl < K , V , S > HashMap < K , V , S >
1282
- where
1283
- S : BuildHasher ,
1284
- {
1285
- /// Creates a raw entry builder for the `HashMap`.
1286
- ///
1287
- /// Raw entries provide the lowest level of control for searching and
1288
- /// manipulating a map. They must be manually initialized with a hash and
1289
- /// then manually searched. After this, insertions into a vacant entry
1290
- /// still require an owned key to be provided.
1291
- ///
1292
- /// Raw entries are useful for such exotic situations as:
1293
- ///
1294
- /// * Hash memoization
1295
- /// * Deferring the creation of an owned key until it is known to be required
1296
- /// * Using a search key that doesn't work with the Borrow trait
1297
- /// * Using custom comparison logic without newtype wrappers
1298
- ///
1299
- /// Because raw entries provide much more low-level control, it's much easier
1300
- /// to put the `HashMap` into an inconsistent state which, while memory-safe,
1301
- /// will cause the map to produce seemingly random results. Higher-level and
1302
- /// more foolproof APIs like `entry` should be preferred when possible.
1303
- ///
1304
- /// In particular, the hash used to initialize the raw entry must still be
1305
- /// consistent with the hash of the key that is ultimately stored in the entry.
1306
- /// This is because implementations of `HashMap` may need to recompute hashes
1307
- /// when resizing, at which point only the keys are available.
1308
- ///
1309
- /// Raw entries give mutable access to the keys. This must not be used
1310
- /// to modify how the key would compare or hash, as the map will not re-evaluate
1311
- /// where the key should go, meaning the keys may become "lost" if their
1312
- /// location does not reflect their state. For instance, if you change a key
1313
- /// so that the map now contains keys which compare equal, search may start
1314
- /// acting erratically, with two keys randomly masking each other. Implementations
1315
- /// are free to assume this doesn't happen (within the limits of memory-safety).
1316
- #[ inline]
1317
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1318
- pub fn raw_entry_mut ( & mut self ) -> RawEntryBuilderMut < ' _ , K , V , S > {
1319
- RawEntryBuilderMut { map : self }
1320
- }
1321
-
1322
- /// Creates a raw immutable entry builder for the `HashMap`.
1323
- ///
1324
- /// Raw entries provide the lowest level of control for searching and
1325
- /// manipulating a map. They must be manually initialized with a hash and
1326
- /// then manually searched.
1327
- ///
1328
- /// This is useful for
1329
- /// * Hash memoization
1330
- /// * Using a search key that doesn't work with the Borrow trait
1331
- /// * Using custom comparison logic without newtype wrappers
1332
- ///
1333
- /// Unless you are in such a situation, higher-level and more foolproof APIs like
1334
- /// `get` should be preferred.
1335
- ///
1336
- /// Immutable raw entries have very limited use; you might instead want `raw_entry_mut`.
1337
- #[ inline]
1338
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1339
- pub fn raw_entry ( & self ) -> RawEntryBuilder < ' _ , K , V , S > {
1340
- RawEntryBuilder { map : self }
1341
- }
1342
- }
1343
-
1344
1281
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1345
1282
impl < K , V , S > Clone for HashMap < K , V , S >
1346
1283
where
@@ -1828,404 +1765,6 @@ impl<K, V> Default for IntoValues<K, V> {
1828
1765
}
1829
1766
}
1830
1767
1831
- /// A builder for computing where in a HashMap a key-value pair would be stored.
1832
- ///
1833
- /// See the [`HashMap::raw_entry_mut`] docs for usage examples.
1834
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1835
- pub struct RawEntryBuilderMut < ' a , K : ' a , V : ' a , S : ' a > {
1836
- map : & ' a mut HashMap < K , V , S > ,
1837
- }
1838
-
1839
- /// A view into a single entry in a map, which may either be vacant or occupied.
1840
- ///
1841
- /// This is a lower-level version of [`Entry`].
1842
- ///
1843
- /// This `enum` is constructed through the [`raw_entry_mut`] method on [`HashMap`],
1844
- /// then calling one of the methods of that [`RawEntryBuilderMut`].
1845
- ///
1846
- /// [`raw_entry_mut`]: HashMap::raw_entry_mut
1847
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1848
- pub enum RawEntryMut < ' a , K : ' a , V : ' a , S : ' a > {
1849
- /// An occupied entry.
1850
- Occupied ( RawOccupiedEntryMut < ' a , K , V , S > ) ,
1851
- /// A vacant entry.
1852
- Vacant ( RawVacantEntryMut < ' a , K , V , S > ) ,
1853
- }
1854
-
1855
- /// A view into an occupied entry in a `HashMap`.
1856
- /// It is part of the [`RawEntryMut`] enum.
1857
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1858
- pub struct RawOccupiedEntryMut < ' a , K : ' a , V : ' a , S : ' a > {
1859
- base : base:: RawOccupiedEntryMut < ' a , K , V , S > ,
1860
- }
1861
-
1862
- /// A view into a vacant entry in a `HashMap`.
1863
- /// It is part of the [`RawEntryMut`] enum.
1864
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1865
- pub struct RawVacantEntryMut < ' a , K : ' a , V : ' a , S : ' a > {
1866
- base : base:: RawVacantEntryMut < ' a , K , V , S > ,
1867
- }
1868
-
1869
- /// A builder for computing where in a HashMap a key-value pair would be stored.
1870
- ///
1871
- /// See the [`HashMap::raw_entry`] docs for usage examples.
1872
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1873
- pub struct RawEntryBuilder < ' a , K : ' a , V : ' a , S : ' a > {
1874
- map : & ' a HashMap < K , V , S > ,
1875
- }
1876
-
1877
- impl < ' a , K , V , S > RawEntryBuilderMut < ' a , K , V , S >
1878
- where
1879
- S : BuildHasher ,
1880
- {
1881
- /// Creates a `RawEntryMut` from the given key.
1882
- #[ inline]
1883
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1884
- pub fn from_key < Q : ?Sized > ( self , k : & Q ) -> RawEntryMut < ' a , K , V , S >
1885
- where
1886
- K : Borrow < Q > ,
1887
- Q : Hash + Eq ,
1888
- {
1889
- map_raw_entry ( self . map . base . raw_entry_mut ( ) . from_key ( k) )
1890
- }
1891
-
1892
- /// Creates a `RawEntryMut` from the given key and its hash.
1893
- #[ inline]
1894
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1895
- pub fn from_key_hashed_nocheck < Q : ?Sized > ( self , hash : u64 , k : & Q ) -> RawEntryMut < ' a , K , V , S >
1896
- where
1897
- K : Borrow < Q > ,
1898
- Q : Eq ,
1899
- {
1900
- map_raw_entry ( self . map . base . raw_entry_mut ( ) . from_key_hashed_nocheck ( hash, k) )
1901
- }
1902
-
1903
- /// Creates a `RawEntryMut` from the given hash.
1904
- #[ inline]
1905
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1906
- pub fn from_hash < F > ( self , hash : u64 , is_match : F ) -> RawEntryMut < ' a , K , V , S >
1907
- where
1908
- for < ' b > F : FnMut ( & ' b K ) -> bool ,
1909
- {
1910
- map_raw_entry ( self . map . base . raw_entry_mut ( ) . from_hash ( hash, is_match) )
1911
- }
1912
- }
1913
-
1914
- impl < ' a , K , V , S > RawEntryBuilder < ' a , K , V , S >
1915
- where
1916
- S : BuildHasher ,
1917
- {
1918
- /// Access an entry by key.
1919
- #[ inline]
1920
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1921
- pub fn from_key < Q : ?Sized > ( self , k : & Q ) -> Option < ( & ' a K , & ' a V ) >
1922
- where
1923
- K : Borrow < Q > ,
1924
- Q : Hash + Eq ,
1925
- {
1926
- self . map . base . raw_entry ( ) . from_key ( k)
1927
- }
1928
-
1929
- /// Access an entry by a key and its hash.
1930
- #[ inline]
1931
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1932
- pub fn from_key_hashed_nocheck < Q : ?Sized > ( self , hash : u64 , k : & Q ) -> Option < ( & ' a K , & ' a V ) >
1933
- where
1934
- K : Borrow < Q > ,
1935
- Q : Hash + Eq ,
1936
- {
1937
- self . map . base . raw_entry ( ) . from_key_hashed_nocheck ( hash, k)
1938
- }
1939
-
1940
- /// Access an entry by hash.
1941
- #[ inline]
1942
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1943
- pub fn from_hash < F > ( self , hash : u64 , is_match : F ) -> Option < ( & ' a K , & ' a V ) >
1944
- where
1945
- F : FnMut ( & K ) -> bool ,
1946
- {
1947
- self . map . base . raw_entry ( ) . from_hash ( hash, is_match)
1948
- }
1949
- }
1950
-
1951
- impl < ' a , K , V , S > RawEntryMut < ' a , K , V , S > {
1952
- /// Ensures a value is in the entry by inserting the default if empty, and returns
1953
- /// mutable references to the key and value in the entry.
1954
- ///
1955
- /// # Examples
1956
- ///
1957
- /// ```
1958
- /// #![feature(hash_raw_entry)]
1959
- /// use std::collections::HashMap;
1960
- ///
1961
- /// let mut map: HashMap<&str, u32> = HashMap::new();
1962
- ///
1963
- /// map.raw_entry_mut().from_key("poneyland").or_insert("poneyland", 3);
1964
- /// assert_eq!(map["poneyland"], 3);
1965
- ///
1966
- /// *map.raw_entry_mut().from_key("poneyland").or_insert("poneyland", 10).1 *= 2;
1967
- /// assert_eq!(map["poneyland"], 6);
1968
- /// ```
1969
- #[ inline]
1970
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
1971
- pub fn or_insert ( self , default_key : K , default_val : V ) -> ( & ' a mut K , & ' a mut V )
1972
- where
1973
- K : Hash ,
1974
- S : BuildHasher ,
1975
- {
1976
- match self {
1977
- RawEntryMut :: Occupied ( entry) => entry. into_key_value ( ) ,
1978
- RawEntryMut :: Vacant ( entry) => entry. insert ( default_key, default_val) ,
1979
- }
1980
- }
1981
-
1982
- /// Ensures a value is in the entry by inserting the result of the default function if empty,
1983
- /// and returns mutable references to the key and value in the entry.
1984
- ///
1985
- /// # Examples
1986
- ///
1987
- /// ```
1988
- /// #![feature(hash_raw_entry)]
1989
- /// use std::collections::HashMap;
1990
- ///
1991
- /// let mut map: HashMap<&str, String> = HashMap::new();
1992
- ///
1993
- /// map.raw_entry_mut().from_key("poneyland").or_insert_with(|| {
1994
- /// ("poneyland", "hoho".to_string())
1995
- /// });
1996
- ///
1997
- /// assert_eq!(map["poneyland"], "hoho".to_string());
1998
- /// ```
1999
- #[ inline]
2000
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2001
- pub fn or_insert_with < F > ( self , default : F ) -> ( & ' a mut K , & ' a mut V )
2002
- where
2003
- F : FnOnce ( ) -> ( K , V ) ,
2004
- K : Hash ,
2005
- S : BuildHasher ,
2006
- {
2007
- match self {
2008
- RawEntryMut :: Occupied ( entry) => entry. into_key_value ( ) ,
2009
- RawEntryMut :: Vacant ( entry) => {
2010
- let ( k, v) = default ( ) ;
2011
- entry. insert ( k, v)
2012
- }
2013
- }
2014
- }
2015
-
2016
- /// Provides in-place mutable access to an occupied entry before any
2017
- /// potential inserts into the map.
2018
- ///
2019
- /// # Examples
2020
- ///
2021
- /// ```
2022
- /// #![feature(hash_raw_entry)]
2023
- /// use std::collections::HashMap;
2024
- ///
2025
- /// let mut map: HashMap<&str, u32> = HashMap::new();
2026
- ///
2027
- /// map.raw_entry_mut()
2028
- /// .from_key("poneyland")
2029
- /// .and_modify(|_k, v| { *v += 1 })
2030
- /// .or_insert("poneyland", 42);
2031
- /// assert_eq!(map["poneyland"], 42);
2032
- ///
2033
- /// map.raw_entry_mut()
2034
- /// .from_key("poneyland")
2035
- /// .and_modify(|_k, v| { *v += 1 })
2036
- /// .or_insert("poneyland", 0);
2037
- /// assert_eq!(map["poneyland"], 43);
2038
- /// ```
2039
- #[ inline]
2040
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2041
- pub fn and_modify < F > ( self , f : F ) -> Self
2042
- where
2043
- F : FnOnce ( & mut K , & mut V ) ,
2044
- {
2045
- match self {
2046
- RawEntryMut :: Occupied ( mut entry) => {
2047
- {
2048
- let ( k, v) = entry. get_key_value_mut ( ) ;
2049
- f ( k, v) ;
2050
- }
2051
- RawEntryMut :: Occupied ( entry)
2052
- }
2053
- RawEntryMut :: Vacant ( entry) => RawEntryMut :: Vacant ( entry) ,
2054
- }
2055
- }
2056
- }
2057
-
2058
- impl < ' a , K , V , S > RawOccupiedEntryMut < ' a , K , V , S > {
2059
- /// Gets a reference to the key in the entry.
2060
- #[ inline]
2061
- #[ must_use]
2062
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2063
- pub fn key ( & self ) -> & K {
2064
- self . base . key ( )
2065
- }
2066
-
2067
- /// Gets a mutable reference to the key in the entry.
2068
- #[ inline]
2069
- #[ must_use]
2070
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2071
- pub fn key_mut ( & mut self ) -> & mut K {
2072
- self . base . key_mut ( )
2073
- }
2074
-
2075
- /// Converts the entry into a mutable reference to the key in the entry
2076
- /// with a lifetime bound to the map itself.
2077
- #[ inline]
2078
- #[ must_use = "`self` will be dropped if the result is not used" ]
2079
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2080
- pub fn into_key ( self ) -> & ' a mut K {
2081
- self . base . into_key ( )
2082
- }
2083
-
2084
- /// Gets a reference to the value in the entry.
2085
- #[ inline]
2086
- #[ must_use]
2087
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2088
- pub fn get ( & self ) -> & V {
2089
- self . base . get ( )
2090
- }
2091
-
2092
- /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
2093
- /// with a lifetime bound to the map itself.
2094
- #[ inline]
2095
- #[ must_use = "`self` will be dropped if the result is not used" ]
2096
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2097
- pub fn into_mut ( self ) -> & ' a mut V {
2098
- self . base . into_mut ( )
2099
- }
2100
-
2101
- /// Gets a mutable reference to the value in the entry.
2102
- #[ inline]
2103
- #[ must_use]
2104
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2105
- pub fn get_mut ( & mut self ) -> & mut V {
2106
- self . base . get_mut ( )
2107
- }
2108
-
2109
- /// Gets a reference to the key and value in the entry.
2110
- #[ inline]
2111
- #[ must_use]
2112
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2113
- pub fn get_key_value ( & mut self ) -> ( & K , & V ) {
2114
- self . base . get_key_value ( )
2115
- }
2116
-
2117
- /// Gets a mutable reference to the key and value in the entry.
2118
- #[ inline]
2119
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2120
- pub fn get_key_value_mut ( & mut self ) -> ( & mut K , & mut V ) {
2121
- self . base . get_key_value_mut ( )
2122
- }
2123
-
2124
- /// Converts the `OccupiedEntry` into a mutable reference to the key and value in the entry
2125
- /// with a lifetime bound to the map itself.
2126
- #[ inline]
2127
- #[ must_use = "`self` will be dropped if the result is not used" ]
2128
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2129
- pub fn into_key_value ( self ) -> ( & ' a mut K , & ' a mut V ) {
2130
- self . base . into_key_value ( )
2131
- }
2132
-
2133
- /// Sets the value of the entry, and returns the entry's old value.
2134
- #[ inline]
2135
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2136
- pub fn insert ( & mut self , value : V ) -> V {
2137
- self . base . insert ( value)
2138
- }
2139
-
2140
- /// Sets the value of the entry, and returns the entry's old value.
2141
- #[ inline]
2142
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2143
- pub fn insert_key ( & mut self , key : K ) -> K {
2144
- self . base . insert_key ( key)
2145
- }
2146
-
2147
- /// Takes the value out of the entry, and returns it.
2148
- #[ inline]
2149
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2150
- pub fn remove ( self ) -> V {
2151
- self . base . remove ( )
2152
- }
2153
-
2154
- /// Take the ownership of the key and value from the map.
2155
- #[ inline]
2156
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2157
- pub fn remove_entry ( self ) -> ( K , V ) {
2158
- self . base . remove_entry ( )
2159
- }
2160
- }
2161
-
2162
- impl < ' a , K , V , S > RawVacantEntryMut < ' a , K , V , S > {
2163
- /// Sets the value of the entry with the `VacantEntry`'s key,
2164
- /// and returns a mutable reference to it.
2165
- #[ inline]
2166
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2167
- pub fn insert ( self , key : K , value : V ) -> ( & ' a mut K , & ' a mut V )
2168
- where
2169
- K : Hash ,
2170
- S : BuildHasher ,
2171
- {
2172
- self . base . insert ( key, value)
2173
- }
2174
-
2175
- /// Sets the value of the entry with the VacantEntry's key,
2176
- /// and returns a mutable reference to it.
2177
- #[ inline]
2178
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2179
- pub fn insert_hashed_nocheck ( self , hash : u64 , key : K , value : V ) -> ( & ' a mut K , & ' a mut V )
2180
- where
2181
- K : Hash ,
2182
- S : BuildHasher ,
2183
- {
2184
- self . base . insert_hashed_nocheck ( hash, key, value)
2185
- }
2186
- }
2187
-
2188
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2189
- impl < K , V , S > Debug for RawEntryBuilderMut < ' _ , K , V , S > {
2190
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2191
- f. debug_struct ( "RawEntryBuilder" ) . finish_non_exhaustive ( )
2192
- }
2193
- }
2194
-
2195
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2196
- impl < K : Debug , V : Debug , S > Debug for RawEntryMut < ' _ , K , V , S > {
2197
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2198
- match * self {
2199
- RawEntryMut :: Vacant ( ref v) => f. debug_tuple ( "RawEntry" ) . field ( v) . finish ( ) ,
2200
- RawEntryMut :: Occupied ( ref o) => f. debug_tuple ( "RawEntry" ) . field ( o) . finish ( ) ,
2201
- }
2202
- }
2203
- }
2204
-
2205
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2206
- impl < K : Debug , V : Debug , S > Debug for RawOccupiedEntryMut < ' _ , K , V , S > {
2207
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2208
- f. debug_struct ( "RawOccupiedEntryMut" )
2209
- . field ( "key" , self . key ( ) )
2210
- . field ( "value" , self . get ( ) )
2211
- . finish_non_exhaustive ( )
2212
- }
2213
- }
2214
-
2215
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2216
- impl < K , V , S > Debug for RawVacantEntryMut < ' _ , K , V , S > {
2217
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2218
- f. debug_struct ( "RawVacantEntryMut" ) . finish_non_exhaustive ( )
2219
- }
2220
- }
2221
-
2222
- #[ unstable( feature = "hash_raw_entry" , issue = "56167" ) ]
2223
- impl < K , V , S > Debug for RawEntryBuilder < ' _ , K , V , S > {
2224
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2225
- f. debug_struct ( "RawEntryBuilder" ) . finish_non_exhaustive ( )
2226
- }
2227
- }
2228
-
2229
1768
/// A view into a single entry in a map, which may either be vacant or occupied.
2230
1769
///
2231
1770
/// This `enum` is constructed from the [`entry`] method on [`HashMap`].
@@ -3298,16 +2837,6 @@ pub(super) fn map_try_reserve_error(err: hashbrown::TryReserveError) -> TryReser
3298
2837
}
3299
2838
}
3300
2839
3301
- #[ inline]
3302
- fn map_raw_entry < ' a , K : ' a , V : ' a , S : ' a > (
3303
- raw : base:: RawEntryMut < ' a , K , V , S > ,
3304
- ) -> RawEntryMut < ' a , K , V , S > {
3305
- match raw {
3306
- base:: RawEntryMut :: Occupied ( base) => RawEntryMut :: Occupied ( RawOccupiedEntryMut { base } ) ,
3307
- base:: RawEntryMut :: Vacant ( base) => RawEntryMut :: Vacant ( RawVacantEntryMut { base } ) ,
3308
- }
3309
- }
3310
-
3311
2840
#[ allow( dead_code) ]
3312
2841
fn assert_covariance ( ) {
3313
2842
fn map_key < ' new > ( v : HashMap < & ' static str , u8 > ) -> HashMap < & ' new str , u8 > {
0 commit comments