Skip to content

Commit 58b8a25

Browse files
zzorbaDaniel SalinasDaniel Salinas
authored
feat(wasm): Comprehensive Send+Sync bound improvements for Wasm compatibility (#5082)
This commit systematically replaces Send+Sync trait bounds throughout the matrix-rust-sdk codebase to enable Wasm compatibility while maintaining thread safety on native targets. Key changes: - Use SendOutsideWasm/SyncOutsideWasm traits instead of Send/Sync for trait bounds - Apply conditional compilation for error types and trait objects - Update FFI trait definitions to use Wasm-compatible bounds - Fix event handler and type alias definitions for cross-platform compatibility - Maintain existing functionality while enabling WebAssembly target support The SendOutsideWasm/SyncOutsideWasm traits are empty on Wasm (allowing all types) and alias to Send/Sync on native targets, ensuring zero-cost abstraction. Files updated: - All FFI bindings (30+ trait definitions) - Core SDK error types and type aliases - Event handler infrastructure - Store and crypto abstractions - UI service filters and sorters - Timeline and authentication modules <!-- description of the changes in this PR --> - [ ] Public API changes documented in changelogs (optional) <!-- Sign-off, if not part of the commits --> <!-- See CONTRIBUTING.md if you don't know what this is --> Signed-off-by: Daniel Salinas --------- Signed-off-by: Daniel Salinas <zzorba@users.noreply.github.com> Co-authored-by: Daniel Salinas <danielsalinas@Daniels-MacBook-Pro-2.local> Co-authored-by: Daniel Salinas <danielsalinas@daniels-mbp-2.myfiosgateway.com>
1 parent edb7a0f commit 58b8a25

File tree

31 files changed

+186
-51
lines changed

31 files changed

+186
-51
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use matrix_sdk::{
4141
AuthApi, AuthSession, Client as MatrixClient, SessionChange, SessionTokens,
4242
STATE_STORE_DATABASE_NAME,
4343
};
44-
use matrix_sdk_common::stream::StreamExt;
44+
use matrix_sdk_common::{stream::StreamExt, SendOutsideWasm, SyncOutsideWasm};
4545
use matrix_sdk_ui::{
4646
notification_client::{
4747
NotificationClient as MatrixNotificationClient,
@@ -167,39 +167,39 @@ impl From<PushFormat> for RumaPushFormat {
167167
}
168168

169169
#[matrix_sdk_ffi_macros::export(callback_interface)]
170-
pub trait ClientDelegate: Sync + Send {
170+
pub trait ClientDelegate: SyncOutsideWasm + SendOutsideWasm {
171171
fn did_receive_auth_error(&self, is_soft_logout: bool);
172172
}
173173

174174
#[matrix_sdk_ffi_macros::export(callback_interface)]
175-
pub trait ClientSessionDelegate: Sync + Send {
175+
pub trait ClientSessionDelegate: SyncOutsideWasm + SendOutsideWasm {
176176
fn retrieve_session_from_keychain(&self, user_id: String) -> Result<Session, ClientError>;
177177
fn save_session_in_keychain(&self, session: Session);
178178
}
179179

180180
#[matrix_sdk_ffi_macros::export(callback_interface)]
181-
pub trait ProgressWatcher: Send + Sync {
181+
pub trait ProgressWatcher: SyncOutsideWasm + SendOutsideWasm {
182182
fn transmission_progress(&self, progress: TransmissionProgress);
183183
}
184184

185185
/// A listener to the global (client-wide) error reporter of the send queue.
186186
#[matrix_sdk_ffi_macros::export(callback_interface)]
187-
pub trait SendQueueRoomErrorListener: Sync + Send {
187+
pub trait SendQueueRoomErrorListener: SyncOutsideWasm + SendOutsideWasm {
188188
/// Called every time the send queue has ran into an error for a given room,
189189
/// which will disable the send queue for that particular room.
190190
fn on_error(&self, room_id: String, error: ClientError);
191191
}
192192

193193
/// A listener for changes of global account data events.
194194
#[matrix_sdk_ffi_macros::export(callback_interface)]
195-
pub trait AccountDataListener: Sync + Send {
195+
pub trait AccountDataListener: SyncOutsideWasm + SendOutsideWasm {
196196
/// Called when a global account data event has changed.
197197
fn on_change(&self, event: AccountDataEvent);
198198
}
199199

200200
/// A listener for changes of room account data events.
201201
#[matrix_sdk_ffi_macros::export(callback_interface)]
202-
pub trait RoomAccountDataListener: Sync + Send {
202+
pub trait RoomAccountDataListener: SyncOutsideWasm + SendOutsideWasm {
203203
/// Called when a room account data event was changed.
204204
fn on_change(&self, event: RoomAccountDataEvent, room_id: String);
205205
}
@@ -1563,12 +1563,12 @@ impl Client {
15631563
}
15641564

15651565
#[matrix_sdk_ffi_macros::export(callback_interface)]
1566-
pub trait MediaPreviewConfigListener: Sync + Send {
1566+
pub trait MediaPreviewConfigListener: SyncOutsideWasm + SendOutsideWasm {
15671567
fn on_change(&self, media_preview_config: Option<MediaPreviewConfig>);
15681568
}
15691569

15701570
#[matrix_sdk_ffi_macros::export(callback_interface)]
1571-
pub trait IgnoredUsersListener: Sync + Send {
1571+
pub trait IgnoredUsersListener: SyncOutsideWasm + SendOutsideWasm {
15721572
fn call(&self, ignored_user_ids: Vec<String>);
15731573
}
15741574

bindings/matrix-sdk-ffi/src/client_builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use matrix_sdk::{
1919
Client as MatrixClient, ClientBuildError as MatrixClientBuildError, HttpError, IdParseError,
2020
RumaApiError, SqliteStoreConfig,
2121
};
22+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
2223
use ruma::api::error::{DeserializationError, FromHttpResponseError};
2324
use tracing::{debug, error};
2425
use zeroize::Zeroizing;
@@ -173,7 +174,7 @@ pub enum QrLoginProgress {
173174
}
174175

175176
#[matrix_sdk_ffi_macros::export(callback_interface)]
176-
pub trait QrLoginProgressListener: Sync + Send {
177+
pub trait QrLoginProgressListener: SyncOutsideWasm + SendOutsideWasm {
177178
fn on_update(&self, state: QrLoginProgress);
178179
}
179180

bindings/matrix-sdk-ffi/src/encryption.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use matrix_sdk::{
66
encryption,
77
encryption::{backups, recovery},
88
};
9+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
910
use thiserror::Error;
1011
use tracing::{error, info};
1112
use zeroize::Zeroize;
@@ -25,22 +26,22 @@ pub struct Encryption {
2526
}
2627

2728
#[matrix_sdk_ffi_macros::export(callback_interface)]
28-
pub trait BackupStateListener: Sync + Send {
29+
pub trait BackupStateListener: SyncOutsideWasm + SendOutsideWasm {
2930
fn on_update(&self, status: BackupState);
3031
}
3132

3233
#[matrix_sdk_ffi_macros::export(callback_interface)]
33-
pub trait BackupSteadyStateListener: Sync + Send {
34+
pub trait BackupSteadyStateListener: SyncOutsideWasm + SendOutsideWasm {
3435
fn on_update(&self, status: BackupUploadState);
3536
}
3637

3738
#[matrix_sdk_ffi_macros::export(callback_interface)]
38-
pub trait RecoveryStateListener: Sync + Send {
39+
pub trait RecoveryStateListener: SyncOutsideWasm + SendOutsideWasm {
3940
fn on_update(&self, status: RecoveryState);
4041
}
4142

4243
#[matrix_sdk_ffi_macros::export(callback_interface)]
43-
pub trait VerificationStateListener: Sync + Send {
44+
pub trait VerificationStateListener: SyncOutsideWasm + SendOutsideWasm {
4445
fn on_update(&self, status: VerificationState);
4546
}
4647

@@ -164,7 +165,7 @@ impl From<recovery::RecoveryState> for RecoveryState {
164165
}
165166

166167
#[matrix_sdk_ffi_macros::export(callback_interface)]
167-
pub trait EnableRecoveryProgressListener: Sync + Send {
168+
pub trait EnableRecoveryProgressListener: SyncOutsideWasm + SendOutsideWasm {
168169
fn on_update(&self, status: EnableRecoveryProgress);
169170
}
170171

bindings/matrix-sdk-ffi/src/notification_settings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use matrix_sdk::{
99
ruma::events::push_rules::PushRulesEvent,
1010
Client as MatrixClient,
1111
};
12+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
1213
use ruma::{
1314
push::{
1415
Action as SdkAction, ComparisonOperator as SdkComparisonOperator, PredefinedOverrideRuleId,
@@ -387,7 +388,7 @@ impl From<RoomNotificationMode> for SdkRoomNotificationMode {
387388

388389
/// Delegate to notify of changes in push rules
389390
#[matrix_sdk_ffi_macros::export(callback_interface)]
390-
pub trait NotificationSettingsDelegate: Sync + Send {
391+
pub trait NotificationSettingsDelegate: SyncOutsideWasm + SendOutsideWasm {
391392
fn settings_did_change(&self);
392393
}
393394

bindings/matrix-sdk-ffi/src/room.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use matrix_sdk::{
1313
PredecessorRoom as SdkPredecessorRoom, RoomHero as SdkRoomHero, RoomMemberships, RoomState,
1414
SuccessorRoom as SdkSuccessorRoom,
1515
};
16+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
1617
use matrix_sdk_ui::{
1718
timeline::{default_event_filter, RoomExt, TimelineBuilder},
1819
unable_to_decrypt_hook::UtdHookManager,
@@ -1173,7 +1174,7 @@ impl Room {
11731174

11741175
/// A listener for receiving new live location shares in a room.
11751176
#[matrix_sdk_ffi_macros::export(callback_interface)]
1176-
pub trait LiveLocationShareListener: Sync + Send {
1177+
pub trait LiveLocationShareListener: SyncOutsideWasm + SendOutsideWasm {
11771178
fn call(&self, live_location_shares: Vec<LiveLocationShare>);
11781179
}
11791180

@@ -1195,7 +1196,7 @@ impl From<matrix_sdk::room::knock_requests::KnockRequest> for KnockRequest {
11951196

11961197
/// A listener for receiving new requests to a join a room.
11971198
#[matrix_sdk_ffi_macros::export(callback_interface)]
1198-
pub trait KnockRequestsListener: Send + Sync {
1199+
pub trait KnockRequestsListener: SendOutsideWasm + SyncOutsideWasm {
11991200
fn call(&self, join_requests: Vec<KnockRequest>);
12001201
}
12011202

@@ -1315,17 +1316,17 @@ impl From<RumaPowerLevels> for RoomPowerLevels {
13151316
}
13161317

13171318
#[matrix_sdk_ffi_macros::export(callback_interface)]
1318-
pub trait RoomInfoListener: Sync + Send {
1319+
pub trait RoomInfoListener: SyncOutsideWasm + SendOutsideWasm {
13191320
fn call(&self, room_info: RoomInfo);
13201321
}
13211322

13221323
#[matrix_sdk_ffi_macros::export(callback_interface)]
1323-
pub trait TypingNotificationsListener: Sync + Send {
1324+
pub trait TypingNotificationsListener: SyncOutsideWasm + SendOutsideWasm {
13241325
fn call(&self, typing_user_ids: Vec<String>);
13251326
}
13261327

13271328
#[matrix_sdk_ffi_macros::export(callback_interface)]
1328-
pub trait IdentityStatusChangeListener: Sync + Send {
1329+
pub trait IdentityStatusChangeListener: SyncOutsideWasm + SendOutsideWasm {
13291330
fn call(&self, identity_status_change: Vec<IdentityStatusChange>);
13301331
}
13311332

bindings/matrix-sdk-ffi/src/room_directory_search.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use async_compat::get_runtime_handle;
1919
use eyeball_im::VectorDiff;
2020
use futures_util::StreamExt;
2121
use matrix_sdk::room_directory_search::RoomDirectorySearch as SdkRoomDirectorySearch;
22+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
2223
use ruma::ServerName;
2324
use tokio::sync::RwLock;
2425

@@ -198,6 +199,6 @@ impl From<VectorDiff<matrix_sdk::room_directory_search::RoomDescription>>
198199
}
199200

200201
#[matrix_sdk_ffi_macros::export(callback_interface)]
201-
pub trait RoomDirectorySearchEntriesListener: Send + Sync + Debug {
202+
pub trait RoomDirectorySearchEntriesListener: SendOutsideWasm + SyncOutsideWasm + Debug {
202203
fn on_update(&self, room_entries_update: Vec<RoomDirectorySearchEntryUpdate>);
203204
}

bindings/matrix-sdk-ffi/src/room_list.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use matrix_sdk::{
1212
},
1313
Room as SdkRoom,
1414
};
15+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
1516
use matrix_sdk_ui::{
1617
room_list_service::filters::{
1718
new_filter_all, new_filter_any, new_filter_category, new_filter_deduplicate_versions,
@@ -342,17 +343,17 @@ impl From<matrix_sdk_ui::room_list_service::RoomListLoadingState> for RoomListLo
342343
}
343344

344345
#[matrix_sdk_ffi_macros::export(callback_interface)]
345-
pub trait RoomListServiceStateListener: Send + Sync + Debug {
346+
pub trait RoomListServiceStateListener: SendOutsideWasm + SyncOutsideWasm + Debug {
346347
fn on_update(&self, state: RoomListServiceState);
347348
}
348349

349350
#[matrix_sdk_ffi_macros::export(callback_interface)]
350-
pub trait RoomListLoadingStateListener: Send + Sync + Debug {
351+
pub trait RoomListLoadingStateListener: SendOutsideWasm + SyncOutsideWasm + Debug {
351352
fn on_update(&self, state: RoomListLoadingState);
352353
}
353354

354355
#[matrix_sdk_ffi_macros::export(callback_interface)]
355-
pub trait RoomListServiceSyncIndicatorListener: Send + Sync + Debug {
356+
pub trait RoomListServiceSyncIndicatorListener: SendOutsideWasm + SyncOutsideWasm + Debug {
356357
fn on_update(&self, sync_indicator: RoomListServiceSyncIndicator);
357358
}
358359

@@ -412,7 +413,7 @@ impl RoomListEntriesUpdate {
412413
}
413414

414415
#[matrix_sdk_ffi_macros::export(callback_interface)]
415-
pub trait RoomListEntriesListener: Send + Sync + Debug {
416+
pub trait RoomListEntriesListener: SendOutsideWasm + SyncOutsideWasm + Debug {
416417
fn on_update(&self, room_entries_update: Vec<RoomListEntriesUpdate>);
417418
}
418419

bindings/matrix-sdk-ffi/src/session_verification.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use matrix_sdk::{
1111
ruma::events::key::verification::VerificationMethod,
1212
Account,
1313
};
14+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
1415
use ruma::UserId;
1516
use tracing::{error, warn};
1617

@@ -51,7 +52,7 @@ pub struct SessionVerificationRequestDetails {
5152
}
5253

5354
#[matrix_sdk_ffi_macros::export(callback_interface)]
54-
pub trait SessionVerificationControllerDelegate: Sync + Send {
55+
pub trait SessionVerificationControllerDelegate: SyncOutsideWasm + SendOutsideWasm {
5556
fn did_receive_verification_request(&self, details: SessionVerificationRequestDetails);
5657
fn did_accept_verification_request(&self);
5758
fn did_start_sas_verification(&self);

bindings/matrix-sdk-ffi/src/sync_service.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{fmt::Debug, sync::Arc};
1717
use async_compat::get_runtime_handle;
1818
use futures_util::pin_mut;
1919
use matrix_sdk::Client;
20+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
2021
use matrix_sdk_ui::{
2122
sync_service::{
2223
State as MatrixSyncServiceState, SyncService as MatrixSyncService,
@@ -51,7 +52,7 @@ impl From<MatrixSyncServiceState> for SyncServiceState {
5152
}
5253

5354
#[matrix_sdk_ffi_macros::export(callback_interface)]
54-
pub trait SyncServiceStateObserver: Send + Sync + Debug {
55+
pub trait SyncServiceStateObserver: SendOutsideWasm + SyncOutsideWasm + Debug {
5556
fn on_update(&self, state: SyncServiceState);
5657
}
5758

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ mod msg_like;
8585
mod reply;
8686

8787
use matrix_sdk::utils::formatted_body_from;
88+
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
8889

8990
use crate::error::QueueWedgeError;
9091

@@ -807,12 +808,12 @@ pub enum FocusEventError {
807808
}
808809

809810
#[matrix_sdk_ffi_macros::export(callback_interface)]
810-
pub trait TimelineListener: Sync + Send {
811+
pub trait TimelineListener: SyncOutsideWasm + SendOutsideWasm {
811812
fn on_update(&self, diff: Vec<Arc<TimelineDiff>>);
812813
}
813814

814815
#[matrix_sdk_ffi_macros::export(callback_interface)]
815-
pub trait PaginationStatusListener: Sync + Send {
816+
pub trait PaginationStatusListener: SyncOutsideWasm + SendOutsideWasm {
816817
fn on_update(&self, status: RoomPaginationStatus);
817818
}
818819

0 commit comments

Comments
 (0)