https://github.com/harryscholes/observable-maps
Thread-safe, generic, observable hash maps that notify observers of state changes
https://github.com/harryscholes/observable-maps
Last synced: 2 months ago
JSON representation
Thread-safe, generic, observable hash maps that notify observers of state changes
- Host: GitHub
- URL: https://github.com/harryscholes/observable-maps
- Owner: harryscholes
- Created: 2021-11-04T14:10:05.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-05-20T09:18:23.000Z (about 2 years ago)
- Last Synced: 2025-02-05T10:49:17.814Z (4 months ago)
- Language: Rust
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# observable-maps
Thread-safe, generic, observable hash maps that notify observers of state changes.
### Usage
```rust
use std::{thread, time::Duration};use observable_maps::{ObservableMap, ThreadSafeObserverMap};
use rust_decimal_macros::dec;fn main() {
// Types that are `ObservableMap` are generic, which allows them to store
// arbitrary precision decimal types, for example.
let mut map = ThreadSafeObserverMap::new();let key = "pi";
let value = dec!(3.1415926535897932384);{
// `ThreadSafeObserverMap` is made thread-safe by encapsulating an `Arc`,
// which automatically increases the reference count to the price holder
// when it is cloned.
let mut map = map.clone();// Spawn a new thread ...
thread::spawn(move || {
// ... that waits for some time ...
thread::sleep(Duration::from_secs(1));// ... then inserts a new value into the map.
map.insert(key, value).unwrap();println!("Inserted {} => {}", key, value);
})
};// Wait for the value of a key to be updated in the map,
// by blocking execution of the thread.
let updated_value = map.wait(key).unwrap();println!("Updated {} => {}", key, updated_value);
assert_eq!(updated_value, value);
}
```Outputs:
```sh
$ cargo run --quiet main.rs
Updated pi => 3.1415926535897932384
Inserted pi => 3.1415926535897932384
```