Skip to content

Commit 2ce56de

Browse files
committed
style: return Result instead of Option
1 parent 524982c commit 2ce56de

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/raw/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,12 @@ impl<T, A: Allocator> RawTable<T, A> {
10921092
///
10931093
/// This does not check if the given element already exists in the table.
10941094
#[inline]
1095-
pub(crate) fn insert_within_capacity(&mut self, hash: u64, value: T) -> Option<Bucket<T>> {
1096-
let slot = self.find_insert_slot(hash)?;
1097-
// SAFETY: todo
1098-
Some(unsafe { self.insert_in_slot(hash, slot, value) })
1095+
pub(crate) fn insert_within_capacity(&mut self, hash: u64, value: T) -> Result<Bucket<T>, T> {
1096+
match self.find_insert_slot(hash) {
1097+
// SAFETY: todo
1098+
Some(slot) => Ok(unsafe { self.insert_in_slot(hash, slot, value) }),
1099+
None => Err(value),
1100+
}
10991101
}
11001102

11011103
/// Inserts a new element into the table, and returns a mutable reference to it.

src/table.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ where
417417

418418
/// Inserts an element into the `HashTable` with the given hash value, but
419419
/// without checking whether an equivalent element already exists within
420-
/// the table. If there is insufficient capacity, then this returns None.
420+
/// the table. If there is insufficient capacity, then this returns an
421+
/// error holding the provided value.
421422
///
422423
/// # Examples
423424
///
@@ -430,9 +431,10 @@ where
430431
/// let mut v = HashTable::new();
431432
/// let hasher = DefaultHashBuilder::default();
432433
/// let hasher = |val: &_| hasher.hash_one(val);
433-
/// assert!(v.insert_unique_within_capacity(hasher(&1), 1).is_none());
434+
/// assert!(v.insert_unique_within_capacity(hasher(&1), 1).is_err());
435+
/// assert!(v.is_empty());
434436
/// v.reserve(1, hasher);
435-
/// v.insert_unique_within_capacity(hasher(&1), 1);
437+
/// assert!(v.insert_unique_within_capacity(hasher(&1), 1).is_ok());
436438
/// assert!(!v.is_empty());
437439
/// # }
438440
/// # fn main() {
@@ -444,13 +446,15 @@ where
444446
&mut self,
445447
hash: u64,
446448
value: T,
447-
) -> Option<OccupiedEntry<'_, T, A>> {
448-
let bucket = self.raw.insert_within_capacity(hash, value)?;
449-
Some(OccupiedEntry {
450-
hash,
451-
bucket,
452-
table: self,
453-
})
449+
) -> Result<OccupiedEntry<'_, T, A>, T> {
450+
match self.raw.insert_within_capacity(hash, value) {
451+
Some(bucket) => Ok(OccupiedEntry {
452+
hash,
453+
bucket,
454+
table: self,
455+
}),
456+
None => Err(value),
457+
}
454458
}
455459

456460
/// Clears the table, removing all values.

0 commit comments

Comments
 (0)