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