Switch from Rc<RefCell> to Arc<Mutex<T>> or Arc<ReentrantMutex<RefCell<T>>>.

This commit is contained in:
Oleksandr Kozachuk
2023-01-06 13:12:58 +01:00
parent a2978c1230
commit 538a32a471
13 changed files with 384 additions and 214 deletions
+28 -4
View File
@@ -1,8 +1,13 @@
use crate::password::{fix_password_recursion, Name, PasswordRef};
use parking_lot::ReentrantMutex;
use regex::{Captures, Regex};
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
#[derive(PartialEq, Debug)]
pub type LKRef = Arc<ReentrantMutex<RefCell<LK>>>;
#[derive(Debug)]
pub struct LK {
pub db: HashMap<Name, PasswordRef>,
pub ls: HashMap<String, PasswordRef>,
@@ -24,7 +29,7 @@ impl LK {
}
for db in vec![&self.db, &self.ls] {
for (_, name) in db {
let comment = name.borrow().comment.clone();
let comment = name.lock().borrow().comment.clone();
match comment {
Some(comment) => {
let mut changed = false;
@@ -33,7 +38,7 @@ impl LK {
let folder = c[1].to_string();
match self.db.get(&folder) {
Some(entry) => {
name.borrow_mut().parent = Some(entry.clone());
name.lock().borrow_mut().parent = Some(entry.clone());
changed = true;
}
None => (),
@@ -43,7 +48,7 @@ impl LK {
.trim()
.to_string();
if changed && new != comment {
name.borrow_mut().comment = if new.len() > 0 { Some(new) } else { None }
name.lock().borrow_mut().comment = if new.len() > 0 { Some(new) } else { None }
}
}
None => (),
@@ -53,3 +58,22 @@ impl LK {
}
}
}
impl PartialEq for LK {
fn eq(&self, other: &Self) -> bool {
if self.db.len() != other.db.len() || self.ls.len() != other.ls.len() || self.secrets != other.secrets {
return false;
}
for (k, v) in &self.db {
if !other.db.contains_key(k) || *other.db[k].lock() != *v.lock() {
return false;
}
}
for (k, v) in &self.ls {
if !other.ls.contains_key(k) || *other.ls[k].lock() != *v.lock() {
return false;
}
}
true
}
}