Skip to content

Commit c9b3cd4

Browse files
committed
f Put and DerefMut
1 parent c21d975 commit c9b3cd4

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/libstd/collections/hash/table.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,30 @@ impl<K, V, M> Deref for FullBucket<K, V, M> where M: Deref<Target=RawTable<K, V>
217217
}
218218
}
219219

220-
impl<K, V, M> DerefMut for FullBucket<K, V, M> where M: DerefMut<Target=RawTable<K, V>> {
221-
fn deref_mut(&mut self) -> &mut RawTable<K, V> {
222-
&mut self.table
220+
/// `Put` is implemented for types which provide access to a table and cannot be invalidated
221+
/// by filling a bucket. A similar implementation for `Take` is possible.
222+
pub trait Put<K, V> {
223+
unsafe fn borrow_table_mut(&mut self) -> &mut RawTable<K, V>;
224+
}
225+
226+
227+
impl<'t, K, V> Put<K, V> for &'t mut RawTable<K, V> {
228+
unsafe fn borrow_table_mut(&mut self) -> &mut RawTable<K, V> {
229+
*self
223230
}
224231
}
225232

233+
impl<K, V, M> Put<K, V> for Bucket<K, V, M> where M: Put<K, V> {
234+
unsafe fn borrow_table_mut(&mut self) -> &mut RawTable<K, V> {
235+
self.table.borrow_table_mut()
236+
}
237+
}
226238

227-
/// `Put` is implemented for types which provide access to a table and cannot be invalidated
228-
/// by filling a bucket. A similar implementation for `Take` is possible.
229-
pub trait Put {}
230-
impl<K, V> Put for RawTable<K, V> {}
231-
impl<'t, K, V> Put for &'t mut RawTable<K, V> {}
232-
impl<K, V, M: Put> Put for Bucket<K, V, M> {}
233-
impl<K, V, M: Put> Put for FullBucket<K, V, M> {}
239+
impl<K, V, M> Put<K, V> for FullBucket<K, V, M> where M: Put<K, V> {
240+
unsafe fn borrow_table_mut(&mut self) -> &mut RawTable<K, V> {
241+
self.table.borrow_table_mut()
242+
}
243+
}
234244

235245
impl<K, V, M: Deref<Target=RawTable<K, V>>> Bucket<K, V, M> {
236246
pub fn new(table: M, hash: SafeHash) -> Bucket<K, V, M> {
@@ -332,7 +342,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>> EmptyBucket<K, V, M> {
332342
}
333343
}
334344

335-
impl<K, V, M> EmptyBucket<K, V, M> where M: Deref<Target=RawTable<K, V>> + DerefMut + Put {
345+
impl<K, V, M> EmptyBucket<K, V, M> where M: Put<K, V> {
336346
/// Puts given key and value pair, along with the key's hash,
337347
/// into this bucket in the hashtable. Note how `self` is 'moved' into
338348
/// this function, because this slot will no longer be empty when
@@ -346,9 +356,9 @@ impl<K, V, M> EmptyBucket<K, V, M> where M: Deref<Target=RawTable<K, V>> + Deref
346356
*self.raw.hash = hash.inspect();
347357
ptr::write(self.raw.key, key);
348358
ptr::write(self.raw.val, value);
349-
}
350359

351-
self.table.size += 1;
360+
self.table.borrow_table_mut().size += 1;
361+
}
352362

353363
FullBucket { raw: self.raw, idx: self.idx, table: self.table }
354364
}
@@ -436,7 +446,7 @@ impl<'t, K, V> FullBucket<K, V, &'t mut RawTable<K, V>> {
436446
}
437447
}
438448

439-
impl<K, V, M> FullBucket<K, V, M> where M: Deref<Target=RawTable<K, V>> + DerefMut {
449+
impl<K, V, M> FullBucket<K, V, M> where M: Put<K, V> {
440450
pub fn replace(&mut self, h: SafeHash, k: K, v: V) -> (SafeHash, K, V) {
441451
unsafe {
442452
let old_hash = ptr::replace(self.raw.hash as *mut SafeHash, h);
@@ -446,7 +456,9 @@ impl<K, V, M> FullBucket<K, V, M> where M: Deref<Target=RawTable<K, V>> + DerefM
446456
(old_hash, old_key, old_val)
447457
}
448458
}
459+
}
449460

461+
impl<K, V, M> FullBucket<K, V, M> where M: Deref<Target=RawTable<K, V>> + DerefMut {
450462
/// Gets mutable references to the key and value at a given index.
451463
pub fn read_mut(&mut self) -> (&mut K, &mut V) {
452464
unsafe {

0 commit comments

Comments
 (0)