Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax trait bounds on generic parameters #275

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use arbitrary::{Arbitrary, Unstructured};
use core::hash::BuildHasher;
use core::hash::{BuildHasher, Hash};

impl<'a, K, V, S> Arbitrary<'a> for crate::DashMap<K, V, S>
where
K: Eq + std::hash::Hash + Arbitrary<'a>,
K: Eq + Hash + Arbitrary<'a>,
V: Arbitrary<'a>,
S: Default + BuildHasher + Clone,
{
Expand Down
53 changes: 15 additions & 38 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::lock::{RwLockReadGuard, RwLockWriteGuard};
use crate::t::Map;
use crate::util::SharedValue;
use crate::{DashMap, HashMap};
use core::hash::{BuildHasher, Hash};
use core::mem;
use hashbrown::hash_map;
use std::collections::hash_map::RandomState;
Expand All @@ -29,7 +28,7 @@ pub struct OwningIter<K, V, S = RandomState> {
current: Option<GuardOwningIter<K, V>>,
}

impl<K: Eq + Hash, V, S: BuildHasher + Clone> OwningIter<K, V, S> {
impl<K, V, S> OwningIter<K, V, S> {
pub(crate) fn new(map: DashMap<K, V, S>) -> Self {
Self {
map,
Expand All @@ -41,7 +40,7 @@ impl<K: Eq + Hash, V, S: BuildHasher + Clone> OwningIter<K, V, S> {

type GuardOwningIter<K, V> = hash_map::IntoIter<K, SharedValue<V>>;

impl<K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for OwningIter<K, V, S> {
impl<K, V, S: Clone> Iterator for OwningIter<K, V, S> {
type Item = (K, V);

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -75,22 +74,6 @@ impl<K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for OwningIter<K, V, S> {
}
}

unsafe impl<K, V, S> Send for OwningIter<K, V, S>
where
K: Eq + Hash + Send,
V: Send,
S: BuildHasher + Clone + Send,
Copy link
Contributor Author

@tomkarw tomkarw Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite inconsistent with other Send/Sync implementations, they all just require K: Send, V: Send. I'm not quite sure why S: Send is not required in all other cases, or why it's required here.

Also, it would be wise to add Safety comments to Send (Sync) implementations, possibly including rationale why we S: Send/Sync is not required.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there still a willingness accept this type of change? I am happy to go through the existing code base and relax the trait bounds where possible. I'll leave Send/Sync bounds where they are.

{
}

unsafe impl<K, V, S> Sync for OwningIter<K, V, S>
where
K: Eq + Hash + Sync,
V: Sync,
S: BuildHasher + Clone + Sync,
{
}

type GuardIter<'a, K, V, S> = (
Arc<RwLockReadGuard<'a, HashMap<K, V, S>>>,
hash_map::Iter<'a, K, SharedValue<V>>,
Expand Down Expand Up @@ -118,31 +101,31 @@ pub struct Iter<'a, K, V, S = RandomState, M = DashMap<K, V, S>> {
current: Option<GuardIter<'a, K, V, S>>,
}

impl<'i, K: Clone + Hash + Eq, V: Clone, S: Clone + BuildHasher> Clone for Iter<'i, K, V, S> {
impl<'i, K: Clone, V: Clone, S: Clone> Clone for Iter<'i, K, V, S> {
fn clone(&self) -> Self {
Iter::new(self.map)
}
}

unsafe impl<'a, 'i, K, V, S, M> Send for Iter<'i, K, V, S, M>
where
K: 'a + Eq + Hash + Send,
K: 'a + Send,
V: 'a + Send,
S: 'a + BuildHasher + Clone,
S: 'a + Clone,
M: Map<'a, K, V, S>,
{
}

unsafe impl<'a, 'i, K, V, S, M> Sync for Iter<'i, K, V, S, M>
where
K: 'a + Eq + Hash + Sync,
K: 'a + Sync,
V: 'a + Sync,
S: 'a + BuildHasher + Clone,
S: 'a + Clone,
M: Map<'a, K, V, S>,
{
}

impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter<'a, K, V, S, M> {
impl<'a, K, V, S: 'a, M: Map<'a, K, V, S>> Iter<'a, K, V, S, M> {
pub(crate) fn new(map: &'a M) -> Self {
Self {
map,
Expand All @@ -152,9 +135,7 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
}
}

impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iterator
for Iter<'a, K, V, S, M>
{
impl<'a, K, V, S: 'a, M: Map<'a, K, V, S>> Iterator for Iter<'a, K, V, S, M> {
type Item = RefMulti<'a, K, V, S>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -204,25 +185,23 @@ pub struct IterMut<'a, K, V, S = RandomState, M = DashMap<K, V, S>> {

unsafe impl<'a, 'i, K, V, S, M> Send for IterMut<'i, K, V, S, M>
where
K: 'a + Eq + Hash + Send,
K: 'a + Send,
V: 'a + Send,
S: 'a + BuildHasher + Clone,
S: 'a + Clone,
M: Map<'a, K, V, S>,
{
}

unsafe impl<'a, 'i, K, V, S, M> Sync for IterMut<'i, K, V, S, M>
where
K: 'a + Eq + Hash + Sync,
K: 'a + Sync,
V: 'a + Sync,
S: 'a + BuildHasher + Clone,
S: 'a + Clone,
M: Map<'a, K, V, S>,
{
}

impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>>
IterMut<'a, K, V, S, M>
{
impl<'a, K, V, S: 'a, M: Map<'a, K, V, S>> IterMut<'a, K, V, S, M> {
pub(crate) fn new(map: &'a M) -> Self {
Self {
map,
Expand All @@ -232,9 +211,7 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>>
}
}

impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iterator
for IterMut<'a, K, V, S, M>
{
impl<'a, K, V, S: 'a, M: Map<'a, K, V, S>> Iterator for IterMut<'a, K, V, S, M> {
type Item = RefMutMulti<'a, K, V, S>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
27 changes: 12 additions & 15 deletions src/iter_set.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::setref::multiple::RefMulti;
use crate::t::Map;
use core::hash::{BuildHasher, Hash};

pub struct OwningIter<K, S> {
inner: crate::iter::OwningIter<K, (), S>,
}

impl<K: Eq + Hash, S: BuildHasher + Clone> OwningIter<K, S> {
impl<K, S> OwningIter<K, S> {
pub(crate) fn new(inner: crate::iter::OwningIter<K, (), S>) -> Self {
Self { inner }
}
}

impl<K: Eq + Hash, S: BuildHasher + Clone> Iterator for OwningIter<K, S> {
impl<K, S: Clone> Iterator for OwningIter<K, S> {
type Item = K;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -22,15 +21,15 @@ impl<K: Eq + Hash, S: BuildHasher + Clone> Iterator for OwningIter<K, S> {

unsafe impl<K, S> Send for OwningIter<K, S>
where
K: Eq + Hash + Send,
S: BuildHasher + Clone + Send,
K: Send,
S: Send,
{
}

unsafe impl<K, S> Sync for OwningIter<K, S>
where
K: Eq + Hash + Sync,
S: BuildHasher + Clone + Sync,
K: Sync,
S: Sync,
{
}

Expand All @@ -40,29 +39,27 @@ pub struct Iter<'a, K, S, M> {

unsafe impl<'a, 'i, K, S, M> Send for Iter<'i, K, S, M>
where
K: 'a + Eq + Hash + Send,
S: 'a + BuildHasher + Clone,
K: 'a + Send,
S: 'a,
M: Map<'a, K, (), S>,
{
}

unsafe impl<'a, 'i, K, S, M> Sync for Iter<'i, K, S, M>
where
K: 'a + Eq + Hash + Sync,
S: 'a + BuildHasher + Clone,
K: 'a + Sync,
S: 'a,
M: Map<'a, K, (), S>,
{
}

impl<'a, K: Eq + Hash, S: 'a + BuildHasher + Clone, M: Map<'a, K, (), S>> Iter<'a, K, S, M> {
impl<'a, K, S: 'a, M: Map<'a, K, (), S>> Iter<'a, K, S, M> {
pub(crate) fn new(inner: crate::iter::Iter<'a, K, (), S, M>) -> Self {
Self { inner }
}
}

impl<'a, K: Eq + Hash, S: 'a + BuildHasher + Clone, M: Map<'a, K, (), S>> Iterator
for Iter<'a, K, S, M>
{
impl<'a, K, S: 'a, M: Map<'a, K, (), S>> Iterator for Iter<'a, K, S, M> {
type Item = RefMulti<'a, K, S>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
Loading