Do wasm sepcific changes:

- Only use send+sync when not using wasm
- Use wasm capabale async_trait wrapper macro
- Make room and room_member specific structs always clonable
master
Marcel 2020-06-17 18:52:53 +02:00
parent ea427cf366
commit 8b77b4171a
11 changed files with 33 additions and 20 deletions

View File

@ -6,4 +6,5 @@ members = [
"matrix_sdk_test_macros", "matrix_sdk_test_macros",
"matrix_sdk_crypto", "matrix_sdk_crypto",
"matrix_sdk_common", "matrix_sdk_common",
"matrix_sdk_common_macros",
] ]

View File

@ -5,6 +5,7 @@ use matrix_sdk::{
events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings, Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings,
}; };
use matrix_sdk_common_macros::async_trait;
use url::Url; use url::Url;
struct CommandBot { struct CommandBot {
@ -19,7 +20,7 @@ impl CommandBot {
} }
} }
#[async_trait::async_trait] #[async_trait]
impl EventEmitter for CommandBot { impl EventEmitter for CommandBot {
async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) { async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) {
if let SyncRoom::Joined(room) = room { if let SyncRoom::Joined(room) = room {

View File

@ -6,10 +6,11 @@ use matrix_sdk::{
events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
Client, ClientConfig, EventEmitter, SyncRoom, SyncSettings, Client, ClientConfig, EventEmitter, SyncRoom, SyncSettings,
}; };
use matrix_sdk_common_macros::async_trait;
struct EventCallback; struct EventCallback;
#[async_trait::async_trait] #[async_trait]
impl EventEmitter for EventCallback { impl EventEmitter for EventCallback {
async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) { async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) {
if let SyncRoom::Joined(room) = room { if let SyncRoom::Joined(room) = room {

View File

@ -22,6 +22,7 @@ serde = "1.0.110"
serde_json = "1.0.53" serde_json = "1.0.53"
zeroize = "1.1.0" zeroize = "1.1.0"
matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" }
matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" }
matrix-sdk-crypto = { version = "0.1.0", path = "../matrix_sdk_crypto", optional = true } matrix-sdk-crypto = { version = "0.1.0", path = "../matrix_sdk_crypto", optional = true }

View File

@ -1768,6 +1768,7 @@ mod test {
BaseClient, Session, BaseClient, Session,
}; };
use matrix_sdk_test::{async_test, EventBuilder, EventsFile}; use matrix_sdk_test::{async_test, EventBuilder, EventsFile};
use matrix_sdk_common_macros::async_trait;
use serde_json::json; use serde_json::json;
use std::convert::TryFrom; use std::convert::TryFrom;
@ -1933,7 +1934,7 @@ mod test {
}; };
struct EE(Arc<AtomicBool>); struct EE(Arc<AtomicBool>);
#[async_trait::async_trait] #[async_trait]
impl EventEmitter for EE { impl EventEmitter for EE {
async fn on_room_member(&self, room: SyncRoom, event: &MemberEvent) { async fn on_room_member(&self, room: SyncRoom, event: &MemberEvent) {
if let SyncRoom::Joined(_) = room { if let SyncRoom::Joined(_) = room {
@ -2029,7 +2030,7 @@ mod test {
}; };
struct EE(Arc<AtomicBool>); struct EE(Arc<AtomicBool>);
#[async_trait::async_trait] #[async_trait]
impl EventEmitter for EE { impl EventEmitter for EE {
async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) { async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) {
if let SyncRoom::Joined(_) = room { if let SyncRoom::Joined(_) = room {
@ -2125,7 +2126,7 @@ mod test {
}; };
struct EE(Arc<AtomicBool>); struct EE(Arc<AtomicBool>);
#[async_trait::async_trait] #[async_trait]
impl EventEmitter for EE { impl EventEmitter for EE {
async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) { async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) {
if let SyncRoom::Joined(_) = room { if let SyncRoom::Joined(_) = room {

View File

@ -43,6 +43,7 @@ use crate::events::{
CustomEvent, CustomRoomEvent, CustomStateEvent, CustomEvent, CustomRoomEvent, CustomStateEvent,
}; };
use crate::{Room, RoomState}; use crate::{Room, RoomState};
use matrix_sdk_common_macros::async_trait;
/// Type alias for `RoomState` enum when passed to `EventEmitter` methods. /// Type alias for `RoomState` enum when passed to `EventEmitter` methods.
pub type SyncRoom = RoomState<Arc<RwLock<Room>>>; pub type SyncRoom = RoomState<Arc<RwLock<Room>>>;
@ -78,10 +79,11 @@ pub enum CustomOrRawEvent<'c> {
/// # EventEmitter, SyncRoom /// # EventEmitter, SyncRoom
/// # }; /// # };
/// # use matrix_sdk_common::locks::RwLock; /// # use matrix_sdk_common::locks::RwLock;
/// # use matrix_sdk_common_macros::async_trait;
/// ///
/// struct EventCallback; /// struct EventCallback;
/// ///
/// #[async_trait::async_trait] /// #[async_trait]
/// impl EventEmitter for EventCallback { /// impl EventEmitter for EventCallback {
/// async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) { /// async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) {
/// if let SyncRoom::Joined(room) = room { /// if let SyncRoom::Joined(room) = room {
@ -106,7 +108,7 @@ pub enum CustomOrRawEvent<'c> {
/// } /// }
/// } /// }
/// ``` /// ```
#[async_trait::async_trait] #[async_trait]
pub trait EventEmitter: Send + Sync { pub trait EventEmitter: Send + Sync {
// ROOM EVENTS from `IncomingTimeline` // ROOM EVENTS from `IncomingTimeline`
/// Fires when `Client` receives a `RoomEvent::RoomMember` event. /// Fires when `Client` receives a `RoomEvent::RoomMember` event.
@ -202,6 +204,7 @@ mod test {
use matrix_sdk_common::locks::Mutex; use matrix_sdk_common::locks::Mutex;
use matrix_sdk_test::{async_test, sync_response, SyncResponseFile}; use matrix_sdk_test::{async_test, sync_response, SyncResponseFile};
use std::sync::Arc; use std::sync::Arc;
use matrix_sdk_common_macros::async_trait;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub use wasm_bindgen_test::*; pub use wasm_bindgen_test::*;
@ -209,7 +212,7 @@ mod test {
#[derive(Clone)] #[derive(Clone)]
pub struct EvEmitterTest(Arc<Mutex<Vec<String>>>); pub struct EvEmitterTest(Arc<Mutex<Vec<String>>>);
#[async_trait::async_trait] #[async_trait]
impl EventEmitter for EvEmitterTest { impl EventEmitter for EvEmitterTest {
async fn on_room_member(&self, _: SyncRoom, _: &MemberEvent) { async fn on_room_member(&self, _: SyncRoom, _: &MemberEvent) {
self.0.lock().await.push("member".to_string()) self.0.lock().await.push("member".to_string())

View File

@ -42,8 +42,7 @@ use crate::identifiers::{RoomAliasId, RoomId, UserId};
use crate::js_int::{Int, UInt}; use crate::js_int::{Int, UInt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)] #[derive(Debug, Default, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(test, derive(Clone))]
/// `RoomName` allows the calculation of a text room name. /// `RoomName` allows the calculation of a text room name.
pub struct RoomName { pub struct RoomName {
/// The displayed name of the room. /// The displayed name of the room.
@ -65,8 +64,7 @@ pub struct RoomName {
pub invited_member_count: Option<UInt>, pub invited_member_count: Option<UInt>,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[cfg_attr(test, derive(Clone))]
pub struct PowerLevels { pub struct PowerLevels {
/// The level required to ban a user. /// The level required to ban a user.
pub ban: Int, pub ban: Int,
@ -137,8 +135,7 @@ impl From<&EncryptionEvent> for EncryptionInfo {
} }
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[cfg_attr(test, derive(Clone))]
pub struct Tombstone { pub struct Tombstone {
/// A server-defined message. /// A server-defined message.
body: String, body: String,
@ -146,8 +143,7 @@ pub struct Tombstone {
replacement: RoomId, replacement: RoomId,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(test, derive(Clone))]
/// A Matrix room. /// A Matrix room.
pub struct Room { pub struct Room {
/// The unique id of the room. /// The unique id of the room.

View File

@ -27,8 +27,7 @@ use crate::js_int::{Int, UInt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
// Notes: if Alice invites Bob into a room we will get an event with the sender as Alice and the state key as Bob. // Notes: if Alice invites Bob into a room we will get an event with the sender as Alice and the state key as Bob.
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(test, derive(Clone))]
/// A Matrix room member. /// A Matrix room member.
/// ///
pub struct RoomMember { pub struct RoomMember {

View File

@ -27,6 +27,9 @@ use crate::events::push_rules::Ruleset;
use crate::identifiers::{RoomId, UserId}; use crate::identifiers::{RoomId, UserId};
use crate::{Result, Room, RoomState, Session}; use crate::{Result, Room, RoomState, Session};
#[cfg(not(target_arch = "wasm32"))]
use matrix_sdk_common_macros::send_sync;
/// `ClientState` holds all the information to restore a `BaseClient` /// `ClientState` holds all the information to restore a `BaseClient`
/// except the `access_token` as the default store is not secure. /// except the `access_token` as the default store is not secure.
/// ///
@ -85,7 +88,8 @@ pub struct AllRooms {
/// Abstraction around the data store to avoid unnecessary request on client initialization. /// Abstraction around the data store to avoid unnecessary request on client initialization.
#[async_trait::async_trait] #[async_trait::async_trait]
pub trait StateStore: Send + Sync { #[cfg_attr(not(target_arch = "wasm32"), send_sync)]
pub trait StateStore {
/// Loads the state of `BaseClient` through `ClientState` type. /// Loads the state of `BaseClient` through `ClientState` type.
/// ///
/// An `Option::None` should be returned only if the `StateStore` tries to /// An `Option::None` should be returned only if the `StateStore` tries to

View File

@ -17,6 +17,7 @@ sqlite-cryptostore = ["sqlx"]
[dependencies] [dependencies]
async-trait = "0.1.31" async-trait = "0.1.31"
matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" }
matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" }
olm-rs = { version = "0.5.0", features = ["serde"] } olm-rs = { version = "0.5.0", features = ["serde"] }

View File

@ -28,11 +28,15 @@ use super::memory_stores::UserDevices;
use super::olm::{Account, InboundGroupSession, Session}; use super::olm::{Account, InboundGroupSession, Session};
use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId}; use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId};
use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError}; use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError};
use matrix_sdk_common_macros::send_sync;
pub mod memorystore; pub mod memorystore;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite-cryptostore")] #[cfg(feature = "sqlite-cryptostore")]
pub mod sqlite; pub mod sqlite;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite-cryptostore")] #[cfg(feature = "sqlite-cryptostore")]
use sqlx::Error as SqlxError; use sqlx::Error as SqlxError;
@ -83,9 +87,10 @@ pub enum CryptoStoreError {
pub type Result<T> = std::result::Result<T, CryptoStoreError>; pub type Result<T> = std::result::Result<T, CryptoStoreError>;
#[async_trait] #[async_trait]
#[cfg_attr(not(target_arch = "wasm32"), send_sync)]
/// Trait abstracting a store that the `OlmMachine` uses to store cryptographic /// Trait abstracting a store that the `OlmMachine` uses to store cryptographic
/// keys. /// keys.
pub trait CryptoStore: Debug + Send + Sync { pub trait CryptoStore: Debug {
/// Load an account that was previously stored. /// Load an account that was previously stored.
async fn load_account(&mut self) -> Result<Option<Account>>; async fn load_account(&mut self) -> Result<Option<Account>>;