Skip to content

Commit cfd071a

Browse files
committed
Remove inventory registration, doesn't work on wasm :sadface:
1 parent 4e67ee4 commit cfd071a

File tree

12 files changed

+16
-231
lines changed

12 files changed

+16
-231
lines changed

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::sync::Arc;
22

3-
use bitwarden_state::{
4-
registry::StateRegistryError,
5-
repository::{Repository, RepositoryItem},
6-
};
3+
use bitwarden_state::repository::{Repository, RepositoryItem};
74

85
use crate::Client;
96

@@ -17,7 +14,7 @@ impl StateClient {
1714
pub fn register_client_managed<T: 'static + Repository<V>, V: RepositoryItem>(
1815
&self,
1916
store: Arc<T>,
20-
) -> Result<(), StateRegistryError> {
17+
) {
2118
self.client
2219
.internal
2320
.repository_map

crates/bitwarden-state/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ wasm = []
1616
[dependencies]
1717
async-trait = { workspace = true }
1818
bitwarden-error = { workspace = true }
19-
inventory = ">=0.3.20, <0.4"
2019
log = { workspace = true }
2120
thiserror = { workspace = true }
2221

crates/bitwarden-state/src/registry.rs

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ use std::{
44
sync::{Arc, RwLock},
55
};
66

7-
use bitwarden_error::bitwarden_error;
8-
use thiserror::Error;
9-
10-
use super::repository::RepositoryItemRegistration;
117
use crate::repository::{Repository, RepositoryItem};
128

139
/// A registry that contains repositories for different types of items.
@@ -22,17 +18,6 @@ impl std::fmt::Debug for StateRegistry {
2218
}
2319
}
2420

25-
#[allow(missing_docs)]
26-
#[bitwarden_error(flat)]
27-
#[derive(Debug, Error)]
28-
pub enum StateRegistryError {
29-
#[error("Repository for type {0} is not registered as client-managed")]
30-
RepositoryNotClientManaged(&'static str),
31-
32-
#[error("Not all client-managed repositories are registered")]
33-
NotAllRepositoriesRegistered,
34-
}
35-
3621
impl StateRegistry {
3722
/// Creates a new empty `StateRegistry`.
3823
#[allow(clippy::new_without_default)]
@@ -43,29 +28,11 @@ impl StateRegistry {
4328
}
4429

4530
/// Registers a client-managed repository into the map, associating it with its type.
46-
pub fn register_client_managed<T: RepositoryItem>(
47-
&self,
48-
value: Arc<dyn Repository<T>>,
49-
) -> Result<(), StateRegistryError> {
50-
let mut possible_registrations = RepositoryItemRegistration::iter();
51-
match possible_registrations.find(|reg| reg.type_id() == TypeId::of::<T>()) {
52-
Some(reg) => {
53-
if !reg.rtype.is_client_managed() {
54-
return Err(StateRegistryError::RepositoryNotClientManaged(reg.name));
55-
}
56-
}
57-
// This should never happen, as we have tests to ensure all repositories are registered.
58-
_ => {
59-
return Err(StateRegistryError::NotAllRepositoriesRegistered);
60-
}
61-
}
62-
31+
pub fn register_client_managed<T: RepositoryItem>(&self, value: Arc<dyn Repository<T>>) {
6332
self.client_managed
6433
.write()
6534
.expect("RwLock should not be poisoned")
6635
.insert(TypeId::of::<T>(), Box::new(value));
67-
68-
Ok(())
6936
}
7037

7138
/// Retrieves a client-managed repository from the map given its type.
@@ -77,34 +44,6 @@ impl StateRegistry {
7744
.and_then(|boxed| boxed.downcast_ref::<Arc<dyn Repository<T>>>())
7845
.map(Arc::clone)
7946
}
80-
81-
/// Validates that all repositories registered in the client-managed state registry.
82-
/// This should only be called after all the repositories have been registered by the clients.
83-
pub fn validate_repositories(&self) -> Result<(), StateRegistryError> {
84-
let possible_registrations = RepositoryItemRegistration::iter();
85-
let mut missing_repository = false;
86-
87-
let client_managed = self
88-
.client_managed
89-
.read()
90-
.expect("RwLock should not be poisoned");
91-
92-
for reg in possible_registrations {
93-
if reg.rtype.is_client_managed() && !client_managed.contains_key(&reg.type_id()) {
94-
log::error!(
95-
"Repository for type {} is not registered in the client-managed state registry",
96-
reg.name
97-
);
98-
missing_repository = true;
99-
}
100-
}
101-
102-
if missing_repository {
103-
return Err(StateRegistryError::NotAllRepositoriesRegistered);
104-
}
105-
106-
Ok(())
107-
}
10847
}
10948

11049
#[cfg(test)]
@@ -144,9 +83,9 @@ mod tests {
14483
#[derive(PartialEq, Eq, Debug)]
14584
struct TestItem<T>(T);
14685

147-
register_repository_item!(TestItem<usize>, "TestItem<usize>", ClientManaged);
148-
register_repository_item!(TestItem<String>, "TestItem<String>", ClientManaged);
149-
register_repository_item!(TestItem<Vec<u8>>, "TestItem<Vec<u8>>", ClientManaged);
86+
register_repository_item!(TestItem<usize>, "TestItem<usize>");
87+
register_repository_item!(TestItem<String>, "TestItem<String>");
88+
register_repository_item!(TestItem<Vec<u8>>, "TestItem<Vec<u8>>");
15089

15190
impl_repository!(TestA, TestItem<usize>);
15291
impl_repository!(TestB, TestItem<String>);
@@ -172,17 +111,17 @@ mod tests {
172111
assert!(map.get_client_managed::<TestItem<String>>().is_none());
173112
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_none());
174113

175-
map.register_client_managed(a.clone()).unwrap();
114+
map.register_client_managed(a.clone());
176115
assert_eq!(get(&map).await, Some(TestItem(a.0)));
177116
assert!(map.get_client_managed::<TestItem<String>>().is_none());
178117
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_none());
179118

180-
map.register_client_managed(b.clone()).unwrap();
119+
map.register_client_managed(b.clone());
181120
assert_eq!(get(&map).await, Some(TestItem(a.0)));
182121
assert_eq!(get(&map).await, Some(TestItem(b.0.clone())));
183122
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_none());
184123

185-
map.register_client_managed(c.clone()).unwrap();
124+
map.register_client_managed(c.clone());
186125
assert_eq!(get(&map).await, Some(TestItem(a.0)));
187126
assert_eq!(get(&map).await, Some(TestItem(b.0.clone())));
188127
assert_eq!(get(&map).await, Some(TestItem(c.0.clone())));

crates/bitwarden-state/src/repository.rs

Lines changed: 4 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -35,128 +35,25 @@ pub trait RepositoryItem: Internal + Send + Sync + 'static {
3535
}
3636

3737
/// Register a type for use in a repository. The type must only be registered once in the crate
38-
/// where it's defined. The provided name must be unique and not be changed. Through the use of the
39-
/// `inventory` crate, this macro will register the type globally and
40-
/// [test_utils::verify_no_duplicate_registrations] will ensure that no duplicate registrations
41-
/// exist.
38+
/// where it's defined. The provided name must be unique and not be changed.
4239
#[macro_export]
4340
macro_rules! register_repository_item {
44-
($ty:ty, $name:literal, $type:expr) => {
41+
($ty:ty, $name:literal) => {
4542
const _: () = {
4643
impl $crate::repository::___internal::Internal for $ty {}
4744
impl $crate::repository::RepositoryItem for $ty {
4845
const NAME: &'static str = $name;
4946
}
50-
use $crate::repository::___internal::RepositoryItemRegistrationType::*;
51-
$crate::repository::___internal::submit! {
52-
$crate::repository::___internal::RepositoryItemRegistration::new::<$ty>($name, $type)
53-
}
5447
};
5548
};
5649
}
5750

58-
/// This code is used to register types that can be stored in a repository.
59-
/// It's not meant to be used directly, users of this crate should use the
51+
/// This code is not meant to be used directly, users of this crate should use the
6052
/// [register_repository_item] macro to register their types.
6153
#[doc(hidden)]
6254
pub mod ___internal {
63-
use super::*;
6455

6556
// This trait is just to try to discourage users from implementing `RepositoryItem` directly.
6657
pub trait Internal {}
67-
68-
/// This enum indicated what kind of repository registration is being performed.
69-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70-
pub enum RepositoryItemRegistrationType {
71-
ClientManaged,
72-
SdkManaged,
73-
Both,
74-
}
75-
76-
impl RepositoryItemRegistrationType {
77-
pub fn is_client_managed(&self) -> bool {
78-
matches!(self, Self::ClientManaged | Self::Both)
79-
}
80-
pub fn is_sdk_managed(&self) -> bool {
81-
matches!(self, Self::SdkManaged | Self::Both)
82-
}
83-
}
84-
85-
#[derive(Debug)]
86-
pub struct RepositoryItemRegistration {
87-
pub(crate) name: &'static str,
88-
/// The type identifier of the repository item. We're using a function pointer because
89-
/// [TypeId::of] is not const stable.
90-
pub(crate) type_id: fn() -> TypeId,
91-
pub(crate) rtype: RepositoryItemRegistrationType,
92-
}
93-
94-
impl RepositoryItemRegistration {
95-
/// Creates a new `RepositoryRegistration` for the given type.
96-
pub const fn new<T: RepositoryItem>(
97-
name: &'static str,
98-
rtype: RepositoryItemRegistrationType,
99-
) -> Self {
100-
Self {
101-
name,
102-
type_id: || T::type_id(),
103-
rtype,
104-
}
105-
}
106-
107-
pub(crate) fn iter() -> impl Iterator<Item = &'static RepositoryItemRegistration> {
108-
inventory::iter::<RepositoryItemRegistration>()
109-
}
110-
}
111-
112-
inventory::collect!(RepositoryItemRegistration);
113-
pub use inventory::submit;
114-
}
115-
pub(crate) use ___internal::{Internal, RepositoryItemRegistration};
116-
117-
#[cfg(test)]
118-
mod tests {
119-
use super::*;
120-
121-
struct TestItem;
122-
123-
register_repository_item!(TestItem, "TestItem", SdkManaged);
124-
125-
#[test]
126-
fn test_repository_item_registration() {
127-
let registered: Vec<_> = RepositoryItemRegistration::iter().collect();
128-
// We can't really test the exact number, as they might be registered in different crates.
129-
assert!(!registered.is_empty(), "No repository items registered");
130-
}
131-
132-
#[test]
133-
pub fn verify_no_duplicate_registrations() {
134-
crate::repository::test_utils::verify_no_duplicate_registrations();
135-
}
136-
}
137-
138-
#[doc(hidden)]
139-
pub mod test_utils {
140-
/// Verify that no duplicate repository item registrations exist.
141-
/// This needs to be called in the final SDK crates (WASM, UniFFI, CLI, ...) to ensure that all
142-
/// registrations are checked.
143-
pub fn verify_no_duplicate_registrations() {
144-
use crate::repository::___internal::RepositoryItemRegistration;
145-
146-
let mut seen_names = std::collections::HashSet::new();
147-
let mut seen_ids = std::collections::HashSet::new();
148-
149-
for reg in RepositoryItemRegistration::iter() {
150-
assert!(
151-
seen_names.insert(reg.name),
152-
"Duplicate repository registration name: {}",
153-
reg.name
154-
);
155-
assert!(
156-
seen_ids.insert((reg.type_id)()),
157-
"Duplicate repository registration id: {}",
158-
reg.name
159-
);
160-
}
161-
}
16258
}
59+
pub(crate) use ___internal::Internal;

crates/bitwarden-uniffi/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ pub enum Error {
6464
#[error(transparent)]
6565
Crypto(#[from] bitwarden_crypto::CryptoError),
6666

67-
#[error(transparent)]
68-
StateRegistry(#[from] bitwarden_state::registry::StateRegistryError),
69-
7067
// Generators
7168
#[error(transparent)]
7269
Username(#[from] UsernameError),

crates/bitwarden-uniffi/src/platform/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,11 @@ repository::create_uniffi_repository!(CipherRepository, Cipher);
5656

5757
#[uniffi::export]
5858
impl StateClient {
59-
pub fn register_cipher_repository(&self, store: Arc<dyn CipherRepository>) -> Result<()> {
59+
pub fn register_cipher_repository(&self, store: Arc<dyn CipherRepository>) {
6060
let store_internal = UniffiRepositoryBridge::new(store);
6161
self.0
6262
.platform()
6363
.state()
6464
.register_client_managed(store_internal)
65-
.map_err(Error::StateRegistry)?;
66-
Ok(())
6765
}
6866
}

crates/bitwarden-uniffi/src/platform/repository.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,3 @@ macro_rules! create_uniffi_repository {
100100
};
101101
}
102102
pub(super) use create_uniffi_repository;
103-
104-
#[cfg(test)]
105-
mod tests {
106-
#[test]
107-
pub fn verify_no_duplicate_registrations() {
108-
bitwarden_state::repository::test_utils::verify_no_duplicate_registrations();
109-
}
110-
}

crates/bitwarden-vault/src/cipher/cipher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub struct Cipher {
121121
pub revision_date: DateTime<Utc>,
122122
}
123123

124-
bitwarden_state::register_repository_item!(Cipher, "Cipher", ClientManaged);
124+
bitwarden_state::register_repository_item!(Cipher, "Cipher");
125125

126126
#[allow(missing_docs)]
127127
#[derive(Serialize, Deserialize, Debug, Clone)]

crates/bitwarden-wasm-internal/src/init.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ pub fn set_log_level(level: LogLevel) {
2626
set_max_level(log_level.to_level_filter());
2727
}
2828

29-
// This is needed to ensure the linker doesn't optimize away the constructors used by the inventory
30-
// crate. https://docs.rs/inventory/0.3.20/inventory/index.html#webassembly-and-constructors
31-
unsafe extern "C" {
32-
fn __wasm_call_ctors();
33-
}
34-
35-
#[wasm_bindgen(start)]
36-
fn start() {
37-
unsafe {
38-
__wasm_call_ctors();
39-
}
40-
}
41-
4229
#[allow(missing_docs)]
4330
#[wasm_bindgen]
4431
pub fn init_sdk(log_level: Option<LogLevel>) {

0 commit comments

Comments
 (0)