Skip to content

Commit

Permalink
feat: Replace Array by Vec<T> when possible.
Browse files Browse the repository at this point in the history
This patch replaces a few `js_sys::Array` by `std::vec::Vec` when
possible. It makes the code simpler, and `wasm_bindgen` now handles
`Vec` well for us.
  • Loading branch information
Hywan committed Jan 18, 2024
1 parent 6fb85bd commit 61ece69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 45 deletions.
19 changes: 4 additions & 15 deletions src/sync_events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! `GET /_matrix/client/*/sync`
use js_sys::Array;
use matrix_sdk_common::ruma;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -44,24 +43,14 @@ impl DeviceLists {
/// who now share an encrypted room with the client since the
/// previous sync
#[wasm_bindgen(getter)]
pub fn changed(&self) -> Array {
self.inner
.changed
.iter()
.map(|user| identifiers::UserId::from(user.clone()))
.map(JsValue::from)
.collect()
pub fn changed(&self) -> Vec<identifiers::UserId> {
self.inner.changed.iter().map(|user| identifiers::UserId::from(user.clone())).collect()
}

/// List of users who no longer share encrypted rooms since the
/// previous sync response.
#[wasm_bindgen(getter)]
pub fn left(&self) -> Array {
self.inner
.left
.iter()
.map(|user| identifiers::UserId::from(user.clone()))
.map(JsValue::from)
.collect()
pub fn left(&self) -> Vec<identifiers::UserId> {
self.inner.left.iter().map(|user| identifiers::UserId::from(user.clone())).collect()
}
}
40 changes: 10 additions & 30 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,8 @@ impl Sas {
///
/// Returns `undefined` if we can't yet present the short auth string,
/// otherwise an array of seven `Emoji` objects.
pub fn emoji(&self) -> Option<Array> {
Some(
self.inner
.emoji()?
.iter()
.map(|emoji| Emoji::from(emoji.to_owned()))
.map(JsValue::from)
.collect(),
)
pub fn emoji(&self) -> Option<Vec<Emoji>> {
Some(self.inner.emoji()?.iter().map(|emoji| Emoji::from(emoji.to_owned())).collect())
}

/// Get the index of the emoji representing the short auth string
Expand All @@ -349,24 +342,19 @@ impl Sas {
/// relevant specification
/// entry](https://spec.matrix.org/unstable/client-server-api/#sas-method-emoji).
#[wasm_bindgen(js_name = "emojiIndex")]
pub fn emoji_index(&self) -> Option<Array> {
Some(self.inner.emoji_index()?.into_iter().map(JsValue::from).collect())
pub fn emoji_index(&self) -> Option<Vec<u8>> {
Some(self.inner.emoji_index()?.into())
}

/// Get the decimal version of the short auth string.
///
/// Returns None if we can’t yet present the short auth string,
/// otherwise a tuple containing three 4-digit integers that
/// represent the short auth string.
pub fn decimals(&self) -> Option<Array> {
pub fn decimals(&self) -> Option<Vec<u16>> {
let decimals = self.inner.decimals()?;

let out = Array::new_with_length(3);
out.set(0, JsValue::from(decimals.0));
out.set(1, JsValue::from(decimals.1));
out.set(2, JsValue::from(decimals.2));

Some(out)
Some(vec![decimals.0, decimals.1, decimals.2])
}

/// Register a callback which will be called whenever there is an update to
Expand Down Expand Up @@ -874,17 +862,12 @@ impl VerificationRequest {
///
/// Will be present only if the other side requested the
/// verification or if we’re in the ready state.
///
/// It return a `Option<Vec<VerificationMethod>>`.
#[wasm_bindgen(getter, js_name = "theirSupportedMethods")]
pub fn their_supported_methods(&self) -> Result<Option<Array>, JsError> {
pub fn their_supported_methods(&self) -> Result<Option<Vec<VerificationMethod>>, JsError> {
self.inner
.their_supported_methods()
.map(|methods| {
methods
.into_iter()
.map(|method| VerificationMethod::try_from(method).map(JsValue::from))
.collect::<Result<Array, _>>()
methods.into_iter().map(|method| VerificationMethod::try_from(method)).collect()
})
.transpose()
}
Expand All @@ -894,14 +877,11 @@ impl VerificationRequest {
/// Will be present only we requested the verification or if we’re
/// in the ready state.
#[wasm_bindgen(getter, js_name = "ourSupportedMethods")]
pub fn our_supported_methods(&self) -> Result<Option<Array>, JsError> {
pub fn our_supported_methods(&self) -> Result<Option<Vec<VerificationMethod>>, JsError> {
self.inner
.our_supported_methods()
.map(|methods| {
methods
.into_iter()
.map(|method| VerificationMethod::try_from(method).map(JsValue::from))
.collect::<Result<Array, _>>()
methods.into_iter().map(|method| VerificationMethod::try_from(method)).collect()
})
.transpose()
}
Expand Down

0 comments on commit 61ece69

Please sign in to comment.