Skip to content

Commit 4e67ee4

Browse files
committed
Use internal mutability
1 parent 503e6eb commit 4e67ee4

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

crates/bitwarden-core/src/client/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Client {
9191
external_client,
9292
key_store: KeyStore::default(),
9393
#[cfg(feature = "internal")]
94-
repository_map: RwLock::new(StateRegistry::new()),
94+
repository_map: StateRegistry::new(),
9595
}),
9696
}
9797
}

crates/bitwarden-core/src/client/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub struct InternalClient {
6767
pub(super) key_store: KeyStore<KeyIds>,
6868

6969
#[cfg(feature = "internal")]
70-
pub(crate) repository_map: RwLock<StateRegistry>,
70+
pub(crate) repository_map: StateRegistry,
7171
}
7272

7373
impl InternalClient {

crates/bitwarden-core/src/platform/state_client.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,11 @@ impl StateClient {
2121
self.client
2222
.internal
2323
.repository_map
24-
.write()
25-
.expect("RwLock is not poisoned")
2624
.register_client_managed(store)
2725
}
2826

2927
/// Get a client managed state repository for a specific type, if it exists.
3028
pub fn get_client_managed<T: RepositoryItem>(&self) -> Option<Arc<dyn Repository<T>>> {
31-
self.client
32-
.internal
33-
.repository_map
34-
.read()
35-
.expect("RwLock is not poisoned")
36-
.get_client_managed()
29+
self.client.internal.repository_map.get_client_managed()
3730
}
3831
}

crates/bitwarden-state/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ log = { workspace = true }
2121
thiserror = { workspace = true }
2222

2323
[dev-dependencies]
24-
tokio = { workspace = true, features = ["rt"] }
24+
tokio = { workspace = true }
2525

2626
[lints]
2727
workspace = true

crates/bitwarden-state/src/registry.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
any::{Any, TypeId},
33
collections::HashMap,
4-
sync::Arc,
4+
sync::{Arc, RwLock},
55
};
66

77
use bitwarden_error::bitwarden_error;
@@ -13,14 +13,12 @@ use crate::repository::{Repository, RepositoryItem};
1313
/// A registry that contains repositories for different types of items.
1414
/// These repositories can be either managed by the client or by the SDK itself.
1515
pub struct StateRegistry {
16-
client_managed: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
16+
client_managed: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
1717
}
1818

1919
impl std::fmt::Debug for StateRegistry {
2020
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21-
f.debug_struct("StateRegistry")
22-
.field("client_managed", &self.client_managed.keys())
23-
.finish()
21+
f.debug_struct("StateRegistry").finish()
2422
}
2523
}
2624

@@ -40,13 +38,13 @@ impl StateRegistry {
4038
#[allow(clippy::new_without_default)]
4139
pub fn new() -> Self {
4240
StateRegistry {
43-
client_managed: HashMap::new(),
41+
client_managed: RwLock::new(HashMap::new()),
4442
}
4543
}
4644

4745
/// Registers a client-managed repository into the map, associating it with its type.
4846
pub fn register_client_managed<T: RepositoryItem>(
49-
&mut self,
47+
&self,
5048
value: Arc<dyn Repository<T>>,
5149
) -> Result<(), StateRegistryError> {
5250
let mut possible_registrations = RepositoryItemRegistration::iter();
@@ -63,6 +61,8 @@ impl StateRegistry {
6361
}
6462

6563
self.client_managed
64+
.write()
65+
.expect("RwLock should not be poisoned")
6666
.insert(TypeId::of::<T>(), Box::new(value));
6767

6868
Ok(())
@@ -71,6 +71,8 @@ impl StateRegistry {
7171
/// Retrieves a client-managed repository from the map given its type.
7272
pub fn get_client_managed<T: RepositoryItem>(&self) -> Option<Arc<dyn Repository<T>>> {
7373
self.client_managed
74+
.read()
75+
.expect("RwLock should not be poisoned")
7476
.get(&TypeId::of::<T>())
7577
.and_then(|boxed| boxed.downcast_ref::<Arc<dyn Repository<T>>>())
7678
.map(Arc::clone)
@@ -82,8 +84,13 @@ impl StateRegistry {
8284
let possible_registrations = RepositoryItemRegistration::iter();
8385
let mut missing_repository = false;
8486

87+
let client_managed = self
88+
.client_managed
89+
.read()
90+
.expect("RwLock should not be poisoned");
91+
8592
for reg in possible_registrations {
86-
if reg.rtype.is_client_managed() && !self.client_managed.contains_key(&reg.type_id()) {
93+
if reg.rtype.is_client_managed() && !client_managed.contains_key(&reg.type_id()) {
8794
log::error!(
8895
"Repository for type {} is not registered in the client-managed state registry",
8996
reg.name
@@ -151,7 +158,7 @@ mod tests {
151158
let b = Arc::new(TestB("test".to_string()));
152159
let c = Arc::new(TestC(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]));
153160

154-
let mut map = StateRegistry::new();
161+
let map = StateRegistry::new();
155162

156163
async fn get<T: RepositoryItem>(map: &StateRegistry) -> Option<T> {
157164
map.get_client_managed::<T>()

0 commit comments

Comments
 (0)