Skip to main content

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