Skip to content

Add Ref/RefMut::map_split #338

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

Open
wants to merge 1 commit 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
26 changes: 13 additions & 13 deletions src/mapref/multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use core::hash::Hash;
use core::ops::{Deref, DerefMut};
use std::sync::Arc;

pub struct RefMulti<'a, K, V> {
_guard: Arc<RwLockReadGuardDetached<'a>>,
k: &'a K,
v: &'a V,
pub struct RefMulti<'a, K, V: ?Sized> {
pub(super) _guard: Arc<RwLockReadGuardDetached<'a>>,
pub(super) k: &'a K,
pub(super) v: &'a V,
}

impl<'a, K: Eq + Hash, V> RefMulti<'a, K, V> {
impl<'a, K: Eq + Hash, V: ?Sized> RefMulti<'a, K, V> {
pub(crate) fn new(guard: Arc<RwLockReadGuardDetached<'a>>, k: &'a K, v: &'a V) -> Self {
Self {
_guard: guard,
Expand All @@ -31,21 +31,21 @@ impl<'a, K: Eq + Hash, V> RefMulti<'a, K, V> {
}
}

impl<'a, K: Eq + Hash, V> Deref for RefMulti<'a, K, V> {
impl<'a, K: Eq + Hash, V: ?Sized> Deref for RefMulti<'a, K, V> {
type Target = V;

fn deref(&self) -> &V {
self.value()
}
}

pub struct RefMutMulti<'a, K, V> {
_guard: Arc<RwLockWriteGuardDetached<'a>>,
k: &'a K,
v: &'a mut V,
pub struct RefMutMulti<'a, K, V: ?Sized> {
pub(super) _guard: Arc<RwLockWriteGuardDetached<'a>>,
pub(super) k: &'a K,
pub(super) v: &'a mut V,
}

impl<'a, K: Eq + Hash, V> RefMutMulti<'a, K, V> {
impl<'a, K: Eq + Hash, V: ?Sized> RefMutMulti<'a, K, V> {
pub(crate) fn new(guard: Arc<RwLockWriteGuardDetached<'a>>, k: &'a K, v: &'a mut V) -> Self {
Self {
_guard: guard,
Expand Down Expand Up @@ -75,15 +75,15 @@ impl<'a, K: Eq + Hash, V> RefMutMulti<'a, K, V> {
}
}

impl<'a, K: Eq + Hash, V> Deref for RefMutMulti<'a, K, V> {
impl<'a, K: Eq + Hash, V: ?Sized> Deref for RefMutMulti<'a, K, V> {
type Target = V;

fn deref(&self) -> &V {
self.value()
}
}

impl<'a, K: Eq + Hash, V> DerefMut for RefMutMulti<'a, K, V> {
impl<'a, K: Eq + Hash, V: ?Sized> DerefMut for RefMutMulti<'a, K, V> {
fn deref_mut(&mut self) -> &mut V {
self.value_mut()
}
Expand Down
84 changes: 82 additions & 2 deletions src/mapref/one.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::lock::{RwLockReadGuardDetached, RwLockWriteGuardDetached};
use crate::{
lock::{RwLockReadGuardDetached, RwLockWriteGuardDetached},
mapref::multiple::{RefMulti, RefMutMulti},
};
use core::hash::Hash;
use core::ops::{Deref, DerefMut};
use std::fmt::{Debug, Formatter};
use std::{
fmt::{Debug, Formatter},
sync::Arc,
};

pub struct Ref<'a, K, V> {
_guard: RwLockReadGuardDetached<'a>,
Expand Down Expand Up @@ -55,6 +61,29 @@ impl<'a, K: Eq + Hash, V> Ref<'a, K, V> {
Err(self)
}
}

pub fn map_split<F, A: ?Sized, B: ?Sized>(
self,
f: F,
) -> (RefMulti<'a, K, A>, RefMulti<'a, K, B>)
where
F: FnOnce(&V) -> (&A, &B),
{
let (a, b) = f(self.v);
let guard = Arc::new(self._guard);
(
RefMulti {
_guard: guard.clone(),
k: self.k,
v: a,
},
RefMulti {
_guard: guard,
k: self.k,
v: b,
},
)
}
}

impl<'a, K: Eq + Hash + Debug, V: Debug> Debug for Ref<'a, K, V> {
Expand Down Expand Up @@ -140,6 +169,29 @@ impl<'a, K: Eq + Hash, V> RefMut<'a, K, V> {
v,
})
}

pub fn map_split<F, A: ?Sized, B: ?Sized>(
self,
f: F,
) -> (RefMutMulti<'a, K, A>, RefMutMulti<'a, K, B>)
where
F: FnOnce(&mut V) -> (&mut A, &mut B),
{
let (a, b) = f(self.v);
let guard = Arc::new(self.guard);
(
RefMutMulti {
_guard: guard.clone(),
k: self.k,
v: a,
},
RefMutMulti {
_guard: guard,
k: self.k,
v: b,
},
)
}
}

impl<'a, K: Eq + Hash + Debug, V: Debug> Debug for RefMut<'a, K, V> {
Expand Down Expand Up @@ -373,6 +425,34 @@ mod tests {
};
}

#[test]
fn ref_map_split() {
struct Data(String, String);
let data = DashMap::new();
data.insert("test", Data("hello".to_string(), "world".to_string()));
if let Some(b_ref) = data.get("test") {
let (l_ref, r_ref) = b_ref.map_split(|d| (&d.0, &d.1));

assert_eq!(l_ref.value(), "hello");
assert_eq!(r_ref.value(), "world");
};
}

#[test]
fn ref_mut_map_split() {
let data = DashMap::new();
data.insert("test", "hello world".to_string());
if let Some(b_ref) = data.get_mut("test") {
let (mut l_ref, r_ref) = b_ref.map_split(|d| d.split_at_mut(5));

assert_eq!(l_ref.value(), "hello");
assert_eq!(r_ref.value(), " world");
l_ref.make_ascii_uppercase();
};
let Some(b_ref) = data.get("test") else { panic!("") };
assert_eq!(b_ref.value(), "HELLO world");
}

#[test]
fn mapped_ref_again() {
let data = DashMap::new();
Expand Down