You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #405 - JustForFun88:find_insert_slot, r=Amanieu
Doc `RawTableInner::find_insert_slot` and also make it unsafe
Made this function unsafe, as it is possible to use the index returned by this function unsafely. Here are two functions that confirm the documentation I made:
```rust
#[test]
fn test_infinite_loop() {
use ::alloc::vec::Vec;
let mut table: RawTable<u16> = RawTable::with_capacity(20);
assert_eq!(table.capacity(), 28);
assert_eq!(table.buckets(), 32);
for i in 0..table.buckets() {
unsafe {
let bucket_index = table.table.find_insert_slot(i as u64);
table.table.set_ctrl_h2(bucket_index, i as u64);
table.table.items += 1;
table.bucket(bucket_index).write(i as u16);
}
}
let vec = unsafe { table.iter().map(|x| x.read()).collect::<Vec<_>>() };
assert_eq!(vec, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]);
// this never return
let _bucket_index = unsafe { table.table.find_insert_slot(32) };
}
#[test]
fn test_undefined_behavior() {
use ::alloc::vec::Vec;
let mut table: RawTable<u16> = RawTable::with_capacity(3);
assert_eq!(table.capacity(), 3);
assert_eq!(table.buckets(), 4);
for i in 0..3 {
table.insert(i, i as u16, |i| *i as u64);
}
assert_eq!(table.table.items, 3);
assert_eq!(table.table.growth_left, 0);
let bucket_index = unsafe { table.table.find_insert_slot(3_u64) };
assert_eq!(bucket_index, 3);
unsafe {
table.table.set_ctrl_h2(bucket_index, 3_u64);
table.table.items += 1;
table.bucket(bucket_index).write(3_u16);
}
let vec = unsafe { table.iter().map(|x| x.read()).collect::<Vec<_>>() };
assert_eq!(vec, [0, 1, 2, 3]);
let bucket_index = unsafe { table.table.find_insert_slot(4_u64) };
assert_eq!(bucket_index, 4);
}
```
0 commit comments