diff --git a/src/ledger/address.rs b/src/ledger/address.rs index 88dd05a..c198b24 100644 --- a/src/ledger/address.rs +++ b/src/ledger/address.rs @@ -119,12 +119,12 @@ mod tests { fn addresses() { let ledger = Ledger::new(); - assert_eq!(ledger.credentials.take().len(), 0); + assert_eq!(ledger.credentials.lock().unwrap().take().len(), 0); let credential = Ledger::generate_credential(); ledger.add_credential(credential.clone()); - let credentials = ledger.credentials.take(); + let credentials = ledger.credentials.lock().unwrap().take(); assert_eq!(credentials.len(), 1); assert_eq!(credential, credentials.get(0).unwrap().to_owned()); diff --git a/src/ledger/macros.rs b/src/ledger/macros.rs index 75fa954..e97465f 100644 --- a/src/ledger/macros.rs +++ b/src/ledger/macros.rs @@ -7,11 +7,11 @@ macro_rules! add_item_to_vec { ($member:expr, $item:expr) => { // Update item list. - let mut items = $member.take(); + let mut items = $member.lock().unwrap().take(); items.push($item); // Commit new change. - $member.set(items); + $member.lock().unwrap().set(items); }; } /// Removes given item from a `Vec` member, which is guarded by a `Cell`. @@ -19,21 +19,21 @@ macro_rules! add_item_to_vec { macro_rules! remove_item_from_vec { ($member:expr, $item:expr) => { // Get item list. - let mut items = $member.take(); + let mut items = $member.lock().unwrap().take(); // Delete given item. items.retain(|&i| i != $item); // Commit new change. - $member.set(items); + $member.lock().unwrap().set(items); }; } /// Returns items of a `Vec` member, which is guarded by a `Cell`. #[macro_export] macro_rules! return_vec_item { ($member:expr) => { - let items = $member.take(); - $member.set(items.clone()); + let items = $member.lock().unwrap().take(); + $member.lock().unwrap().set(items.clone()); return items; }; @@ -43,7 +43,7 @@ macro_rules! return_vec_item { #[macro_export] macro_rules! update_item { ($member:expr, $item:expr) => { - $member.set($item); + $member.lock().unwrap().set($item); }; } @@ -51,20 +51,23 @@ macro_rules! update_item { #[macro_export] macro_rules! get_item { ($member:expr, $assignee:ident) => { - let $assignee = $member.take(); - $member.set($assignee.clone()); + let $assignee = $member.lock().unwrap().take(); + $member.lock().unwrap().set($assignee.clone()); }; } #[cfg(test)] mod tests { - use std::cell::Cell; + use std::{ + cell::Cell, + sync::{Arc, Mutex}, + }; /// Temporary struct for macro testing. #[derive(Default)] struct Test { - pub vec_member_1: Cell>, - pub int_member_1: Cell, + pub vec_member_1: Arc>>>, + pub int_member_1: Arc>>, } #[test] diff --git a/src/ledger/mod.rs b/src/ledger/mod.rs index 838d00c..093816b 100644 --- a/src/ledger/mod.rs +++ b/src/ledger/mod.rs @@ -7,7 +7,10 @@ use address::UserCredential; use bitcoin::{OutPoint, Transaction}; -use std::cell::Cell; +use std::{ + cell::Cell, + sync::{Arc, Mutex}, +}; mod address; mod errors; @@ -19,20 +22,20 @@ mod utxo; /// Mock Bitcoin ledger. pub struct Ledger { /// User's keys and address. - credentials: Cell>, + credentials: Arc>>>, /// Happened transactions. - transactions: Cell>, + transactions: Arc>>>, /// Unspent transaction outputs. - utxos: Cell>, + utxos: Arc>>>, } impl Ledger { /// Creates a new empty ledger. pub fn new() -> Self { Self { - credentials: Cell::new(Vec::new()), - utxos: Cell::new(Vec::new()), - transactions: Cell::new(Vec::new()), + credentials: Arc::new(Mutex::new(Cell::new(Vec::new()))), + utxos: Arc::new(Mutex::new(Cell::new(Vec::new()))), + transactions: Arc::new(Mutex::new(Cell::new(Vec::new()))), } } }