From 6a265e3e94641a6c86e2ee8f1504d6f0ea646acf Mon Sep 17 00:00:00 2001 From: Ty Overby Date: Thu, 14 Sep 2017 15:06:23 -0700 Subject: [PATCH 1/2] remove send/sync retrictions when possible --- .gitignore | 1 + src/hamt.rs | 37 ++++++++++++++----------------------- src/item_store.rs | 16 ++++++++++++---- src/lib.rs | 2 -- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index e52e30e..f6fd755 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target/ benchmark/ +Cargo.lock diff --git a/src/hamt.rs b/src/hamt.rs index c4c4b0b..c51027e 100644 --- a/src/hamt.rs +++ b/src/hamt.rs @@ -60,8 +60,7 @@ enum BorrowedNodeRef<'a, K, V, IS, H> } impl NodeRef - where K: Eq+Send+Sync, - V: Send+Sync, + where K: Eq, IS: ItemStore, H: Hasher { @@ -183,9 +182,7 @@ enum NodeEntryRef<'a, K, V, IS, H> } impl<'a, K, V, IS, H> NodeEntryRef<'a, K, V, IS, H> - where K: Send+Sync, - V: Send+Sync, - IS: ItemStore + where IS: ItemStore { // Clones the contents of a NodeEntryRef into a NodeEntryOwned value to be used elsewhere. fn clone_out(&self) -> NodeEntryOwned { @@ -406,8 +403,7 @@ impl<'a, K, V, IS, H> UnsafeNode // impl UnsafeNode (continued) impl UnsafeNode - where K: Eq+Send+Sync+Hash, - V: Send+Sync, + where K: Eq+Hash, IS: ItemStore, H: Hasher+Default { @@ -1159,8 +1155,7 @@ pub struct HamtMap, H=StdHasher> { // impl HamtMap impl HamtMap - where K: Eq+Send+Sync+Hash, - V: Send+Sync, + where K: Eq+Hash, IS: ItemStore, H: Hasher+Default { @@ -1336,8 +1331,7 @@ impl Clone for HamtMap { // Default for HamtMap impl Default for HamtMap - where K: Eq+Send+Sync+Hash, - V: Send+Sync, + where K: Eq+Hash, IS: ItemStore, H: Hasher+Default { @@ -1347,8 +1341,8 @@ impl Default for HamtMap } impl<'a, K, V, IS, H> IntoIterator for &'a HamtMap - where K: Eq+Send+Sync+Hash+'a, - V: Send+Sync+'a, + where K: Eq+Hash+'a, + V: 'a, IS: ItemStore+'a, H: Hasher+Default+'a { @@ -1363,8 +1357,8 @@ impl<'a, K, V, IS, H> IntoIterator for &'a HamtMap // Eq for HamtMap impl PartialEq for HamtMap - where K: Eq+Send+Sync+Hash, - V: PartialEq+Send+Sync, + where K: Eq+Hash, + V: PartialEq, IS: ItemStore, H: Hasher+Default { @@ -1397,8 +1391,8 @@ impl PartialEq for HamtMap // Eq for HamtMap impl Eq for HamtMap - where K: Eq+Send+Sync+Hash, - V: Eq+Send+Sync, + where K: Eq+Hash, + V: Eq, IS: ItemStore, H: Hasher+Default { @@ -1407,8 +1401,7 @@ impl Eq for HamtMap // FromIterator impl ::std::iter::FromIterator<(K, V)> for HamtMap - where K: Eq+Send+Sync+Hash, - V: Send+Sync, + where K: Eq+Hash, IS: ItemStore, H: Hasher+Default { @@ -1467,8 +1460,7 @@ pub struct HamtMapIterator<'a, K, V, IS, H> impl<'a, K, V, IS, H> HamtMapIterator<'a, K, V, IS, H> - where K: Eq+Send+Sync, - V: Send+Sync, + where K: Eq, IS: ItemStore, H: Hasher { @@ -1486,8 +1478,7 @@ HamtMapIterator<'a, K, V, IS, H> impl<'a, K, V, IS, H> Iterator for HamtMapIterator<'a, K, V, IS, H> - where K: Eq+Send+Sync, - V: Send+Sync, + where K: Eq, IS: ItemStore, H: 'a + Hasher { diff --git a/src/item_store.rs b/src/item_store.rs index dbfee89..3958b3c 100644 --- a/src/item_store.rs +++ b/src/item_store.rs @@ -23,7 +23,7 @@ use std::sync::Arc; //=------------------------------------------------------------------------------------------------- // trait ItemStore //=------------------------------------------------------------------------------------------------- -pub trait ItemStore: Clone+Send+Sync { +pub trait ItemStore: Clone { fn key<'a>(&'a self) -> &'a K; fn val<'a>(&'a self) -> &'a V; @@ -40,7 +40,7 @@ pub struct CopyStore { val: V } -impl ItemStore for CopyStore { +impl ItemStore for CopyStore { fn key<'a>(&'a self) -> &'a K { &self.key } fn val<'a>(&'a self) -> &'a V { &self.val } @@ -52,7 +52,7 @@ impl ItemStore for CopyStore } } -impl Clone for CopyStore { +impl Clone for CopyStore { fn clone(&self) -> CopyStore { CopyStore { key: self.key.clone(), @@ -61,12 +61,16 @@ impl Clone for CopyStore { } } +unsafe impl Send for CopyStore where K: Send, V: Send { } + +unsafe impl Sync for CopyStore where K: Sync, V: Sync { } + //=------------------------------------------------------------------------------------------------- // struct ShareStore //=------------------------------------------------------------------------------------------------- -pub struct ShareStore { +pub struct ShareStore { store: Arc<(K, V)>, } @@ -86,3 +90,7 @@ impl Clone for ShareStore { } } } + +unsafe impl Send for ShareStore where K: Send + Sync, V: Send + Sync { } + +unsafe impl Sync for ShareStore where K: Send + Sync, V: Send + Sync { } diff --git a/src/lib.rs b/src/lib.rs index a66dbb8..77d9175 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,8 +28,6 @@ extern crate libc; -extern crate rand; - pub use hamt::HamtMap; pub use hamt::HamtMapIterator; pub use item_store::{ItemStore, ShareStore, CopyStore}; From c9209d4d85452544495ddeca13b9293647815e01 Mon Sep 17 00:00:00 2001 From: Ty Overby Date: Thu, 14 Sep 2017 17:42:37 -0700 Subject: [PATCH 2/2] fix rand dependency --- Cargo.toml | 1 + src/lib.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a2416ea..1768220 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ license = "MIT" [dependencies] libc = "^0.2" +[dev-dependencies] rand = "^0.3.9" diff --git a/src/lib.rs b/src/lib.rs index 77d9175..16cb6e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,8 @@ #![allow(unused_parens)] extern crate libc; +#[cfg(test)] +extern crate rand; pub use hamt::HamtMap; pub use hamt::HamtMapIterator;