Skip to content

Commit a90403f

Browse files
committed
refactor: make key can be clone, so, we can forget unsafe ptr
Signed-off-by: Chojan Shang <psiace@apache.org>
1 parent a9594da commit a90403f

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/common/cache/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::Meter;
2323
/// A trait for a cache.
2424
pub trait Cache<K, V, S, M>
2525
where
26-
K: Eq + Hash,
26+
K: Eq + Hash + Clone,
2727
S: BuildHasher,
2828
M: Meter<K, V>,
2929
{

src/common/cache/src/cache/lru.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::meter::count_meter::CountableMeter;
6969
/// An LRU cache.
7070
#[derive(Clone)]
7171
pub struct LruCache<
72-
K: Eq + Hash,
72+
K: Eq + Hash + Clone,
7373
V,
7474
S: BuildHasher = DefaultHashBuilder,
7575
M: CountableMeter<K, V> = Count,
@@ -81,7 +81,7 @@ pub struct LruCache<
8181
meter: M,
8282
}
8383

84-
impl<K: Eq + Hash, V> LruCache<K, V> {
84+
impl<K: Eq + Hash + Clone, V> LruCache<K, V> {
8585
/// Creates an empty cache that can hold at most `capacity` items.
8686
///
8787
/// # Examples
@@ -101,7 +101,7 @@ impl<K: Eq + Hash, V> LruCache<K, V> {
101101
}
102102
}
103103

104-
impl<K: Eq + Hash, V, M: CountableMeter<K, V>> LruCache<K, V, DefaultHashBuilder, M> {
104+
impl<K: Eq + Hash + Clone, V, M: CountableMeter<K, V>> LruCache<K, V, DefaultHashBuilder, M> {
105105
/// Creates an empty cache that can hold at most `capacity` as measured by `meter`.
106106
///
107107
/// You can implement the [`Meter`][meter] trait to allow custom metrics.
@@ -146,7 +146,7 @@ impl<K: Eq + Hash, V, M: CountableMeter<K, V>> LruCache<K, V, DefaultHashBuilder
146146
}
147147
}
148148

149-
impl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S, Count> {
149+
impl<K: Eq + Hash + Clone, V, S: BuildHasher> LruCache<K, V, S, Count> {
150150
/// Creates an empty cache that can hold at most `capacity` items with the given hash builder.
151151
pub fn with_hasher(capacity: u64, hash_builder: S) -> LruCache<K, V, S, Count> {
152152
LruCache {
@@ -159,13 +159,12 @@ impl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S, Count> {
159159
}
160160
}
161161

162-
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S, M> {
162+
impl<K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S, M> {
163163
fn find_evict_candidate(&mut self) -> Option<K> {
164-
let iter = self.visited.iter_mut();
165164
let mut p: Option<K> = None;
166-
for (key, value) in iter {
165+
for (key, value) in self.visited.iter_mut() {
167166
if !(*value) && p.is_none() {
168-
p = Some(unsafe { std::ptr::read(key) })
167+
p = Some(key.clone())
169168
}
170169
if *value {
171170
*value = false;
@@ -175,18 +174,17 @@ impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S,
175174
}
176175

177176
fn peek_evict_candidate(&self) -> Option<K> {
178-
let iter = self.visited.iter();
179177
let mut p: Option<K> = None;
180-
for (key, value) in iter {
178+
for (key, value) in self.visited.iter() {
181179
if !(*value) && p.is_none() {
182-
p = Some(unsafe { std::ptr::read(key) })
180+
p = Some(key.clone())
183181
}
184182
}
185183
p
186184
}
187185
}
188186

189-
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Cache<K, V, S, M>
187+
impl<K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter<K, V>> Cache<K, V, S, M>
190188
for LruCache<K, V, S, M>
191189
{
192190
/// Creates an empty cache that can hold at most `capacity` as measured by `meter` with the
@@ -339,8 +337,7 @@ impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Cache<K, V, S, M>
339337
.meter
340338
.sub(self.current_measure, self.meter.measure(&k, old));
341339
} else {
342-
let clone_k = unsafe { std::ptr::read(&k) };
343-
self.visited.replace(clone_k, false);
340+
self.visited.replace(k.clone(), false);
344341
}
345342
let old_val = self.map.replace(k, v);
346343
while self.size() > self.capacity() {
@@ -496,7 +493,7 @@ impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Cache<K, V, S, M>
496493
}
497494
}
498495

499-
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S, M> {
496+
impl<K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S, M> {
500497
/// Returns an iterator over the cache's key-value pairs in least- to most-recently-used order.
501498
///
502499
/// Accessing the cache through the iterator does _not_ affect the cache's LRU state.
@@ -555,7 +552,7 @@ impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S,
555552
}
556553
}
557554

558-
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Extend<(K, V)>
555+
impl<K: Eq + Hash + Clone +Clone, V, S: BuildHasher, M: CountableMeter<K, V>> Extend<(K, V)>
559556
for LruCache<K, V, S, M>
560557
{
561558
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
@@ -565,15 +562,15 @@ impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Extend<(K, V)>
565562
}
566563
}
567564

568-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher, M: CountableMeter<K, V>> fmt::Debug
565+
impl<K: fmt::Debug + Eq + Hash + Clone, V: fmt::Debug, S: BuildHasher, M: CountableMeter<K, V>> fmt::Debug
569566
for LruCache<K, V, S, M>
570567
{
571568
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
572569
f.debug_map().entries(self.iter().rev()).finish()
573570
}
574571
}
575572

576-
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
573+
impl<K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
577574
for LruCache<K, V, S, M>
578575
{
579576
type Item = (K, V);
@@ -584,7 +581,7 @@ impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
584581
}
585582
}
586583

587-
impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
584+
impl<'a, K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
588585
for &'a LruCache<K, V, S, M>
589586
{
590587
type Item = (&'a K, &'a V);
@@ -594,7 +591,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
594591
}
595592
}
596593

597-
impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
594+
impl<'a, K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator
598595
for &'a mut LruCache<K, V, S, M>
599596
{
600597
type Item = (&'a K, &'a mut V);

0 commit comments

Comments
 (0)