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