Skip to content

Commit 45828d9

Browse files
CopilotSF-Zhou
andcommitted
Remove Send impl from Entry/LruEntry, relax Sync bounds, bump version to 0.2.1
Entry and LruEntry are RAII lock guards that should not be Send (similar to MutexGuard). The Sync impl only needs K: Sync + V: Sync since sharing &Entry only involves shared references, not ownership transfer. Co-authored-by: SF-Zhou <7477599+SF-Zhou@users.noreply.github.com>
1 parent 9974549 commit 45828d9

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lockmap"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55

66
authors = ["SF-Zhou <sfzhou.scut@gmail.com>"]

src/lockmap.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,10 @@ pub struct Entry<'a, K: Eq + Hash, V> {
699699
}
700700

701701
// SAFETY: The guard holds a per-key mutex lock and a valid, ref-counted pointer.
702-
unsafe impl<K: Eq + Hash + Send, V: Send> Send for Entry<'_, K, V> {}
703-
unsafe impl<K: Eq + Hash + Send + Sync, V: Send + Sync> Sync for Entry<'_, K, V> {}
702+
// Entry is intentionally !Send — like MutexGuard, it should not be moved across threads.
703+
// For Sync, only K: Sync and V: Sync are needed: sharing &Entry across threads only
704+
// requires shared references (&K, &Option<V>) to be safe to share, not ownership transfer.
705+
unsafe impl<K: Eq + Hash + Sync, V: Sync> Sync for Entry<'_, K, V> {}
704706

705707
impl<K: Eq + Hash, V> Entry<'_, K, V> {
706708
/// Returns a reference to the entry's key.
@@ -1199,4 +1201,12 @@ mod tests {
11991201
assert_eq!(lock_map.get(&(i as u32)), Some(i as u32));
12001202
}
12011203
}
1204+
1205+
// Compile-time trait assertions: Entry must be !Send but Sync when K, V: Sync.
1206+
const _: () = {
1207+
fn assert_sync<T: Sync>() {}
1208+
fn assert_entry_sync() {
1209+
assert_sync::<Entry<'_, String, String>>();
1210+
}
1211+
};
12021212
}

src/lru_lockmap.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,10 @@ pub struct LruEntry<'a, K: Eq + Hash, V> {
809809
}
810810

811811
// SAFETY: The guard holds a per-key mutex lock and a valid, ref-counted pointer.
812-
unsafe impl<K: Eq + Hash + Send, V: Send> Send for LruEntry<'_, K, V> {}
813-
unsafe impl<K: Eq + Hash + Send + Sync, V: Send + Sync> Sync for LruEntry<'_, K, V> {}
812+
// LruEntry is intentionally !Send — like MutexGuard, it should not be moved across threads.
813+
// For Sync, only K: Sync and V: Sync are needed: sharing &LruEntry across threads only
814+
// requires shared references (&K, &Option<V>) to be safe to share, not ownership transfer.
815+
unsafe impl<K: Eq + Hash + Sync, V: Sync> Sync for LruEntry<'_, K, V> {}
814816

815817
impl<K: Eq + Hash, V> LruEntry<'_, K, V> {
816818
/// Returns a reference to the entry's key.
@@ -1627,4 +1629,12 @@ mod tests {
16271629
THREADS as u32 * OPS_PER_THREAD as u32
16281630
);
16291631
}
1632+
1633+
// Compile-time trait assertions: LruEntry must be !Send but Sync when K, V: Sync.
1634+
const _: () = {
1635+
fn assert_sync<T: Sync>() {}
1636+
fn assert_lru_entry_sync() {
1637+
assert_sync::<LruEntry<'_, String, String>>();
1638+
}
1639+
};
16301640
}

0 commit comments

Comments
 (0)