#map #arc #arc-mutex #mutex #lock #sync #locking

arc_map

A Map of Arc<Mutex<V>> that eases the access of indivdual members

4 releases

Uses old Rust 2015

0.1.3 Aug 28, 2018
0.1.2 Aug 28, 2018
0.1.1 Aug 20, 2018
0.1.0 Aug 18, 2018

#12 in #arc-mutex

30 downloads per month

MIT license

9KB
151 lines

ArcMap

Arc Map is designed to allow a HashMap of Arc<Mutex<T>> to be stored in such a way that elements can be accessed without the need to lock the whole Map while you have access to them.

Contributions Welcome

changes in v0.1.3

changed &mut self to &self, in three main methods. making it much easier to use as part of another object.

changes in v0.1.2

Switched main channel to SyncSender, for sync in parents


lib.rs:

The ArcMap exists to enable multiple Mutex based Elements in a Map to be accessible

without the need for locking the whole HashMap, while you access one element.

Instead the Map is hidden inside a thread, that will return accesible elements as requested.

Rather than limit the access within the fn it seemed simple to return the Arc directly.

Though because the Map may not contain the required element, and becuase of thread send/recieve. The get method returns a "Result<Arc<Mutex>,AMapErr>"

This can be accessed as follows : (Though normally for more complex objects)

use arc_map::ArcMap;
let mut am = ArcMap::new();

//update by grabbing mutex  
am.insert(3,"hello".to_string());
{
    let p = am.get(3).unwrap(); //p is Arc<Mutex<String>>
    let mut s = p.lock().unwrap();
    s.push_str(" world");
}

//read by grabbing mutex 
{ 
    let p2 = am.get(3).unwrap();
    let s2 = p2.lock().unwrap();
    assert_eq!(*s2,"hello world".to_string());
}


am.insert(4,"goodbye".to_string());

//update in place (No need for scoping)
am.on_do(4,|mut s| (&mut s).push_str(" cruel world")).unwrap();

//get info out
let ls = am.on_do(4,|s| s.clone()).unwrap();

assert_eq!(&ls,"goodbye cruel world");

While this can be achieved using traditional mutex locks, the interface here is much simpler to use.

No runtime deps