Skip to content

Commit ac65b9f

Browse files
author
Cole Miller
committed
Add try_insert_no_grow method on RawTable
1 parent f9b3183 commit ac65b9f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/raw/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,31 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
10131013
}
10141014
}
10151015

1016+
/// Attempts to insert a new element without growing the table and return its raw bucket.
1017+
///
1018+
/// Returns an `Err` containing the given element if inserting it would require growing the
1019+
/// table.
1020+
///
1021+
/// This does not check if the given element already exists in the table.
1022+
#[cfg(feature = "raw")]
1023+
#[cfg_attr(feature = "inline-more", inline)]
1024+
pub fn try_insert_no_grow(&mut self, hash: u64, value: T) -> Result<Bucket<T>, T> {
1025+
unsafe {
1026+
let index = self.find_insert_slot(hash);
1027+
let old_ctrl = *self.ctrl(index);
1028+
if unlikely(self.growth_left == 0 && special_is_empty(old_ctrl)) {
1029+
Err(value)
1030+
} else {
1031+
let bucket = self.bucket(index);
1032+
self.growth_left -= special_is_empty(old_ctrl) as usize;
1033+
self.set_ctrl(index, h2(hash));
1034+
bucket.write(value);
1035+
self.items += 1;
1036+
Ok(bucket)
1037+
}
1038+
}
1039+
}
1040+
10161041
/// Inserts a new element into the table, and returns a mutable reference to it.
10171042
///
10181043
/// This does not check if the given element already exists in the table.

0 commit comments

Comments
 (0)