1212
1313use self :: BucketState :: * ;
1414
15+ use borrow:: { BorrowFrom , BorrowFromMut } ;
1516use clone:: Clone ;
1617use cmp;
1718use hash:: { Hash , Hasher } ;
@@ -78,11 +79,16 @@ struct RawBucket<K, V, M> {
7879
7980impl < K , V , M > Copy for RawBucket < K , V , M > { }
8081
82+ #[ derive( Clone ) ]
83+ pub struct TableRef < M > ( M ) ;
84+
85+ impl < M : Copy > Copy for TableRef < M > { }
86+
8187struct BareBucket < K , V , M > {
8288 raw : RawBucket < K , V , M > ,
8389 idx : usize ,
8490 capacity : usize ,
85- table : M ,
91+ table : TableRef < M > ,
8692}
8793
8894impl < K , V , M : Copy > Copy for BareBucket < K , V , M > { }
@@ -151,11 +157,19 @@ fn can_alias_safehash_as_option() {
151157 assert_eq ! ( size_of:: <SafeHash >( ) , size_of:: <Option <SafeHash >>( ) )
152158}
153159
154- impl < K , V > Deref for RawTable < K , V > {
160+ #[ old_impl_check]
161+ impl < K , V , M > Deref for TableRef < M > where RawTable < K , V > : BorrowFrom < M > {
155162 type Target = RawTable < K , V > ;
156163
157164 fn deref ( & self ) -> & RawTable < K , V > {
158- self
165+ BorrowFrom :: borrow_from ( & mut self . 0 )
166+ }
167+ }
168+
169+ #[ old_impl_check]
170+ impl < K , V , M > DerefMut for TableRef < M > where RawTable < K , V > : BorrowFromMut < M > {
171+ fn deref_mut ( & mut self ) -> & mut RawTable < K , V > {
172+ BorrowFromMut :: borrow_from_mut ( & mut self . 0 )
159173 }
160174}
161175
@@ -168,7 +182,7 @@ impl<K, V, M> RawBucket<K, V, M> {
168182 }
169183}
170184
171- impl < ' t , K , V , M : Deref < Target = RawTable < K , V > > + ' t > RawBucket < K , V , M > {
185+ impl < ' t , K , V , M : ' t > RawBucket < K , V , M > where RawTable < K , V > : BorrowFrom < M > {
172186 pub fn into_refs ( self ) -> ( & ' t K , & ' t V ) {
173187 let & ( ref k, ref v) = unsafe {
174188 & * self . kval
@@ -178,7 +192,7 @@ impl<'t, K, V, M: Deref<Target=RawTable<K, V>> + 't> RawBucket<K, V, M> {
178192}
179193
180194impl < ' t , K , V , M > RawBucket < K , V , M >
181- where M : Deref < Target = RawTable < K , V > > + DerefMut + ' t
195+ where RawTable < K , V > : BorrowFromMut < M > , M : ' t
182196{
183197 pub fn into_mut_refs ( self ) -> ( & ' t mut K , & ' t mut V ) {
184198 let & mut ( ref mut k, ref mut v) = unsafe {
@@ -190,29 +204,31 @@ impl<'t, K, V, M> RawBucket<K, V, M>
190204
191205// Chain buckets! This is perfectly safe as long as operations on one
192206// `Bucket` can't invalidate other `Bucket`s.
193- impl < K , V , M : Deref < Target = RawTable < K , V > > > Deref for Bucket < K , V , M > {
194- type Target = RawTable < K , V > ;
195-
196- fn deref ( & self ) -> & RawTable < K , V > {
197- & * self . 0 . table
207+ impl < K , V , M > BorrowFrom < Bucket < K , V , M > > for RawTable < K , V >
208+ where RawTable < K , V > : BorrowFrom < M >
209+ {
210+ fn borrow_from ( bucket : & Bucket < K , V , M > ) -> & RawTable < K , V > {
211+ BorrowFrom :: borrow_from ( & bucket . 0 . table . 0 )
198212 }
199213}
200214
201- impl < K , V , M : DerefMut < Target =RawTable < K , V > > > DerefMut for Bucket < K , V , M > {
202- fn deref_mut ( & mut self ) -> & mut RawTable < K , V > {
203- & mut * self . 0 . table
215+ impl < K , V , M > BorrowFromMut < Bucket < K , V , M > > for RawTable < K , V >
216+ where RawTable < K , V > : BorrowFromMut < M >
217+ {
218+ fn borrow_from_mut ( bucket : & mut Bucket < K , V , M > ) -> & mut RawTable < K , V > {
219+ BorrowFromMut :: borrow_from_mut ( & mut bucket. 0 . table . 0 )
204220 }
205221}
206222
207223// Buckets hold references to the table.
208224impl < K , V , M , S > Bucket < K , V , M , S > {
209225 /// Borrow a reference to the table.
210226 pub fn table ( & self ) -> & M {
211- & self . 0 . table
227+ & self . 0 . table . 0
212228 }
213229 /// Move out the reference to the table.
214230 pub fn into_table ( self ) -> M {
215- self . 0 . table
231+ self . 0 . table . 0
216232 }
217233 /// Get the raw index.
218234 pub fn index ( & self ) -> uint {
@@ -221,23 +237,24 @@ impl<K, V, M, S> Bucket<K, V, M, S> {
221237 /// Turn the bucket into an iterator over full buckets.
222238 pub fn into_iter ( self ) -> RawFullBuckets < K , V , M > {
223239 RawFullBuckets {
224- raw : self . raw ,
240+ raw : self . 0 . raw ,
225241 hashes_end : unsafe {
226- self . raw . hash . offset ( self . 0 . capacity as int - self . index ( ) as int )
242+ self . 0 . raw . hash . offset ( self . 0 . capacity as int - self . index ( ) as int )
227243 } ,
228- table : self . table ,
244+ table : self . 0 . table ,
229245 }
230246 }
231247}
232248
233- impl < K , V , M : Deref < Target = RawTable < K , V > > > Bucket < K , V , M > {
249+ impl < K , V , M > Bucket < K , V , M > where RawTable < K , V > : BorrowFrom < M > {
234250 pub fn new ( table : M , hash : SafeHash ) -> Option < Bucket < K , V , M > > {
235251 Bucket :: at_index ( table, * hash as usize ) . ok ( )
236252 }
237253
238254 pub fn at_index ( table : M , ib_index : uint )
239255 -> Result < Bucket < K , V , M > , Bucket < K , V , M , bucket:: TableIsEmpty > >
240256 {
257+ let table = TableRef ( table) ;
241258 let ib_index = ib_index & ( table. capacity ( ) - 1 ) ;
242259 let bb = BareBucket {
243260 raw : unsafe {
@@ -261,7 +278,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>> Bucket<K, V, M> {
261278
262279 /// Narrows down the range of iteration, which must be a power of 2.
263280 pub fn iter_to ( mut self , limit : usize ) -> Bucket < K , V , M > {
264- assert ! ( limit <= self . capacity) ;
281+ assert ! ( limit <= TableRef ( & self . table ) . capacity( ) ) ;
265282 assert ! ( limit. is_power_of_two( ) ) ;
266283 self . 0 . capacity = limit;
267284 self
@@ -294,7 +311,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>> Bucket<K, V, M> {
294311 }
295312}
296313
297- impl < K , V , M : Deref < Target = RawTable < K , V > > + DerefMut > Bucket < K , V , M > {
314+ impl < K , V , M > Bucket < K , V , M > where RawTable < K , V > : BorrowFromMut < M > {
298315 /// A combination of `peek` and `take` which doesn't consume the bucket.
299316 pub fn peek_take ( & mut self ) -> Option < ( K , V ) > {
300317 unsafe {
@@ -310,7 +327,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>> + DerefMut> Bucket<K, V, M> {
310327 }
311328}
312329
313- impl < K , V , M : Deref < Target = RawTable < K , V > > , S > Bucket < K , V , M , S > {
330+ impl < K , V , M , S > Bucket < K , V , M , S > where RawTable < K , V > : BorrowFrom < M > {
314331 #[ inline]
315332 pub fn into_next ( self ) -> Bucket < K , V , M > {
316333 let mut bucket = self . into_bucket ( ) ;
@@ -324,10 +341,10 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>, S> Bucket<K, V, M, S> {
324341 }
325342}
326343
327- impl < K , V , M : Deref < Target = RawTable < K , V > > > EmptyBucket < K , V , M > {
344+ impl < K , V , M > EmptyBucket < K , V , M > where RawTable < K , V > : BorrowFrom < M > {
328345 pub fn gap_peek ( self ) -> Option < GapThenFull < K , V , M > > {
329346 let gap = Bucket ( BareBucket {
330- table : ( ) ,
347+ table : TableRef ( ( ) ) ,
331348 idx : self . 0 . idx ,
332349 capacity : self . 0 . capacity ,
333350 raw : RawBucket {
@@ -348,7 +365,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>> EmptyBucket<K, V, M> {
348365 }
349366}
350367
351- impl < K , V , M : Deref < Target = RawTable < K , V > > + DerefMut > EmptyBucket < K , V , M > {
368+ impl < K , V , M > EmptyBucket < K , V , M > where RawTable < K , V > : BorrowFromMut < M > {
352369 /// Puts given key and value pair, along with the key's hash,
353370 /// into this bucket in the hashtable. Note how `self` is 'moved' into
354371 /// this function, because this slot will no longer be empty when
@@ -369,7 +386,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>> + DerefMut> EmptyBucket<K, V, M> {
369386 }
370387}
371388
372- impl < K , V , M : Deref < Target = RawTable < K , V > > > FullBucket < K , V , M > {
389+ impl < K , V , M > FullBucket < K , V , M > where RawTable < K , V > : BorrowFrom < M > {
373390 /// Get the distance between this bucket and the 'ideal' location
374391 /// as determined by the key's hash stored in it.
375392 ///
@@ -391,7 +408,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>> FullBucket<K, V, M> {
391408 }
392409}
393410
394- impl < K , V , M : Deref < Target = RawTable < K , V > > + DerefMut > FullBucket < K , V , M > {
411+ impl < K , V , M > FullBucket < K , V , M > where RawTable < K , V > : BorrowFromMut < M > {
395412 /// Removes this bucket's key and value from the hashtable.
396413 ///
397414 /// This works similarly to `put`, building an `EmptyBucket` out of the
@@ -415,7 +432,7 @@ impl<K, V, M: Deref<Target=RawTable<K, V>> + DerefMut> FullBucket<K, V, M> {
415432 }
416433}
417434
418- impl < ' t , K , V , M : Deref < Target = RawTable < K , V > > + ' t > FullBucket < K , V , M > {
435+ impl < ' t , K , V , M : ' t > FullBucket < K , V , M > where RawTable < K , V > : BorrowFrom < M > {
419436 /// Exchange a bucket state for immutable references into the table.
420437 /// Because the underlying reference to the table is also consumed,
421438 /// no further changes to the structure of the table are possible;
@@ -426,15 +443,15 @@ impl<'t, K, V, M: Deref<Target=RawTable<K, V>> + 't> FullBucket<K, V, M> {
426443 }
427444}
428445
429- impl < ' t , K , V , M : Deref < Target = RawTable < K , V > > + DerefMut + ' t > FullBucket < K , V , M > {
446+ impl < ' t , K , V , M : ' t > FullBucket < K , V , M > where RawTable < K , V > : BorrowFromMut < M > {
430447 /// This works similarly to `into_refs`, exchanging a bucket state
431448 /// for mutable references into the table.
432449 pub fn into_mut_refs ( self ) -> ( & ' t mut K , & ' t mut V ) {
433450 self . 0 . raw . into_mut_refs ( )
434451 }
435452}
436453
437- impl < K , V , M : Deref < Target = RawTable < K , V > > > GapThenFull < K , V , M > {
454+ impl < K , V , M > GapThenFull < K , V , M > where RawTable < K , V > : BorrowFrom < M > {
438455 #[ inline]
439456 pub fn full ( & self ) -> & FullBucket < K , V , M > {
440457 & self . full
@@ -636,7 +653,7 @@ impl<K, V, M> DerefMut for RawFullBucket<K, V, M> {
636653pub struct RawFullBuckets < K , V , M > {
637654 raw : RawBucket < K , V , M > ,
638655 hashes_end : * mut Option < SafeHash > ,
639- table : M ,
656+ table : TableRef < M > ,
640657}
641658
642659// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
@@ -710,12 +727,12 @@ impl<K: Clone, V: Clone> Clone for RawTable<K, V> {
710727
711728 {
712729 let cap = self . capacity ( ) ;
713- let mut buckets = if let Some ( buckets) = Bucket :: at_index ( self , 0 ) {
730+ let mut buckets = if let Ok ( buckets) = Bucket :: at_index ( self , 0 ) {
714731 buckets
715732 } else {
716733 return new_ht;
717734 } ;
718- let mut new_buckets = Bucket :: at_index ( & mut new_ht, 0 ) . unwrap ( ) ;
735+ let mut new_buckets = Bucket :: at_index ( & mut new_ht, 0 ) . ok ( ) . unwrap ( ) ;
719736 while buckets. index ( ) != cap {
720737 match buckets. peek ( ) {
721738 Full ( full) => {
0 commit comments