Skip to content

Commit 5903fed

Browse files
committed
Merge branch 'ps/state-traits' into ps/state-impl
2 parents a776549 + 7d14443 commit 5903fed

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

crates/bitwarden-wasm-internal/src/platform/repository.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
/*!
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+
*/
3335

3436
use std::{future::Future, marker::PhantomData, rc::Rc};
3537

@@ -38,9 +40,9 @@ use bitwarden_threading::ThreadBoundRunner;
3840
use serde::{de::DeserializeOwned, Serialize};
3941
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
4042

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].
4446
pub(crate) trait WasmRepository<T> {
4547
async fn get(&self, id: String) -> Result<JsValue, JsValue>;
4648
async fn list(&self) -> Result<JsValue, JsValue>;
@@ -90,7 +92,7 @@ export interface Repository<T> {
9092
}
9193
"#;
9294

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
9496
/// implementation of [WasmRepository] and a way to convert it into something that implements
9597
/// the [Repository] trait.
9698
macro_rules! create_wasm_repository {

0 commit comments

Comments
 (0)