From 7c1892185c0e7a49197e42adc03834d153fabb80 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Tue, 27 Aug 2024 17:28:24 +0100 Subject: [PATCH] Update to DashMap 6 --- Cargo.toml | 10 ++++++++-- src/cache/mod.rs | 8 ++++---- src/cache/wrappers.rs | 24 +++++++++++++++++------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8da9dc346e1..1a96a7febd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,10 +46,10 @@ bytes = { version = "1.5.0", optional = true } percent-encoding = { version = "2.3.0", optional = true } mini-moka = { version = "0.10.2", optional = true } mime_guess = { version = "2.0.4", optional = true } -dashmap = { version = "5.5.3", features = ["serde"], optional = true } +dashmap = { version = "6.0.1", features = ["serde"], optional = true } parking_lot = { version = "0.12.1", optional = true } ed25519-dalek = { version = "2.0.0", optional = true } -typesize = { version = "0.1.2", optional = true, features = ["url", "time", "serde_json", "secrecy", "dashmap", "parking_lot", "details"] } +typesize = { version = "0.1.8", optional = true, features = ["url", "time", "serde_json", "secrecy", "parking_lot", "details"] } # serde feature only allows for serialisation, # Serenity workspace crates command_attr = { version = "0.5.2", path = "./command_attr", optional = true } @@ -128,6 +128,8 @@ temp_cache = ["cache", "mini-moka", "typesize?/mini_moka"] # Removed feature (https://github.com/serenity-rs/serenity/pull/2246) absolute_ratelimits = [] +typesize = ["dep:typesize", "dashmap?/typesize"] + # Backends to pick from: # - Rustls Backends rustls_backend = [ @@ -147,3 +149,7 @@ native_tls_backend = [ [package.metadata.docs.rs] features = ["full"] rustdoc-args = ["--cfg", "docsrs"] + +[patch.crates-io.dashmap] +git = "https://github.com/GnomedDev/dashmap" +branch = "typesize" diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 14c4b787588..ca5250b1817 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -59,8 +59,8 @@ struct NotSend; enum CacheRefInner<'a, K, V, T> { #[cfg(feature = "temp_cache")] Arc(Arc), - DashRef(Ref<'a, K, V, BuildHasher>), - DashMappedRef(MappedRef<'a, K, T, V, BuildHasher>), + DashRef(Ref<'a, K, V>), + DashMappedRef(MappedRef<'a, K, T, V>), ReadGuard(parking_lot::RwLockReadGuard<'a, V>), } @@ -82,11 +82,11 @@ impl<'a, K, V, T> CacheRef<'a, K, V, T> { Self::new(CacheRefInner::Arc(inner.get_inner())) } - fn from_ref(inner: Ref<'a, K, V, BuildHasher>) -> Self { + fn from_ref(inner: Ref<'a, K, V>) -> Self { Self::new(CacheRefInner::DashRef(inner)) } - fn from_mapped_ref(inner: MappedRef<'a, K, T, V, BuildHasher>) -> Self { + fn from_mapped_ref(inner: MappedRef<'a, K, T, V>) -> Self { Self::new(CacheRefInner::DashMappedRef(inner)) } diff --git a/src/cache/wrappers.rs b/src/cache/wrappers.rs index 0b8e98c7ae9..b26aad47db5 100644 --- a/src/cache/wrappers.rs +++ b/src/cache/wrappers.rs @@ -14,15 +14,15 @@ use typesize::TypeSize; /// A wrapper around Option> to ease disabling specific cache fields. pub(crate) struct MaybeMap(pub(super) Option>); impl MaybeMap { - pub fn iter(&self) -> impl Iterator> { + pub fn iter(&self) -> impl Iterator> { Option::iter(&self.0).flat_map(DashMap::iter) } - pub fn get(&self, k: &K) -> Option> { + pub fn get(&self, k: &K) -> Option> { self.0.as_ref()?.get(k) } - pub fn get_mut(&self, k: &K) -> Option> { + pub fn get_mut(&self, k: &K) -> Option> { self.0.as_ref()?.get_mut(k) } @@ -59,8 +59,10 @@ impl TypeSize for MaybeMap { self.0.as_ref().map(DashMap::extra_size).unwrap_or_default() } - fn get_collection_item_count(&self) -> Option { - self.0.as_ref().and_then(DashMap::get_collection_item_count) + typesize::if_typesize_details! { + fn get_collection_item_count(&self) -> Option { + self.0.as_ref().and_then(DashMap::get_collection_item_count) + } } } @@ -69,11 +71,11 @@ impl TypeSize for MaybeMap { /// map without allowing mutation of internal cache fields, which could cause issues. pub struct ReadOnlyMapRef<'a, K: Eq + Hash, V>(Option<&'a DashMap>); impl<'a, K: Eq + Hash, V> ReadOnlyMapRef<'a, K, V> { - pub fn iter(&self) -> impl Iterator> { + pub fn iter(&self) -> impl Iterator> { self.0.into_iter().flat_map(DashMap::iter) } - pub fn get(&self, k: &K) -> Option> { + pub fn get(&self, k: &K) -> Option> { self.0?.get(k) } @@ -81,6 +83,7 @@ impl<'a, K: Eq + Hash, V> ReadOnlyMapRef<'a, K, V> { self.0.map_or(0, DashMap::len) } } + pub struct Hasher(fxhash::FxHasher); impl std::hash::Hasher for Hasher { fn finish(&self) -> u64 { @@ -91,6 +94,10 @@ impl std::hash::Hasher for Hasher { self.0.write(bytes); } } + +#[cfg(feature = "typesize")] +impl typesize::TypeSize for Hasher {} + #[derive(Clone, Default)] pub struct BuildHasher(fxhash::FxBuildHasher); impl std::hash::BuildHasher for BuildHasher { @@ -101,6 +108,9 @@ impl std::hash::BuildHasher for BuildHasher { } } +#[cfg(feature = "typesize")] +impl typesize::TypeSize for BuildHasher {} + /// Wrapper around `SizableArc`` with support for disabling typesize. /// /// This denotes an Arc where T's size should be considered when calling `TypeSize::get_size`