From: Ian Jackson Date: Fri, 23 Oct 2020 22:28:19 +0000 (+0100) Subject: wip new account etc. X-Git-Tag: otter-0.2.0~605 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=df0493e5d042e9e9c62a9256bd91d0c9211fb34b;p=otter.git wip new account etc. Signed-off-by: Ian Jackson --- diff --git a/src/accounts.rs b/src/accounts.rs index 4398dd6e..2b6e890f 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -5,6 +5,7 @@ use crate::imports::*; use parking_lot::{RwLock, const_rwlock, + RwLockReadGuard, RwLockWriteGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard}; #[derive(Debug,Clone,Deserialize,Serialize)] @@ -139,6 +140,7 @@ pub struct AccountRecord { } #[derive(Copy,Clone,Debug,Ord,PartialOrd,Eq,PartialEq)] +#[derive(Serialize,Deserialize)] pub struct TokenRevelation { pub latest: Timestamp, pub earliest: Timestamp, @@ -156,25 +158,31 @@ pub fn save_accounts_now() -> Result<(), InternalError> { impl AccountRecord { pub fn lookup(account: &AccountName, _: Authorisation) - -> Option> { - ACCOUNTS.read().map( - |accounts| accounts?.get(account) - ) + -> Option> { + RwLockReadGuard::try_map( + ACCOUNTS.read(), + |accounts: &Option>| -> Option<_> { + accounts.as_ref()?.get(account) + } + ).ok() } pub fn lookup_mut_caller_must_save(account: &AccountName, _: Authorisation) -> Option> { - ACCOUNTS.write().map( - |accounts| accounts?.get(account) - ) + RwLockWriteGuard::try_map( + ACCOUNTS.write(), + |accounts: &mut Option>| -> Option<_> { + accounts.as_mut()?.get_mut(account) + } + ).ok() } pub fn with_entry_mut(account: &AccountName, - _: Authorisation, + auth: Authorisation, f: F) -> Result where F: FnOnce(Option<&mut AccountRecord>) -> T { - let entry = AccountRecord::lookup_mut_caller_must_save(account); + let entry = AccountRecord::lookup_mut_caller_must_save(account, auth); let output = f(*entry); let ok = if entry.is_some() { save_accounts_now() } else { Ok(()) }; match ok {