Skip to content

Commit

Permalink
feat(key-wrapper-bounded): add key method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed Apr 27, 2024
1 parent a1699e6 commit d967240
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blazemap"
version = "0.4.0"
version = "0.5.0"
authors = ["Andrew Sonin <sonin.cel@yandex.ru>"]
categories = ["data-structures", "concurrency"]
description = """
Expand Down
13 changes: 13 additions & 0 deletions src/type_gen/key_wrapper_bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,24 @@ macro_rules! key_wrapper_bounded_inner {
#[cfg(not(loom))]
impl $new_type
{
#[doc = ::std::concat!("Creates a new instance of [`", ::std::stringify!($new_type), "`].")]
#[inline]
$vis fn new(value: $orig_type) -> Self {
use $crate::traits::BlazeMapIdStatic;
unsafe { <Self as $crate::prelude::BlazeMapIdWrapper>::new(Self::static_container(), value) }
}

#[doc = ::std::concat!(
"Returns the original key corresponding to the [`",
::std::stringify!($new_type),
"`] instance."
)]
#[inline]
#[allow(dead_code)]
$vis fn key(self) -> &'static $orig_type {
let static_container = <Self as $crate::traits::BlazeMapIdStatic>::static_container();
unsafe { static_container.key_by_offset_unchecked(self.0.into_offset()) }
}
}

impl $crate::prelude::BlazeMapId for $new_type
Expand Down
16 changes: 11 additions & 5 deletions src/type_info_containers/key_wrapper_bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ impl<K, const CAP: usize> StaticContainer<K, CAP> {
next_offset: AtomicUsize::new(0),
}
}

#[inline]
#[doc(hidden)]
pub unsafe fn key_by_offset_unchecked(&self, offset: usize) -> &K {
#[cfg(not(loom))]
let result = (*self.offset_to_orig.get_unchecked(offset).get()).assume_init_ref();
#[cfg(loom)]
let result = BorrowGuard(self.offset_to_orig.get(offset).unwrap().read().unwrap());
result
}
}

impl<K, I, const CAP: usize> WrapKey<I> for StaticContainer<K, CAP>
Expand Down Expand Up @@ -206,10 +216,6 @@ impl<K> Borrow<K> for BorrowGuard<'_, K> {
impl<K, const CAP: usize> KeyByOffsetProvider<K> for StaticContainer<K, CAP> {
#[inline]
unsafe fn key_by_offset_unchecked(&self, offset: usize) -> impl Borrow<K> {
#[cfg(not(loom))]
let result = (*self.offset_to_orig.get_unchecked(offset).get()).assume_init_ref();
#[cfg(loom)]
let result = BorrowGuard(self.offset_to_orig.get(offset).unwrap().read().unwrap());
result
StaticContainer::key_by_offset_unchecked(self, offset)
}
}

0 comments on commit d967240

Please sign in to comment.