Locker Room
Readers-writer access to individual cells of your collection!
LockerRoom and LockerRoomAsync
The central features of the crate is implemented by these structures. More specifically, they provide such functionality:
- Shared readers access to single cell of collection with
LockerRoom::read_cellandLockerRoomAsync::read_cell; - Exclusive writer access to single cell of collection with
LockerRoom::write_cellandLockerRoomAsync::write_cell; - Exclusive writer access to whole collection with
LockerRoom::lock_roomandLockerRoomAsync::lock_room.
But LockerRoomAsync is optional - you need to enable feature async to use it. It depends on
tokio's RwLock.
LockerRoom example
let v = vec!;
let locker_room: = v.into;
let locker_room = new;
scope;
assert_eq!;
LockerRoomAsync example
let v = vec!;
let locker_room: = v.into;
let locker_room = new;
let locker_room_cloned = clone;
let join1 = spawn;
let locker_room_cloned = clone;
let join2 = spawn;
join!;
assert_eq!;
Deadlock example
Carefully block multiple cells in one scope. Otherwise, situation like this may occur:
// Thread 1 | // Thread 2
let _w1 = locker_room.write_cell(0); |
| let _w1 = locker_room.write_cell(1);
// will block
let _w2 = locker_room.write_cell(1); |
| // will deadlock
| let _w2 = locker_room.write_cell(0);
Collections?
By default you can create LockerRoom and LockerRoomAsync from array, Vec, VecDeque, HashMap and BTreeMap.
But the crate provides traits, by which implementing to your collection, you can make it compatible with LockerRoom and LockerRoomAsync.
Collection
Crucial part of the crate that helps your collection to be compatible with LockerRoom.
Just implement it into your collection and everything will work!
In fact, there is two different Collections: sync::Collection and async::Collection.
First one is for the LockerRoom and the second one is for the LockerRoomAsync. So you need to implement both of them for your collection to use it with
both LockerRooms.
That's bad design. But I'll fix it in next versions of the crate.
Example
Let's implement the trait for the struct from Index's example: