1
1
/*!
2
- * To support clients implementing the [Repository] trait in a [wasm_bindgen] environment,
3
- * we need to deal with an `extern "C"` interface, as that is what [wasm_bindgen] supports:
4
- *
5
- * This looks something like this:
6
- *
7
- * ```rust,ignore
8
- #[wasm_bindgen]
9
- extern "C" {
10
- pub type CipherRepository;
11
-
12
- #[wasm_bindgen(method, catch)]
13
- async fn get(this: &CipherRepository, id: String) -> Result<JsValue, JsValue>;
14
- }
15
- * ```
16
- *
17
- * As you can see, this has a few limitations:
18
- * - The type must be known at compile time, so we cannot use generics directly, which means we
19
- * can't use the existing [Repository] trait directly.
20
- * - The return type must be [JsValue], so we need to convert our types to and from [JsValue].
21
- *
22
- * To facilitate this, we provide some utilities:
23
- * - [WasmRepository] trait, which defines the methods as we expect them to come from
24
- * [wasm_bindgen], using [JsValue]. This is generic and should be implemented for each
25
- * concrete repository we define, but the implementation should be very straightforward.
26
- * - [WasmRepositoryChannel] struct, which wraps a [WasmRepository] in a [ThreadBoundRunner] and
27
- * implements the [Repository] trait. This has a few special considerations:
28
- * - It uses [tsify_next::serde_wasm_bindgen] to convert between [JsValue] and our types, so we can use
29
- * the existing [Repository] trait.
30
- * - It runs the calls in a thread-bound manner, so we can safely call the [WasmRepository]
31
- * methods from any thread.
32
- */
2
+ * To support clients implementing the [Repository] trait in a [::wasm_bindgen] environment,
3
+ * we need to deal with an `extern "C"` interface, as that is what [::wasm_bindgen] supports:
4
+ *
5
+ * This looks something like this:
6
+ *
7
+ * ```rust,ignore
8
+ * #[wasm_bindgen]
9
+ * extern "C" {
10
+ * pub type CipherRepository;
11
+ *
12
+ * #[wasm_bindgen(method, catch)]
13
+ * async fn get(this: &CipherRepository, id: String) -> Result<JsValue, JsValue>;
14
+ * }
15
+ * ```
16
+ *
17
+ * As you can see, this has a few limitations:
18
+ * - The type must be known at compile time, so we cannot use generics directly, which means we
19
+ * can't use the existing [Repository] trait directly.
20
+ * - The return type must be [JsValue], so we need to convert our types to and from [JsValue].
21
+ *
22
+ * To facilitate this, we provide some utilities:
23
+ * - [WasmRepository] trait, which defines the methods as we expect them to come from
24
+ * [::wasm_bindgen], using [JsValue]. This is generic and should be implemented for each
25
+ * concrete repository we define, but the implementation should be very straightforward.
26
+ * - [WasmRepositoryChannel] struct, which wraps a [WasmRepository] in a [ThreadBoundRunner] and
27
+ * implements the [Repository] trait. This has a few special considerations:
28
+ * - It uses [tsify_next::serde_wasm_bindgen] to convert between [JsValue] and our types, so
29
+ * we can use the existing [Repository] trait.
30
+ * - It runs the calls in a thread-bound manner, so we can safely call the [WasmRepository]
31
+ * methods from any thread.
32
+ * - The [create_wasm_repository] macro, defines the [::wasm_bindgen] interface and implements
33
+ * the [WasmRepository] trait for you.
34
+ */
33
35
34
36
use std:: { future:: Future , marker:: PhantomData , rc:: Rc } ;
35
37
@@ -38,9 +40,9 @@ use bitwarden_threading::ThreadBoundRunner;
38
40
use serde:: { de:: DeserializeOwned , Serialize } ;
39
41
use wasm_bindgen:: { prelude:: wasm_bindgen, JsValue } ;
40
42
41
- /// This trait defines the methods that a [wasm_bindgen] repository must implement.
42
- /// The trait itself exists to provide a generic way of handling the [wasm_bindgen] interface, which
43
- /// is !Send + !Sync, and only deals with [JsValue].
43
+ /// This trait defines the methods that a [:: wasm_bindgen] repository must implement.
44
+ /// The trait itself exists to provide a generic way of handling the [:: wasm_bindgen] interface,
45
+ /// which is !Send + !Sync, and only deals with [JsValue].
44
46
pub ( crate ) trait WasmRepository < T > {
45
47
async fn get ( & self , id : String ) -> Result < JsValue , JsValue > ;
46
48
async fn list ( & self ) -> Result < JsValue , JsValue > ;
@@ -90,7 +92,7 @@ export interface Repository<T> {
90
92
}
91
93
"# ;
92
94
93
- /// This macro generates a [wasm_bindgen] interface for a repository type, and provides the
95
+ /// This macro generates a [:: wasm_bindgen] interface for a repository type, and provides the
94
96
/// implementation of [WasmRepository] and a way to convert it into something that implements
95
97
/// the [Repository] trait.
96
98
macro_rules! create_wasm_repository {
0 commit comments