Skip to content

Commit 596b0e9

Browse files
committed
f.
1 parent 06bc946 commit 596b0e9

File tree

2 files changed

+25
-48
lines changed

2 files changed

+25
-48
lines changed

src/libstd/collections/hash/map.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use super::table::{
3737
FullBucketMut,
3838
RawTable,
3939
SafeHash,
40-
TableRef,
4140
PartialRawTable,
4241
Put,
4342
};
@@ -322,13 +321,12 @@ fn search_hashed<K, V, M, F>(table: M,
322321
hash: SafeHash,
323322
mut is_match: F)
324323
-> InternalEntry<K, V, M> where
325-
RawTable<K, V>: BorrowFrom<M>,
324+
M: Borrow<RawTable<K, V>>,
326325
F: FnMut(&K) -> bool,
327326
{
328327
// Worst case, we'll find one empty bucket among `size + 1` buckets.
329-
let table = TableRef(table);
330-
let size = table.size();
331-
let mut probe = match Bucket::new(table.0, hash) {
328+
let size = table.borrow().size();
329+
let mut probe = match Bucket::new(table, hash) {
332330
Some(probe) => probe,
333331
None => return InternalEntry::TableIsEmpty,
334332
};
@@ -411,7 +409,7 @@ fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>,
411409
let (h_ref, k_ref, v_ref) = bucket.read_mut();
412410
swap(h_ref, &mut hash);
413411
swap(k_ref, &mut key);
414-
swap(v_ref, &mut val));
412+
swap(v_ref, &mut val);
415413
};
416414
loop {
417415
let probe = bucket.into_next();
@@ -443,12 +441,11 @@ fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>,
443441

444442
// Performs insertion with relaxed requirements.
445443
// The caller should ensure that invariants of Robin Hood linear probing hold.
446-
fn insert_hashed_ordered<M: Put, K, V>(arg: M, h: SafeHash, k: K, v: V) -> M
447-
where RawTable<K, V>: BorrowFromMut<M>
444+
fn insert_hashed_ordered<M: Put, K, V>(arg: M, hash: SafeHash, key: K, val: V) -> M
445+
where M: BorrowMut<RawTable<K, V>>
448446
{
449-
let table = TableRef(arg);
450-
let cap = table.capacity();
451-
let mut buckets = Bucket::new(table.0, h).unwrap();
447+
let cap = table.borrow().capacity();
448+
let mut buckets = Bucket::new(table, h).unwrap();
452449
let ib = buckets.index();
453450

454451
while buckets.index() != ib + cap {
@@ -1567,7 +1564,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
15671564

15681565
#[unstable(feature = "std_misc",
15691566
reason = "matches collection reform v2 specification, waiting for dust to settle")]
1570-
impl<'a, K: 'a, V: 'a, M: 'a> OccupiedEntryState<K, V, M> where RawTable<K, V>: BorrowFromMut<M> {
1567+
impl<'a, K: 'a, V: 'a, M: 'a> OccupiedEntryState<K, V, M> where M: BorrowMut<RawTable<K, V>> {
15711568
/// Gets a mutable reference to the value in the entry.
15721569
#[stable(feature = "rust1", since = "1.0.0")]
15731570
pub fn get_mut(&mut self) -> &mut V {

src/libstd/collections/hash/table.rs

+16-36
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,15 @@ struct RawBucket<K, V> {
7777
kval: *mut (K, V),
7878
}
7979

80-
impl<K, V> Copy for RawBucket<K, V> {}
81-
82-
#[derive(Clone)]
83-
pub struct TableRef<M>(pub M);
84-
85-
impl<M: Copy> Copy for TableRef<M> {}
86-
8780
pub struct Bucket<K, V, M, S = ()> {
8881
raw: RawBucket<K, V>,
8982
idx: usize,
9083
capacity: usize,
91-
table: TableRef<M>,
84+
table: M,
9285
}
9386

87+
impl<K, V> Copy for RawBucket<K, V> {}
88+
9489
impl<K, V, M: Copy, S> Copy for Bucket<K,V,M,S> where M: Borrow<RawTable<K, V>> {}
9590

9691
mod bucket {
@@ -151,20 +146,6 @@ fn can_alias_safehash_as_option() {
151146
assert_eq!(size_of::<SafeHash>(), size_of::<Option<SafeHash>>())
152147
}
153148

154-
impl<K, V, M> Deref for TableRef<M> where M: Borrow<RawTable<K, V>> {
155-
type Target = RawTable<K, V>;
156-
157-
fn deref(&self) -> &RawTable<K, V> {
158-
self.0.borrow()
159-
}
160-
}
161-
162-
impl<K, V, M> DerefMut for TableRef<M> where M: Borrow<RawTable<K, V>> {
163-
fn deref_mut(&mut self) -> &mut RawTable<K, V> {
164-
self.0.borrow_mut()
165-
}
166-
}
167-
168149
impl<K, V> RawBucket<K, V> {
169150
unsafe fn offset(self, count: isize) -> RawBucket<K, V> {
170151
RawBucket {
@@ -180,15 +161,15 @@ impl<K, V, M, S> Borrow<RawTable<K, V>> for Bucket<K, V, M, S>
180161
where M: Borrow<RawTable<K, V>>
181162
{
182163
fn borrow(&self) -> &RawTable<K, V> {
183-
self.table.0.borrow()
164+
self.table.borrow()
184165
}
185166
}
186167

187168
impl<K, V, M, S> BorrowMut<RawTable<K, V>> for Bucket<K, V, M, S>
188169
where M: Borrow<RawTable<K, V>>
189170
{
190171
fn borrow_mut(&mut self) -> &mut RawTable<K, V> {
191-
self.table.0.borrow_mut()
172+
self.table.borrow_mut()
192173
}
193174
}
194175

@@ -202,11 +183,11 @@ impl<K, V, M: Put> Put for FullBucket<K, V, M> {}
202183
impl<K, V, M, S> Bucket<K, V, M, S> {
203184
/// Borrow a reference to the table.
204185
pub fn table(&self) -> &M {
205-
&self.table.0
186+
&self.table
206187
}
207188
/// Move out the reference to the table.
208189
pub fn into_table(self) -> M {
209-
self.table.0
190+
self.table
210191
}
211192
/// Get the raw index.
212193
pub fn index(&self) -> usize {
@@ -232,11 +213,10 @@ impl<K, V, M> Bucket<K, V, M> where M: Borrow<RawTable<K, V>> {
232213
pub fn at_index(table: M, ib_index: uint)
233214
-> Result<Bucket<K, V, M>, Bucket<K, V, M, bucket::TableIsEmpty>>
234215
{
235-
let table = TableRef(table);
236-
let capacity = table.capacity();
216+
let capacity = table.borrow().capacity();
237217
let idx = ib_index & (capacity - 1);
238218
let bucket: Bucket<K, V, M> = Bucket {
239-
raw: unsafe { table.first_bucket_raw().offset(idx as isize) },
219+
raw: unsafe { table.borrow().first_bucket_raw().offset(idx as isize) },
240220
idx: idx,
241221
capacity: capacity,
242222
table: table,
@@ -254,7 +234,7 @@ impl<K, V, M> Bucket<K, V, M> where M: Borrow<RawTable<K, V>> {
254234

255235
/// Narrows down the range of iteration, which must be a power of 2.
256236
pub fn iter_to(mut self, limit: usize) -> Bucket<K, V, M> {
257-
assert!(limit <= self.table.capacity());
237+
assert!(limit <= self.table.borrow().capacity());
258238
assert!(limit.is_power_of_two());
259239
self.capacity = limit;
260240
self
@@ -315,15 +295,15 @@ impl<K, V, M, S> Bucket<K, V, M, S> where M: Borrow<RawTable<K, V>> {
315295
raw: self.raw,
316296
idx: self.idx,
317297
capacity: self.capacity,
318-
table: TableRef(self),
298+
table: self,
319299
}
320300
}
321301
}
322302

323303
impl<K, V, M> EmptyBucket<K, V, M> where M: Borrow<RawTable<K, V>> {
324304
pub fn gap_peek(self) -> Option<GapThenFull<K, V, M>> {
325305
let gap = Bucket {
326-
table: TableRef(()),
306+
table: (),
327307
idx: self.idx,
328308
capacity: self.capacity,
329309
raw: self.raw,
@@ -341,7 +321,7 @@ impl<K, V, M> EmptyBucket<K, V, M> where M: Borrow<RawTable<K, V>> {
341321
}
342322
}
343323

344-
impl<K, V, M> EmptyBucket<K, V, M> where M: Borrow<RawTable<K, V>>, M: Put {
324+
impl<K, V, M> EmptyBucket<K, V, M> where M: BorrowMut<RawTable<K, V>>, M: Put {
345325
/// Puts given key and value pair, along with the key's hash,
346326
/// into this bucket in the hashtable. Note how `self` is 'moved' into
347327
/// this function, because this slot will no longer be empty when
@@ -356,7 +336,7 @@ impl<K, V, M> EmptyBucket<K, V, M> where M: Borrow<RawTable<K, V>>, M: Put {
356336
ptr::write(self.raw.kval, (key, value));
357337
}
358338

359-
self.table.size += 1;
339+
self.table.borrow_mut().size += 1;
360340

361341
self.unsafe_cast()
362342
}
@@ -413,7 +393,7 @@ impl<'t, K, V, M: 't> FullBucket<K, V, M> where M: BorrowMut<RawTable<K, V>> {
413393
/// This works similarly to `put`, building an `EmptyBucket` out of the
414394
/// taken bucket.
415395
pub fn take(mut self) -> (EmptyBucket<K, V, M>, K, V) {
416-
self.table.size -= 1;
396+
self.table.borrow_mut().size -= 1;
417397

418398
unsafe {
419399
*self.raw.hash = None;
@@ -618,7 +598,7 @@ impl<'t, K, V, M: 't> RawFullBucket<K, V, M> where M: BorrowMut<RawTable<K, V>>
618598
pub struct RawFullBuckets<K, V, M> {
619599
raw: RawBucket<K, V>,
620600
hashes_end: *mut Option<SafeHash>,
621-
table: TableRef<M>,
601+
table: M,
622602
}
623603

624604
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`

0 commit comments

Comments
 (0)