use core::ptr::NonNull;
pub(super) type BucketPtr<K, V> = NonNull<Bucket<K, V>>;
pub(super) struct Bucket<K, V> {
pub(super) hash: u64,
pub(super) key: K,
pub(super) value: V,
}
impl<K, V> Bucket<K, V> {
pub(super) fn new(hash: u64, key: K, value: V) -> Self {
Self { hash, key, value }
}
pub(super) fn as_tuple(&self) -> (&K, &V) {
(&self.key, &self.value)
}
pub(super) fn as_tuple_mut(&mut self) -> (&K, &mut V) {
(&self.key, &mut self.value)
}
pub(super) fn as_key(&self) -> &K {
&self.key
}
pub(super) fn as_value(&self) -> &V {
&self.value
}
pub(super) fn as_value_mut(&mut self) -> &mut V {
&mut self.value
}
pub(super) fn into_tuple(self) -> (K, V) {
(self.key, self.value)
}
}
impl<K, V> core::clone::Clone for Bucket<K, V>
where
K: Clone,
V: Clone,
{
fn clone(&self) -> Self {
Self {
hash: self.hash,
key: self.key.clone(),
value: self.value.clone(),
}
}
}
#[cfg(test)]
mod utest {
use super::*;
#[test]
fn test_bucket_ptr() {
let bucket = Bucket::new(1, 1, 2);
let ptr = BucketPtr::from(&bucket);
assert_eq!(unsafe { ptr.as_ref().key }, 1);
assert_eq!(unsafe { ptr.as_ref().value }, 2);
#[allow(clippy::clone_on_copy)]
let clone = ptr.clone();
assert_eq!(unsafe { clone.as_ref().key }, 1);
assert_eq!(unsafe { clone.as_ref().value }, 2);
}
}