Slightly better mutation semantics.

This commit is contained in:
Gav Wood 2015-12-17 12:32:35 +01:00
parent e08958d502
commit 8687d0d097
2 changed files with 7 additions and 5 deletions

View File

@ -97,7 +97,7 @@ impl Account {
} }
// fetch - cannot be done in match because of the borrow rules. // fetch - cannot be done in match because of the borrow rules.
let t = TrieDB::new_existing(db, &mut self.storage_root); let t = TrieDB::new_existing(db, &mut self.storage_root);
let r = H256::from_slice(t.at(key.bytes()).unwrap_or(&[0u8;32][..])); let r = H256::from_slice(t.get(key.bytes()).unwrap_or(&[0u8;32][..]));
self.storage_overlay.insert(key, r.clone()); self.storage_overlay.insert(key, r.clone());
r r
} }

View File

@ -6,6 +6,8 @@ use util::trie::*;
use util::rlp::*; use util::rlp::*;
use util::uint::*; use util::uint::*;
use std::mem; use std::mem;
//use std::cell::*;
//use std::ops::*;
use account::Account; use account::Account;
/* /*
enum ValueOrRef<'self, 'db: 'self> { enum ValueOrRef<'self, 'db: 'self> {
@ -159,11 +161,11 @@ impl State {
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too. /// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
/// `force_create` creates a new, empty basic account if there is not currently an active account. /// `force_create` creates a new, empty basic account if there is not currently an active account.
// TODO: make immutable. // TODO: make immutable.
fn get(&mut self, a: &Address, require_code: bool) -> Option<&mut Account> { fn get(&mut self, a: &Address, require_code: bool) -> Option<&Account> {
if self.cache.get(a).is_none() { if self.cache.get(a).is_none() {
// load from trie. // load from trie.
let t = TrieDB::new_existing(&mut self.db, &mut self.root); let t = TrieDB::new_existing(&mut self.db, &mut self.root);
self.cache.insert(a.clone(), t.at(&a).map(|rlp| { println!("RLP: {:?}", rlp); Account::from_rlp(rlp) })); self.cache.insert(a.clone(), t.get(&a).map(|rlp| { println!("RLP: {:?}", rlp); Account::from_rlp(rlp) }));
} }
let db = &self.db; let db = &self.db;
@ -171,7 +173,7 @@ impl State {
if require_code { if require_code {
account.cache_code(db); account.cache_code(db);
} }
account account as &Account
}) })
} }
@ -180,7 +182,7 @@ impl State {
fn require(&mut self, a: &Address, require_code: bool) -> &mut Account { fn require(&mut self, a: &Address, require_code: bool) -> &mut Account {
if self.cache.get(a).is_none() { if self.cache.get(a).is_none() {
// load from trie. // load from trie.
self.cache.insert(a.clone(), TrieDB::new(&mut self.db, &mut self.root).at(&a).map(|rlp| Account::from_rlp(rlp))); self.cache.insert(a.clone(), TrieDB::new(&mut self.db, &mut self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
} }
if self.cache.get(a).unwrap().is_none() { if self.cache.get(a).unwrap().is_none() {