Skip to content

Commit

Permalink
refactor(id-wrapper): get rid of InsertableStaticInfoApi
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed Apr 17, 2024
1 parent 039adc1 commit da6d736
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 68 deletions.
16 changes: 2 additions & 14 deletions src/collections/blazemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use read_write_api::ReadApi;

#[cfg(feature = "serde")]
use {
crate::{
orig_type_id_map::InsertableStaticInfoApi,
prelude::BlazeMapIdWrapper,
},
read_write_api::RwApi,
crate::prelude::BlazeMapIdWrapper,
serde::{
de::{MapAccess, Visitor},
Deserialize,
Expand Down Expand Up @@ -390,8 +386,6 @@ impl<'de, K, V> Deserialize<'de> for BlazeMap<K, V>
where
K: BlazeMapIdWrapper,
<K as BlazeMapId>::OrigType: Deserialize<'de>,
<K as BlazeMapId>::StaticInfoApi: InsertableStaticInfoApi<<K as BlazeMapId>::OrigType>,
<K as BlazeMapId>::StaticInfoApiLock: RwApi,
V: Deserialize<'de>
{
#[inline]
Expand All @@ -404,19 +398,13 @@ impl<'de, K, V> Deserialize<'de> for BlazeMap<K, V>
}

#[cfg(feature = "serde")]
struct BlazeMapDeserializer<K, V>(PhantomData<(K, V)>)
where
K: BlazeMapIdWrapper,
<K as BlazeMapId>::StaticInfoApi: InsertableStaticInfoApi<<K as BlazeMapId>::OrigType>,
<K as BlazeMapId>::StaticInfoApiLock: RwApi;
struct BlazeMapDeserializer<K, V>(PhantomData<(K, V)>);

#[cfg(feature = "serde")]
impl<'de, K, V> Visitor<'de> for BlazeMapDeserializer<K, V>
where
K: BlazeMapIdWrapper,
<K as BlazeMapId>::OrigType: Deserialize<'de>,
<K as BlazeMapId>::StaticInfoApi: InsertableStaticInfoApi<<K as BlazeMapId>::OrigType>,
<K as BlazeMapId>::StaticInfoApiLock: RwApi,
V: Deserialize<'de>
{
type Value = BlazeMap<K, V>;
Expand Down
23 changes: 3 additions & 20 deletions src/id_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ops::Range;

use read_write_api::{ReadApi, RwApi, WriteApi};
use read_write_api::ReadApi;

use crate::orig_type_id_map::{InsertableStaticInfoApi, StaticInfoApi};
use crate::orig_type_id_map::StaticInfoApi;

/// Provides interface for `blazemap` id types
/// defined by the [`register_blazemap_id_wrapper`](crate::register_blazemap_id_wrapper)
Expand Down Expand Up @@ -44,26 +44,9 @@ pub trait BlazeMapId: Copy
/// Provides interface for constructable `blazemap` key-wrapper types
/// defined by the [`register_blazemap_id_wrapper`](crate::register_blazemap_id_wrapper) macro.
pub trait BlazeMapIdWrapper: BlazeMapId
where
Self::StaticInfoApi: InsertableStaticInfoApi<Self::OrigType>,
Self::StaticInfoApiLock: RwApi
{
/// Creates a new instance of [`Self`] based on the [`Self::OrigType`](BlazeMapId::OrigType) instance.
#[inline]
fn new(key: Self::OrigType) -> Self {
unsafe {
let mut static_info = Self::static_info();
let guard = static_info.read();
if let Some(index) = guard.get_index(&key) {
Self::from_index_unchecked(index)
} else {
drop(guard);
let mut guard = static_info.write();
let index = guard.insert_new_key_unchecked(key);
Self::from_index_unchecked(index)
}
}
}
fn new(key: Self::OrigType) -> Self;
}

/// Iterator over consecutive `blazemap` identifiers.
Expand Down
19 changes: 18 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,24 @@ macro_rules! blazemap_id_wrapper_inner {
}

impl $crate::prelude::BlazeMapIdWrapper for $new_type
{}
{
#[inline]
fn new(key: $orig_type) -> Self {
use $crate::prelude::BlazeMapId;
let static_info = Self::static_info();
let guard = static_info.read();
unsafe {
if let Some(index) = guard.get_index(&key) {
Self::from_index_unchecked(index)
} else {
drop(guard);
let mut guard = static_info.write();
let index = guard.insert_new_key_unchecked(key);
Self::from_index_unchecked(index)
}
}
}
}
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/orig_type_id_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@ pub trait StaticInfoApi<K>
fn num_elems(&self) -> usize;

unsafe fn get_key_unchecked(&self, index: usize) -> Self::KeyUnchecked<'_>;
}

#[doc(hidden)]
pub trait InsertableStaticInfoApi<K>: StaticInfoApi<K>
{
fn get_index(&self, key: &K) -> Option<usize>;

unsafe fn insert_new_key_unchecked(&mut self, key: K) -> usize;
}
50 changes: 25 additions & 25 deletions src/utils/static_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};

use once_cell::sync::Lazy;

use crate::orig_type_id_map::{InsertableStaticInfoApi, StaticInfoApi};
use crate::orig_type_id_map::StaticInfoApi;

/// Global, statically initialized structure that contains correspondence mapping
/// between blazemap index wrappers and original keys.
Expand Down Expand Up @@ -33,6 +33,30 @@ impl<K> IdWrapperStaticInfo<K>
}
}

impl<K> IdWrapperStaticInfo<K>
where
K: Clone + Eq + Hash
{
#[inline]
pub fn get_index(&self, key: &K) -> Option<usize> {
self.orig_to_index.get(key).copied()
}

#[inline]
pub
unsafe fn insert_new_key_unchecked(&mut self, key: K) -> usize
{
let next_id = self.num_elems();
let Self {
index_to_orig,
orig_to_index
} = self;
index_to_orig.push(key.clone());
orig_to_index.insert(key, next_id);
next_id
}
}

impl TrivialIdStaticInfo
{
/// Creates a new instance of [`TrivialIdStaticInfo`].
Expand Down Expand Up @@ -74,30 +98,6 @@ impl<K> StaticInfoApi<K> for IdWrapperStaticInfo<K>
}
}

impl<K> InsertableStaticInfoApi<K> for IdWrapperStaticInfo<K>
where
K: Clone + Eq + Hash
{
#[inline]
fn get_index(&self, key: &K) -> Option<usize> {
self.orig_to_index.get(key).copied()
}

#[inline]
unsafe
fn insert_new_key_unchecked(&mut self, key: K) -> usize
{
let next_id = self.num_elems();
let Self {
index_to_orig,
orig_to_index
} = self;
index_to_orig.push(key.clone());
orig_to_index.insert(key, next_id);
next_id
}
}

impl StaticInfoApi<usize> for TrivialIdStaticInfo
{
type KeyUnchecked<'a> = usize
Expand Down

0 comments on commit da6d736

Please sign in to comment.