Skip to content

Commit

Permalink
refactor(id-wrapper): get rid of RwLockReadGuard::upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed Apr 3, 2024
1 parent b676a3a commit 8e98191
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 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.0.7"
version = "0.0.8"
authors = ["Andrew Sonin <[email protected]>"]
categories = ["data-structures"]
description = """
Expand Down
8 changes: 4 additions & 4 deletions src/collections/blazemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use read_write_api::ReadApi;
#[cfg(feature = "serde")]
use {
crate::{
external::read_write_api::UpgradableReadApi,
orig_type_id_map::InsertableStaticInfoApi,
prelude::BlazeMapIdWrapper,
},
read_write_api::RwApi,
serde::{
de::{MapAccess, Visitor},
Deserialize,
Expand Down Expand Up @@ -391,7 +391,7 @@ impl<'de, K, V> Deserialize<'de> for BlazeMap<K, V>
K: BlazeMapIdWrapper,
<K as BlazeMapId>::OrigType: Deserialize<'de>,
<K as BlazeMapId>::StaticInfoApi: InsertableStaticInfoApi<<K as BlazeMapId>::OrigType>,
<K as BlazeMapId>::StaticInfoApiLock: UpgradableReadApi,
<K as BlazeMapId>::StaticInfoApiLock: RwApi,
V: Deserialize<'de>
{
#[inline]
Expand All @@ -408,15 +408,15 @@ struct BlazeMapDeserializer<K, V>(PhantomData<(K, V)>)
where
K: BlazeMapIdWrapper,
<K as BlazeMapId>::StaticInfoApi: InsertableStaticInfoApi<<K as BlazeMapId>::OrigType>,
<K as BlazeMapId>::StaticInfoApiLock: UpgradableReadApi;
<K as BlazeMapId>::StaticInfoApiLock: RwApi;

#[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: UpgradableReadApi,
<K as BlazeMapId>::StaticInfoApiLock: RwApi,
V: Deserialize<'de>
{
type Value = BlazeMap<K, V>;
Expand Down
9 changes: 5 additions & 4 deletions src/id_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ops::Range;

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

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

Expand Down Expand Up @@ -46,18 +46,19 @@ pub trait BlazeMapId: Copy
pub trait BlazeMapIdWrapper: BlazeMapId
where
Self::StaticInfoApi: InsertableStaticInfoApi<Self::OrigType>,
Self::StaticInfoApiLock: UpgradableReadApi
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.upgradable_read();
let guard = static_info.read();
if let Some(index) = guard.get_index(&key) {
Self::from_index_unchecked(index)
} else {
let mut guard = guard.upgrade();
drop(guard);
let mut guard = static_info.write();
let index = guard.insert_new_key_unchecked(key);
Self::from_index_unchecked(index)
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/private_index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::hash::Hash;

/// Necessary to protect the internal `usize`, which, in the absence of this wrapper,
/// would be public in the module calling [`register_blazemap_id_wrapper`](crate::register_blazemap_id_wrapper).
/// would be public in the module calling
/// the [`register_blazemap_id_wrapper`](crate::register_blazemap_id_wrapper).
///
/// Publicity of the internal `usize` may lead to:
/// * UB if the programmer of the downstream crate would accidentally mutate it.
Expand Down

0 comments on commit 8e98191

Please sign in to comment.