Skip to content

Commit 8f72446

Browse files
committed
Add get_bucket_unchecked and get_bucket_unchecked_mut
1 parent 8c30147 commit 8f72446

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/table.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,49 @@ where
495495
self.raw.get_bucket(index)
496496
}
497497

498+
/// Gets a reference to an entry in the table at the given bucket index,
499+
/// without checking whether the index is in-bounds or occupied.
500+
///
501+
/// For a safe alternative, see [`get_bucket`](Self::get_bucket).
502+
///
503+
/// # Safety
504+
///
505+
/// It is *[undefined behavior]* to call this method with an index that is
506+
/// out-of-bounds or unoccupied, even if the resulting reference is not used.
507+
///
508+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
509+
///
510+
/// # Examples
511+
///
512+
/// ```
513+
/// # #[cfg(feature = "nightly")]
514+
/// # fn test() {
515+
/// use hashbrown::{HashTable, DefaultHashBuilder};
516+
/// use std::hash::BuildHasher;
517+
///
518+
/// let mut table = HashTable::new();
519+
/// let hasher = DefaultHashBuilder::default();
520+
/// let hasher = |val: &_| hasher.hash_one(val);
521+
/// table.insert_unique(hasher(&1), (1, 'a'), |val| hasher(&val.0));
522+
/// table.insert_unique(hasher(&2), (2, 'b'), |val| hasher(&val.0));
523+
/// table.insert_unique(hasher(&3), (3, 'c'), |val| hasher(&val.0));
524+
///
525+
/// let index = table.find_bucket_index(hasher(&2), |val| val.0 == 2).unwrap();
526+
/// assert!(std::ptr::eq(
527+
/// table.get_bucket(index).unwrap(),
528+
/// unsafe { table.get_bucket_unchecked(index) },
529+
/// ));
530+
/// # }
531+
/// # fn main() {
532+
/// # #[cfg(feature = "nightly")]
533+
/// # test()
534+
/// # }
535+
/// ```
536+
#[inline]
537+
pub unsafe fn get_bucket_unchecked(&self, index: usize) -> &T {
538+
self.raw.bucket(index).as_ref()
539+
}
540+
498541
/// Gets a mutable reference to an entry in the table at the given bucket index,
499542
/// or `None` if it is unoccupied or out of bounds.
500543
///
@@ -530,6 +573,49 @@ where
530573
self.raw.get_bucket_mut(index)
531574
}
532575

576+
/// Gets a mutable reference to an entry in the table at the given bucket index,
577+
/// without checking whether the index is in-bounds or occupied.
578+
///
579+
/// For a safe alternative, see [`get_bucket_mut`](Self::get_bucket_mut).
580+
///
581+
/// # Safety
582+
///
583+
/// It is *[undefined behavior]* to call this method with an index that is
584+
/// out-of-bounds or unoccupied, even if the resulting reference is not used.
585+
///
586+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
587+
///
588+
/// # Examples
589+
///
590+
/// ```
591+
/// # #[cfg(feature = "nightly")]
592+
/// # fn test() {
593+
/// use hashbrown::{HashTable, DefaultHashBuilder};
594+
/// use std::hash::BuildHasher;
595+
///
596+
/// let mut table = HashTable::new();
597+
/// let hasher = DefaultHashBuilder::default();
598+
/// let hasher = |val: &_| hasher.hash_one(val);
599+
/// table.insert_unique(hasher(&1), (1, 'a'), |val| hasher(&val.0));
600+
/// table.insert_unique(hasher(&2), (2, 'b'), |val| hasher(&val.0));
601+
/// table.insert_unique(hasher(&3), (3, 'c'), |val| hasher(&val.0));
602+
///
603+
/// let index = table.find_bucket_index(hasher(&2), |val| val.0 == 2).unwrap();
604+
/// assert!(std::ptr::eq(
605+
/// table.get_bucket_mut(index).unwrap(),
606+
/// unsafe { table.get_bucket_unchecked_mut(index) },
607+
/// ));
608+
/// # }
609+
/// # fn main() {
610+
/// # #[cfg(feature = "nightly")]
611+
/// # test()
612+
/// # }
613+
/// ```
614+
#[inline]
615+
pub unsafe fn get_bucket_unchecked_mut(&mut self, index: usize) -> &mut T {
616+
self.raw.bucket(index).as_mut()
617+
}
618+
533619
/// Inserts an element into the `HashTable` with the given hash value, but
534620
/// without checking whether an equivalent element already exists within the
535621
/// table.

0 commit comments

Comments
 (0)