From c9b3cd47e290d7831120b6a95767802265adfd08 Mon Sep 17 00:00:00 2001 From: Piotr Czarnecki Date: Tue, 22 Mar 2016 09:45:51 +0100 Subject: [PATCH] f Put and DerefMut --- src/libstd/collections/hash/table.rs | 42 ++++++++++++++++++---------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 851a8601c24..f3ed2737c87 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -217,20 +217,30 @@ impl Deref for FullBucket where M: Deref } } -impl DerefMut for FullBucket where M: DerefMut> { - fn deref_mut(&mut self) -> &mut RawTable { - &mut self.table - } +/// `Put` is implemented for types which provide access to a table and cannot be invalidated +/// by filling a bucket. A similar implementation for `Take` is possible. +pub trait Put { + unsafe fn borrow_table_mut(&mut self) -> &mut RawTable; } -/// `Put` is implemented for types which provide access to a table and cannot be invalidated -/// by filling a bucket. A similar implementation for `Take` is possible. -pub trait Put {} -impl Put for RawTable {} -impl<'t, K, V> Put for &'t mut RawTable {} -impl Put for Bucket {} -impl Put for FullBucket {} +impl<'t, K, V> Put for &'t mut RawTable { + unsafe fn borrow_table_mut(&mut self) -> &mut RawTable { + *self + } +} + +impl Put for Bucket where M: Put { + unsafe fn borrow_table_mut(&mut self) -> &mut RawTable { + self.table.borrow_table_mut() + } +} + +impl Put for FullBucket where M: Put { + unsafe fn borrow_table_mut(&mut self) -> &mut RawTable { + self.table.borrow_table_mut() + } +} impl>> Bucket { pub fn new(table: M, hash: SafeHash) -> Bucket { @@ -332,7 +342,7 @@ impl>> EmptyBucket { } } -impl EmptyBucket where M: Deref> + DerefMut + Put { +impl EmptyBucket where M: Put { /// Puts given key and value pair, along with the key's hash, /// into this bucket in the hashtable. Note how `self` is 'moved' into /// this function, because this slot will no longer be empty when @@ -346,9 +356,9 @@ impl EmptyBucket where M: Deref> + Deref *self.raw.hash = hash.inspect(); ptr::write(self.raw.key, key); ptr::write(self.raw.val, value); - } - self.table.size += 1; + self.table.borrow_table_mut().size += 1; + } FullBucket { raw: self.raw, idx: self.idx, table: self.table } } @@ -436,7 +446,7 @@ impl<'t, K, V> FullBucket> { } } -impl FullBucket where M: Deref> + DerefMut { +impl FullBucket where M: Put { pub fn replace(&mut self, h: SafeHash, k: K, v: V) -> (SafeHash, K, V) { unsafe { let old_hash = ptr::replace(self.raw.hash as *mut SafeHash, h); @@ -446,7 +456,9 @@ impl FullBucket where M: Deref> + DerefM (old_hash, old_key, old_val) } } +} +impl FullBucket where M: Deref> + DerefMut { /// Gets mutable references to the key and value at a given index. pub fn read_mut(&mut self) -> (&mut K, &mut V) { unsafe {