matrix-sdk: Get rid of the common macros crate

This crate was used to support different trait bounds on WASM vs other
targets, since we only define async traits in a couple of places having
a whole crate to support this feels a bit excessive.

This patch defines a target specific super trait instead, this lowers
the compile time a couple of seconds.
master
Damir Jelić 2021-01-04 16:23:44 +01:00
parent 2e3b6fba7d
commit 4c6c1d2107
20 changed files with 55 additions and 99 deletions

View File

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

View File

@ -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" }

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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;

View File

@ -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);

View File

@ -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 }

View File

@ -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<AtomicBool>);
#[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,

View File

@ -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<Arc<RwLock<Room>>>;
@ -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<Mutex<Vec<String>>>);
#[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<MemberEventContent>) {
self.0.lock().await.push("member".to_string())

View File

@ -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<Option<ClientState>> {
if !self.user_path_set.load(Ordering::SeqCst) {

View File

@ -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

View File

@ -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"

View File

@ -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<T: std::fmt::Debug + Send + Sync> 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<T: std::fmt::Debug + Send + Sync> AsyncTraitDeps for T {}

View File

@ -1,18 +0,0 @@
[package]
description = "Helper macros for the Matrix SDK"
authors = ["MTRnord <mtrnord1@gmail.com>"]
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"

View File

@ -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
}

View File

@ -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"] }

View File

@ -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<Option<ReadOnlyAccount>> {
Ok(None)

View File

@ -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<Option<ReadOnlyAccount>>;

View File

@ -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,