From 8e981911c57a73ce4a57811f487225ef4b588ead Mon Sep 17 00:00:00 2001 From: Andrew Sonin Date: Wed, 3 Apr 2024 13:43:10 +0300 Subject: [PATCH] refactor(id-wrapper): get rid of RwLockReadGuard::upgrade --- Cargo.toml | 2 +- src/collections/blazemap.rs | 8 ++++---- src/id_wrapper.rs | 9 +++++---- src/utils/private_index.rs | 3 ++- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 582cacd..fd25aab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blazemap" -version = "0.0.7" +version = "0.0.8" authors = ["Andrew Sonin "] categories = ["data-structures"] description = """ diff --git a/src/collections/blazemap.rs b/src/collections/blazemap.rs index ccd1e45..d184418 100644 --- a/src/collections/blazemap.rs +++ b/src/collections/blazemap.rs @@ -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, @@ -391,7 +391,7 @@ impl<'de, K, V> Deserialize<'de> for BlazeMap K: BlazeMapIdWrapper, ::OrigType: Deserialize<'de>, ::StaticInfoApi: InsertableStaticInfoApi<::OrigType>, - ::StaticInfoApiLock: UpgradableReadApi, + ::StaticInfoApiLock: RwApi, V: Deserialize<'de> { #[inline] @@ -408,7 +408,7 @@ struct BlazeMapDeserializer(PhantomData<(K, V)>) where K: BlazeMapIdWrapper, ::StaticInfoApi: InsertableStaticInfoApi<::OrigType>, - ::StaticInfoApiLock: UpgradableReadApi; + ::StaticInfoApiLock: RwApi; #[cfg(feature = "serde")] impl<'de, K, V> Visitor<'de> for BlazeMapDeserializer @@ -416,7 +416,7 @@ impl<'de, K, V> Visitor<'de> for BlazeMapDeserializer K: BlazeMapIdWrapper, ::OrigType: Deserialize<'de>, ::StaticInfoApi: InsertableStaticInfoApi<::OrigType>, - ::StaticInfoApiLock: UpgradableReadApi, + ::StaticInfoApiLock: RwApi, V: Deserialize<'de> { type Value = BlazeMap; diff --git a/src/id_wrapper.rs b/src/id_wrapper.rs index b1f16d9..b32fc33 100644 --- a/src/id_wrapper.rs +++ b/src/id_wrapper.rs @@ -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}; @@ -46,18 +46,19 @@ pub trait BlazeMapId: Copy pub trait BlazeMapIdWrapper: BlazeMapId where Self::StaticInfoApi: InsertableStaticInfoApi, - 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) } diff --git a/src/utils/private_index.rs b/src/utils/private_index.rs index 8708994..6f2a2c0 100644 --- a/src/utils/private_index.rs +++ b/src/utils/private_index.rs @@ -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.