diff --git a/Cargo.toml b/Cargo.toml index fc993d0a..57e18193 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,4 @@ members = [ "matrix_sdk_test_macros", "matrix_sdk_crypto", "matrix_sdk_common", - "matrix_sdk_common_macros", ] diff --git a/matrix_sdk/Cargo.toml b/matrix_sdk/Cargo.toml index 76e4283f..423221be 100644 --- a/matrix_sdk/Cargo.toml +++ b/matrix_sdk/Cargo.toml @@ -28,7 +28,6 @@ socks = ["reqwest/socks"] docs = ["encryption", "sqlite_cryptostore", "messages"] [dependencies] -async-trait = "0.1.41" dashmap = { version = "3.11.10", optional = true } http = "0.2.1" serde_json = "1.0.59" @@ -38,7 +37,6 @@ url = "2.1.1" zeroize = "1.1.1" mime = "0.3.16" -matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } [dependencies.matrix-sdk-base] @@ -68,7 +66,6 @@ version = "3.0.2" features = ["wasm-bindgen"] [dev-dependencies] -async-trait = "0.1.41" async-std = { version = "1.6.5", features = ["unstable"] } dirs = "3.0.1" matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } diff --git a/matrix_sdk/examples/autojoin.rs b/matrix_sdk/examples/autojoin.rs index a5edc34b..91bbf122 100644 --- a/matrix_sdk/examples/autojoin.rs +++ b/matrix_sdk/examples/autojoin.rs @@ -2,11 +2,10 @@ use std::{env, process::exit}; use tokio::time::{delay_for, Duration}; use matrix_sdk::{ - self, + self, async_trait, events::{room::member::MemberEventContent, StrippedStateEvent}, Client, ClientConfig, EventEmitter, SyncRoom, SyncSettings, }; -use matrix_sdk_common_macros::async_trait; use url::Url; struct AutoJoinBot { diff --git a/matrix_sdk/examples/command_bot.rs b/matrix_sdk/examples/command_bot.rs index 8c12fd5a..daef0f43 100644 --- a/matrix_sdk/examples/command_bot.rs +++ b/matrix_sdk/examples/command_bot.rs @@ -1,14 +1,13 @@ use std::{env, process::exit}; use matrix_sdk::{ - self, + self, async_trait, events::{ room::message::{MessageEventContent, TextMessageEventContent}, AnyMessageEventContent, SyncMessageEvent, }, Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings, }; -use matrix_sdk_common_macros::async_trait; use url::Url; struct CommandBot { diff --git a/matrix_sdk/examples/image_bot.rs b/matrix_sdk/examples/image_bot.rs index 141d0d56..142cb24f 100644 --- a/matrix_sdk/examples/image_bot.rs +++ b/matrix_sdk/examples/image_bot.rs @@ -9,14 +9,13 @@ use std::{ use tokio::sync::Mutex; use matrix_sdk::{ - self, + self, async_trait, events::{ room::message::{MessageEventContent, TextMessageEventContent}, SyncMessageEvent, }, Client, ClientConfig, EventEmitter, SyncRoom, SyncSettings, }; -use matrix_sdk_common_macros::async_trait; use url::Url; struct ImageBot { diff --git a/matrix_sdk/examples/login.rs b/matrix_sdk/examples/login.rs index b63155b5..d6fe2fb0 100644 --- a/matrix_sdk/examples/login.rs +++ b/matrix_sdk/examples/login.rs @@ -2,14 +2,13 @@ use std::{env, process::exit}; use url::Url; use matrix_sdk::{ - self, + self, async_trait, events::{ room::message::{MessageEventContent, TextMessageEventContent}, SyncMessageEvent, }, Client, ClientConfig, EventEmitter, SyncRoom, SyncSettings, }; -use matrix_sdk_common_macros::async_trait; struct EventCallback; diff --git a/matrix_sdk/src/http_client.rs b/matrix_sdk/src/http_client.rs index 67c9fee4..0f5adf92 100644 --- a/matrix_sdk/src/http_client.rs +++ b/matrix_sdk/src/http_client.rs @@ -20,9 +20,8 @@ use tracing::trace; use url::Url; use matrix_sdk_common::{ - api::r0::media::create_content, locks::RwLock, AuthScheme, FromHttpResponseError, + api::r0::media::create_content, async_trait, locks::RwLock, AuthScheme, FromHttpResponseError, }; -use matrix_sdk_common_macros::async_trait; use crate::{ClientConfig, Error, OutgoingRequest, Result, Session}; @@ -42,8 +41,7 @@ pub trait HttpSend: Sync + Send + Debug { /// /// ``` /// use std::convert::TryFrom; - /// use matrix_sdk::{HttpSend, Result}; - /// use matrix_sdk_common_macros::async_trait; + /// use matrix_sdk::{HttpSend, Result, async_trait}; /// /// #[derive(Debug)] /// struct Client(reqwest::Client); diff --git a/matrix_sdk_base/Cargo.toml b/matrix_sdk_base/Cargo.toml index 530ca9a3..15516e75 100644 --- a/matrix_sdk_base/Cargo.toml +++ b/matrix_sdk_base/Cargo.toml @@ -24,13 +24,11 @@ unstable-synapse-quirks = ["matrix-sdk-common/unstable-synapse-quirks"] docs = ["encryption", "sqlite_cryptostore", "messages"] [dependencies] -async-trait = "0.1.41" serde = "1.0.117" serde_json = "1.0.59" zeroize = "1.1.1" tracing = "0.1.21" -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-crypto = { version = "0.1.0", path = "../matrix_sdk_crypto", optional = true } diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 3839419e..0c1e8a7e 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -1975,9 +1975,11 @@ mod test { }; use crate::{BaseClient, Session}; - use matrix_sdk_common::identifiers::{room_id, user_id, RoomId}; + use matrix_sdk_common::{ + async_trait, + identifiers::{room_id, user_id, RoomId}, + }; - use matrix_sdk_common_macros::async_trait; use matrix_sdk_test::{async_test, test_json, EventBuilder, EventsJson}; #[cfg(not(target_arch = "wasm32"))] use tempfile::tempdir; @@ -2151,7 +2153,8 @@ mod test { }; struct EE(Arc); - #[async_trait] + #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] + #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl EventEmitter for EE { async fn on_room_member( &self, diff --git a/matrix_sdk_base/src/event_emitter/mod.rs b/matrix_sdk_base/src/event_emitter/mod.rs index 8f5937a8..9062a33b 100644 --- a/matrix_sdk_base/src/event_emitter/mod.rs +++ b/matrix_sdk_base/src/event_emitter/mod.rs @@ -46,7 +46,7 @@ use crate::{ }, Room, RoomState, }; -use matrix_sdk_common_macros::async_trait; +use matrix_sdk_common::async_trait; /// Type alias for `RoomState` enum when passed to `EventEmitter` methods. pub type SyncRoom = RoomState>>; @@ -82,8 +82,7 @@ pub enum CustomEvent<'c> { /// # }, /// # EventEmitter, SyncRoom /// # }; -/// # use matrix_sdk_common::locks::RwLock; -/// # use matrix_sdk_common_macros::async_trait; +/// # use matrix_sdk_common::{async_trait, locks::RwLock}; /// /// struct EventCallback; /// @@ -112,7 +111,8 @@ pub enum CustomEvent<'c> { /// } /// } /// ``` -#[async_trait] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait EventEmitter: Send + Sync { // ROOM EVENTS from `IncomingTimeline` /// Fires when `Client` receives a `RoomEvent::RoomMember` event. @@ -292,8 +292,7 @@ pub trait EventEmitter: Send + Sync { #[cfg(test)] mod test { use super::*; - use matrix_sdk_common::locks::Mutex; - use matrix_sdk_common_macros::async_trait; + use matrix_sdk_common::{async_trait, locks::Mutex}; use matrix_sdk_test::{async_test, sync_response, SyncResponseFile}; use std::sync::Arc; @@ -303,7 +302,8 @@ mod test { #[derive(Clone)] pub struct EvEmitterTest(Arc>>); - #[async_trait] + #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] + #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl EventEmitter for EvEmitterTest { async fn on_room_member(&self, _: SyncRoom, _: &SyncStateEvent) { self.0.lock().await.push("member".to_string()) diff --git a/matrix_sdk_base/src/state/json_store.rs b/matrix_sdk_base/src/state/json_store.rs index 19068995..b88b71f0 100644 --- a/matrix_sdk_base/src/state/json_store.rs +++ b/matrix_sdk_base/src/state/json_store.rs @@ -8,7 +8,7 @@ use std::{ }, }; -use matrix_sdk_common::{identifiers::RoomId, locks::RwLock}; +use matrix_sdk_common::{async_trait, identifiers::RoomId, locks::RwLock}; use tokio::{fs as async_fs, io::AsyncWriteExt}; use super::{AllRooms, ClientState, StateStore}; @@ -78,7 +78,7 @@ impl fmt::Debug for JsonStore { } } -#[async_trait::async_trait] +#[async_trait] impl StateStore for JsonStore { async fn load_client_state(&self, sess: &Session) -> Result> { if !self.user_path_set.load(Ordering::SeqCst) { diff --git a/matrix_sdk_base/src/state/mod.rs b/matrix_sdk_base/src/state/mod.rs index ffd53d58..373cda61 100644 --- a/matrix_sdk_base/src/state/mod.rs +++ b/matrix_sdk_base/src/state/mod.rs @@ -16,8 +16,10 @@ use std::collections::HashMap; use matrix_sdk_common::{ + async_trait, identifiers::{RoomId, UserId}, push::Ruleset, + AsyncTraitDeps, }; use serde::{Deserialize, Serialize}; @@ -31,9 +33,6 @@ 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` /// except the `access_token` as the default store is not secure. /// @@ -91,9 +90,9 @@ pub struct AllRooms { } /// Abstraction around the data store to avoid unnecessary request on client initialization. -#[async_trait::async_trait] -#[cfg_attr(not(target_arch = "wasm32"), send_sync)] -pub trait StateStore { +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait StateStore: AsyncTraitDeps { /// Loads the state of `BaseClient` through `ClientState` type. /// /// An `Option::None` should be returned only if the `StateStore` tries to diff --git a/matrix_sdk_common/Cargo.toml b/matrix_sdk_common/Cargo.toml index 4ee1644f..0964ad19 100644 --- a/matrix_sdk_common/Cargo.toml +++ b/matrix_sdk_common/Cargo.toml @@ -15,6 +15,7 @@ unstable-synapse-quirks = ["ruma/unstable-synapse-quirks"] [dependencies] instant = { version = "0.1.7", features = ["wasm-bindgen", "now"] } +async-trait = "0.1.42" [dependencies.ruma] version = "0.0.2" diff --git a/matrix_sdk_common/src/lib.rs b/matrix_sdk_common/src/lib.rs index daa9d9e0..b157defd 100644 --- a/matrix_sdk_common/src/lib.rs +++ b/matrix_sdk_common/src/lib.rs @@ -1,3 +1,4 @@ +pub use async_trait::async_trait; pub use instant; pub use ruma::{ api::{ @@ -13,3 +14,19 @@ pub use ruma::{ pub use uuid; pub mod locks; + +/// Super trait that is used for our store traits, this trait will differ if +/// it's used on WASM. WASM targets will not require `Send` and `Sync` to have +/// implemented, while other targets will. +#[cfg(not(target_arch = "wasm32"))] +pub trait AsyncTraitDeps: std::fmt::Debug + Send + Sync {} +#[cfg(not(target_arch = "wasm32"))] +impl AsyncTraitDeps for T {} + +/// Super trait that is used for our store traits, this trait will differ if +/// it's used on WASM. WASM targets will not require `Send` and `Sync` to have +/// implemented, while other targets will. +#[cfg(target_arch = "wasm32")] +pub trait AsyncTraitDeps: std::fmt::Debug + Send + Sync {} +#[cfg(target_arch = "wasm32")] +impl AsyncTraitDeps for T {} diff --git a/matrix_sdk_common_macros/Cargo.toml b/matrix_sdk_common_macros/Cargo.toml deleted file mode 100644 index d8219ac7..00000000 --- a/matrix_sdk_common_macros/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -description = "Helper macros for the Matrix SDK" -authors = ["MTRnord "] -edition = "2018" -homepage = "https://github.com/matrix-org/matrix-rust-sdk" -keywords = ["matrix", "chat", "messaging", "ruma"] -license = "Apache-2.0" -name = "matrix-sdk-common-macros" -readme = "README.md" -repository = "https://github.com/matrix-org/matrix-rust-sdk" -version = "0.1.0" - -[lib] -proc-macro = true - -[dependencies] -syn = { version = "1.0.45", features = ["proc-macro"], default-features = false } -quote = "1.0.7" diff --git a/matrix_sdk_common_macros/src/lib.rs b/matrix_sdk_common_macros/src/lib.rs deleted file mode 100644 index 31564805..00000000 --- a/matrix_sdk_common_macros/src/lib.rs +++ /dev/null @@ -1,30 +0,0 @@ -use proc_macro::TokenStream; -use quote::quote; -use syn::{parse_macro_input, parse_quote, ItemTrait}; - -/// Attribute to use `Send + Sync` for everything but wasm32 -#[proc_macro_attribute] -pub fn send_sync(_attr: TokenStream, input: TokenStream) -> TokenStream { - // Parse the input tokens into a syntax tree - let mut input = parse_macro_input!(input as ItemTrait); - - let send_trait_bound = parse_quote!(std::marker::Send); - let sync_trait_bound = parse_quote!(std::marker::Sync); - input.supertraits.push(send_trait_bound); - input.supertraits.push(sync_trait_bound); - - TokenStream::from(quote!(#input)) -} - -/// A wasm32 compatible wrapper for the async_trait::async_trait macro -#[proc_macro_attribute] -pub fn async_trait(_attr: TokenStream, item: TokenStream) -> TokenStream { - let attrs = r#" - #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] - #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] - "#; - - let mut out: TokenStream = attrs.parse().unwrap(); - out.extend(item); - out -} diff --git a/matrix_sdk_crypto/Cargo.toml b/matrix_sdk_crypto/Cargo.toml index 63353cb2..565a0ef5 100644 --- a/matrix_sdk_crypto/Cargo.toml +++ b/matrix_sdk_crypto/Cargo.toml @@ -20,9 +20,6 @@ sqlite_cryptostore = ["sqlx"] docs = ["sqlite_cryptostore"] [dependencies] -async-trait = "0.1.41" - -matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } olm-rs = { version = "1.0.0", features = ["serde"] } diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index d9b3403f..442985a1 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -19,10 +19,10 @@ use std::{ use dashmap::{DashMap, DashSet}; use matrix_sdk_common::{ + async_trait, identifiers::{DeviceId, DeviceIdBox, RoomId, UserId}, locks::Mutex, }; -use matrix_sdk_common_macros::async_trait; use super::{ caches::{DeviceStore, GroupSessionStore, SessionStore}, @@ -92,7 +92,8 @@ impl MemoryStore { } } -#[async_trait] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl CryptoStore for MemoryStore { async fn load_account(&self) -> Result> { Ok(None) diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 9f7f7c4b..54f27b11 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -44,7 +44,6 @@ mod pickle_key; #[cfg(feature = "sqlite_cryptostore")] pub(crate) mod sqlite; -use matrix_sdk_common::identifiers::DeviceIdBox; pub use memorystore::MemoryStore; pub use pickle_key::{EncryptedPickleKey, PickleKey}; #[cfg(not(target_arch = "wasm32"))] @@ -70,14 +69,14 @@ use thiserror::Error; use sqlx::Error as SqlxError; use matrix_sdk_common::{ + async_trait, identifiers::{ - DeviceId, DeviceKeyAlgorithm, Error as IdentifierValidationError, RoomId, UserId, + DeviceId, DeviceIdBox, DeviceKeyAlgorithm, Error as IdentifierValidationError, RoomId, + UserId, }, locks::Mutex, + AsyncTraitDeps, }; -use matrix_sdk_common_macros::async_trait; -#[cfg(not(target_arch = "wasm32"))] -use matrix_sdk_common_macros::send_sync; use crate::{ error::SessionUnpicklingError, @@ -332,10 +331,9 @@ pub enum CryptoStoreError { /// Trait abstracting a store that the `OlmMachine` uses to store cryptographic /// keys. -#[async_trait] -#[allow(clippy::type_complexity)] -#[cfg_attr(not(target_arch = "wasm32"), send_sync)] -pub trait CryptoStore: Debug { +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait CryptoStore: AsyncTraitDeps { /// Load an account that was previously stored. async fn load_account(&self) -> Result>; diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index 27d3cb06..6391028d 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -20,10 +20,10 @@ use std::{ sync::{Arc, Mutex as SyncMutex}, }; -use async_trait::async_trait; use dashmap::DashSet; use matrix_sdk_common::{ api::r0::keys::{CrossSigningKey, KeyUsage}, + async_trait, identifiers::{ DeviceId, DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, RoomId, UserId,