acid_state/lib.rs
1//! `acid-state` adds durable transaction support to any serializable structure.
2#![crate_type = "lib"]
3// #![deny(missing_docs)]
4
5//! Transactional state.
6//!
7//! # Examples
8//!
9//! ```ignore
10//! #[macro_use]
11//! extern crate acid_state;
12//! #[macro_use]
13//! extern crate lazy_static;
14//! extern crate rustc_serialize;
15//!
16//! use std::collections::HashMap;
17//!
18//! #[derive(Debug, RustcDecodable, RustcEncodable)]
19//! struct Cs {
20//! v: u64,
21//! }
22//!
23//! acid_state! {
24//! A: HashMap<String, u64> = HashMap::new();
25//! B: u64 = 0;
26//! C: Cs = Cs {
27//! v: 0
28//! };
29//! }
30//!
31//! let key = "yo".to_owned();
32//! acid! {
33//! (A => a, B => b, C => c) => {
34//! // A, B, C have been pulled off disk
35//! let mut current = a.entry(key).or_insert(0);
36//! **b += 1;
37//! *current += 1;
38//! c.v += 1;
39//! println!("a is now {:?}", current);
40//! println!("b is now {:?}", **b);
41//! println!("c is now {:?}", c.v);
42//! // new values of A, B, C are now synced on disk
43//! }
44//! };
45//! ```
46
47pub use std::sync::{Arc as __ARC, Mutex as __MUTEX};
48
49#[macro_use]
50extern crate lazy_static;
51extern crate rustc_serialize;
52extern crate bincode;
53
54mod wrapper_structs;
55pub use wrapper_structs::{Persistent, Txn};
56
57#[macro_export]
58macro_rules! acid_state {
59 ($N:ident : $T:ty = $e:expr; $($rest:tt)*) => {
60 lazy_static!(
61 static ref $N : $crate::Persistent<$T> =
62 $crate::Persistent {
63 inner: $crate::__ARC::new($crate::__MUTEX::new($e)),
64 name: format!("{}.acidstate", stringify!($N)).into(),
65 };
66 );
67 acid_state!($($rest)*);
68 };
69 () => ()
70}
71
72// begin txn locking [idents], body with idents handle()'d, persist, unlock [idents]
73#[macro_export]
74macro_rules! acid {
75 (($($src:ident => $dst:ident),*) => $b:block) => {
76 {
77 $(let mut $dst = $src.handle();)*
78 $b
79 }
80 };
81}