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