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
20#[cfg(test)]
21matrix_sdk_test_utils::init_tracing_for_tests!();
22
23use futures_core::Future;
24#[doc(no_inline)]
25pub use ruma;
26
27pub mod debug;
28pub mod deserialized_responses;
29pub mod executor;
30pub mod failures_cache;
31pub mod linked_chunk;
32pub mod locks;
33pub mod ring_buffer;
34pub mod serde_helpers;
35pub mod sleep;
36pub mod store_locks;
37pub mod stream;
38pub mod timeout;
39pub mod tracing_timer;
40pub mod ttl_cache;
41
42// We cannot currently measure test coverage in the WASM environment, so
43// js_tracing is incorrectly flagged as untested. Disable coverage checking for
44// it.
45#[cfg(all(target_family = "wasm", not(tarpaulin_include)))]
46pub mod js_tracing;
47
48use ruma::{RoomVersionId, room_version_rules::RoomVersionRules};
49pub use store_locks::LEASE_DURATION_MS;
50
51/// Alias for `Send` on non-wasm, empty trait (implemented by everything) on
52/// wasm.
53#[cfg(not(target_family = "wasm"))]
54pub trait SendOutsideWasm: Send {}
55#[cfg(not(target_family = "wasm"))]
56impl<T: Send> SendOutsideWasm for T {}
57
58/// Alias for `Send` on non-wasm, empty trait (implemented by everything) on
59/// wasm.
60#[cfg(target_family = "wasm")]
61pub trait SendOutsideWasm {}
62#[cfg(target_family = "wasm")]
63impl<T> SendOutsideWasm for T {}
64
65/// Alias for `Sync` on non-wasm, empty trait (implemented by everything) on
66/// wasm.
67#[cfg(not(target_family = "wasm"))]
68pub trait SyncOutsideWasm: Sync {}
69#[cfg(not(target_family = "wasm"))]
70impl<T: Sync> SyncOutsideWasm for T {}
71
72/// Alias for `Sync` on non-wasm, empty trait (implemented by everything) on
73/// wasm.
74#[cfg(target_family = "wasm")]
75pub trait SyncOutsideWasm {}
76#[cfg(target_family = "wasm")]
77impl<T> SyncOutsideWasm for T {}
78
79/// Super trait that is used for our store traits, this trait will differ if
80/// it's used on WASM. WASM targets will not require `Send` and `Sync` to have
81/// implemented, while other targets will.
82pub trait AsyncTraitDeps: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm {}
83impl<T: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm> AsyncTraitDeps for T {}
84
85// TODO: Remove in favor of impl Trait once allowed in associated types
86#[macro_export]
87macro_rules! boxed_into_future {
88    () => {
89        $crate::boxed_into_future!(extra_bounds: );
90    };
91    (extra_bounds: $($extra_bounds:tt)*) => {
92        #[cfg(target_family = "wasm")]
93        type IntoFuture = ::std::pin::Pin<::std::boxed::Box<
94            dyn ::std::future::Future<Output = Self::Output> + $($extra_bounds)*
95        >>;
96        #[cfg(not(target_family = "wasm"))]
97        type IntoFuture = ::std::pin::Pin<::std::boxed::Box<
98            dyn ::std::future::Future<Output = Self::Output> + Send + $($extra_bounds)*
99        >>;
100    };
101}
102
103/// A `Box::pin` future that is `Send` on non-wasm, and without `Send` on wasm.
104#[cfg(target_family = "wasm")]
105pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
106#[cfg(not(target_family = "wasm"))]
107pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
108
109#[cfg(feature = "uniffi")]
110uniffi::setup_scaffolding!();
111
112/// The room version to use as a fallback when the version of a room is unknown.
113pub const ROOM_VERSION_FALLBACK: RoomVersionId = RoomVersionId::V11;
114
115/// The room version rules to use as a fallback when the version of a room is
116/// unknown or unsupported.
117///
118/// These are the rules of the [`ROOM_VERSION_FALLBACK`].
119pub const ROOM_VERSION_RULES_FALLBACK: RoomVersionRules = RoomVersionRules::V11;