Skip to content

Commit 532aa6b

Browse files
committed
Auto merge of #404 - tower120:raw_table_mut, r=Amanieu
raw_table + raw_table_mut Immutable access to `RawTable` from both `HashMap` and `HashSet`. Implements #403
2 parents d40ffa1 + de08089 commit 532aa6b

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/map.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,18 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
21232123
RawEntryBuilder { map: self }
21242124
}
21252125

2126+
/// Returns a reference to the [`RawTable`] used underneath [`HashMap`].
2127+
/// This function is only available if the `raw` feature of the crate is enabled.
2128+
///
2129+
/// See [`raw_table_mut`] for more.
2130+
///
2131+
/// [`raw_table_mut`]: Self::raw_table_mut
2132+
#[cfg(feature = "raw")]
2133+
#[cfg_attr(feature = "inline-more", inline)]
2134+
pub fn raw_table(&self) -> &RawTable<(K, V), A> {
2135+
&self.table
2136+
}
2137+
21262138
/// Returns a mutable reference to the [`RawTable`] used underneath [`HashMap`].
21272139
/// This function is only available if the `raw` feature of the crate is enabled.
21282140
///
@@ -2159,7 +2171,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
21592171
/// where
21602172
/// F: Fn(&(K, V)) -> bool,
21612173
/// {
2162-
/// let raw_table = map.raw_table();
2174+
/// let raw_table = map.raw_table_mut();
21632175
/// match raw_table.find(hash, is_match) {
21642176
/// Some(bucket) => Some(unsafe { raw_table.remove(bucket) }),
21652177
/// None => None,
@@ -2180,7 +2192,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
21802192
/// ```
21812193
#[cfg(feature = "raw")]
21822194
#[cfg_attr(feature = "inline-more", inline)]
2183-
pub fn raw_table(&mut self) -> &mut RawTable<(K, V), A> {
2195+
pub fn raw_table_mut(&mut self) -> &mut RawTable<(K, V), A> {
21842196
&mut self.table
21852197
}
21862198
}

src/set.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ where
12221222
}
12231223
}
12241224

1225-
/// Returns a mutable reference to the [`RawTable`] used underneath [`HashSet`].
1225+
/// Returns a reference to the [`RawTable`] used underneath [`HashSet`].
12261226
/// This function is only available if the `raw` feature of the crate is enabled.
12271227
///
12281228
/// # Note
@@ -1238,9 +1238,29 @@ where
12381238
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
12391239
#[cfg(feature = "raw")]
12401240
#[cfg_attr(feature = "inline-more", inline)]
1241-
pub fn raw_table(&mut self) -> &mut RawTable<(T, ()), A> {
1241+
pub fn raw_table(&self) -> &RawTable<(T, ()), A> {
12421242
self.map.raw_table()
12431243
}
1244+
1245+
/// Returns a mutable reference to the [`RawTable`] used underneath [`HashSet`].
1246+
/// This function is only available if the `raw` feature of the crate is enabled.
1247+
///
1248+
/// # Note
1249+
///
1250+
/// Calling this function is safe, but using the raw hash table API may require
1251+
/// unsafe functions or blocks.
1252+
///
1253+
/// `RawTable` API gives the lowest level of control under the set that can be useful
1254+
/// for extending the HashSet's API, but may lead to *[undefined behavior]*.
1255+
///
1256+
/// [`HashSet`]: struct.HashSet.html
1257+
/// [`RawTable`]: crate::raw::RawTable
1258+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1259+
#[cfg(feature = "raw")]
1260+
#[cfg_attr(feature = "inline-more", inline)]
1261+
pub fn raw_table_mut(&mut self) -> &mut RawTable<(T, ()), A> {
1262+
self.map.raw_table_mut()
1263+
}
12441264
}
12451265

12461266
impl<T, S, A> PartialEq for HashSet<T, S, A>

0 commit comments

Comments
 (0)