matrix_sdk_common/lib.rs
1// Copyright 2022 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![doc = include_str!("../README.md")]
16#![warn(missing_debug_implementations)]
17
18use std::pin::Pin;
19
20use ruma::{RoomVersionId, room_version_rules::RoomVersionRules};
21
22#[cfg(test)]
23matrix_sdk_test_utils::init_tracing_for_tests!();
24
25use futures_core::Future;
26#[doc(no_inline)]
27pub use ruma;
28
29pub mod cross_process_lock;
30pub mod debug;
31pub mod deserialized_responses;
32mod edit_validation;
33pub mod executor;
34pub mod failures_cache;
35pub mod linked_chunk;
36pub mod locks;
37pub mod ring_buffer;
38pub mod serde_helpers;
39pub mod sleep;
40pub mod stream;
41pub mod timeout;
42pub mod tracing_timer;
43pub mod ttl_cache;
44
45// We cannot currently measure test coverage in the WASM environment, so
46// js_tracing is incorrectly flagged as untested. Disable coverage checking for
47// it.
48#[cfg(all(target_family = "wasm", not(tarpaulin_include)))]
49pub mod js_tracing;
50
51pub use cross_process_lock::LEASE_DURATION_MS;
52pub use edit_validation::*;
53
54/// Alias for `Send` on non-wasm, empty trait (implemented by everything) on
55/// wasm.
56#[cfg(not(target_family = "wasm"))]
57pub trait SendOutsideWasm: Send {}
58#[cfg(not(target_family = "wasm"))]
59impl<T: Send> SendOutsideWasm for T {}
60
61/// Alias for `Send` on non-wasm, empty trait (implemented by everything) on
62/// wasm.
63#[cfg(target_family = "wasm")]
64pub trait SendOutsideWasm {}
65#[cfg(target_family = "wasm")]
66impl<T> SendOutsideWasm for T {}
67
68/// Alias for `Sync` on non-wasm, empty trait (implemented by everything) on
69/// wasm.
70#[cfg(not(target_family = "wasm"))]
71pub trait SyncOutsideWasm: Sync {}
72#[cfg(not(target_family = "wasm"))]
73impl<T: Sync> SyncOutsideWasm for T {}
74
75/// Alias for `Sync` on non-wasm, empty trait (implemented by everything) on
76/// wasm.
77#[cfg(target_family = "wasm")]
78pub trait SyncOutsideWasm {}
79#[cfg(target_family = "wasm")]
80impl<T> SyncOutsideWasm for T {}
81
82/// Super trait that is used for our store traits, this trait will differ if
83/// it's used on WASM. WASM targets will not require `Send` and `Sync` to have
84/// implemented, while other targets will.
85pub trait AsyncTraitDeps: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm {}
86impl<T: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm> AsyncTraitDeps for T {}
87
88// TODO: Remove in favor of impl Trait once allowed in associated types
89#[macro_export]
90macro_rules! boxed_into_future {
91 () => {
92 $crate::boxed_into_future!(extra_bounds: );
93 };
94 (extra_bounds: $($extra_bounds:tt)*) => {
95 #[cfg(target_family = "wasm")]
96 type IntoFuture = ::std::pin::Pin<::std::boxed::Box<
97 dyn ::std::future::Future<Output = Self::Output> + $($extra_bounds)*
98 >>;
99 #[cfg(not(target_family = "wasm"))]
100 type IntoFuture = ::std::pin::Pin<::std::boxed::Box<
101 dyn ::std::future::Future<Output = Self::Output> + Send + $($extra_bounds)*
102 >>;
103 };
104}
105
106/// A `Box::pin` future that is `Send` on non-wasm, and without `Send` on wasm.
107#[cfg(target_family = "wasm")]
108pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
109#[cfg(not(target_family = "wasm"))]
110pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
111
112#[cfg(feature = "uniffi")]
113uniffi::setup_scaffolding!();
114
115/// The room version to use as a fallback when the version of a room is unknown.
116pub const ROOM_VERSION_FALLBACK: RoomVersionId = RoomVersionId::V11;
117
118/// The room version rules to use as a fallback when the version of a room is
119/// unknown or unsupported.
120///
121/// These are the rules of the [`ROOM_VERSION_FALLBACK`].
122pub const ROOM_VERSION_RULES_FALLBACK: RoomVersionRules = RoomVersionRules::V11;