Skip to content

Commit 99176cc

Browse files
committed
Add get_bucket_unchecked and get_bucket_unchecked_mut
1 parent 63fbff1 commit 99176cc

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
@@ -496,6 +496,49 @@ where
496496
self.raw.get_bucket(index)
497497
}
498498

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

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

0 commit comments

Comments
 (0)