Skip to content

Commit 65bb2dd

Browse files
committed
get_bucket_entry -> Result<OccupiedEntry, AbsentEntry>
1 parent 99176cc commit 65bb2dd

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/table.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ where
425425
}
426426

427427
/// Returns an `OccupiedEntry` for the given bucket index in the table,
428-
/// or `None` if it is unoccupied or out of bounds.
428+
/// or `AbsentEntry` if it is unoccupied or out of bounds.
429429
///
430430
/// # Examples
431431
///
@@ -444,7 +444,7 @@ where
444444
///
445445
/// let index = table.find_bucket_index(hasher(&2), |val| val.0 == 2).unwrap();
446446
///
447-
/// assert!(table.get_bucket_entry(usize::MAX).is_none());
447+
/// assert!(table.get_bucket_entry(usize::MAX).is_err());
448448
///
449449
/// let occupied_entry = table.get_bucket_entry(index).unwrap();
450450
/// assert_eq!(occupied_entry.get(), &(2, 'b'));
@@ -458,11 +458,17 @@ where
458458
/// # }
459459
/// ```
460460
#[inline]
461-
pub fn get_bucket_entry(&mut self, index: usize) -> Option<OccupiedEntry<'_, T, A>> {
462-
Some(OccupiedEntry {
463-
bucket: self.raw.checked_bucket(index)?,
464-
table: self,
465-
})
461+
pub fn get_bucket_entry(
462+
&mut self,
463+
index: usize,
464+
) -> Result<OccupiedEntry<'_, T, A>, AbsentEntry<'_, T, A>> {
465+
match self.raw.checked_bucket(index) {
466+
Some(bucket) => Ok(OccupiedEntry {
467+
bucket,
468+
table: self,
469+
}),
470+
None => Err(AbsentEntry { table: self }),
471+
}
466472
}
467473

468474
/// Gets a reference to an entry in the table at the given bucket index,
@@ -2224,10 +2230,11 @@ where
22242230
}
22252231
}
22262232

2227-
/// Type representing the absence of an entry, as returned by [`HashTable::find_entry`].
2233+
/// Type representing the absence of an entry, as returned by [`HashTable::find_entry`]
2234+
/// and [`HashTable::AbsentEntry`].
22282235
///
22292236
/// This type only exists due to [limitations] in Rust's NLL borrow checker. In
2230-
/// the future, `find_entry` will return an `Option<OccupiedEntry>` and this
2237+
/// the future, those methods will return an `Option<OccupiedEntry>` and this
22312238
/// type will be removed.
22322239
///
22332240
/// [limitations]: https://smallcultfollowing.com/babysteps/blog/2018/06/15/mir-based-borrow-check-nll-status-update/#polonius

0 commit comments

Comments
 (0)