From 054f5e28f627cbb1cfbcc629ef3aa031c1d38d99 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 18 Dec 2024 12:24:07 +0100 Subject: [PATCH] fix(common): Use a trick to avoid hitting the `recursion_limit` too quickly. This patch adds a trick around `SyncTimelineEvent` to avoid reaching the `recursion_limit` too quickly. Read the documentation in this patch to learn more. --- crates/matrix-sdk-base/Cargo.toml | 5 ++- crates/matrix-sdk-common/Cargo.toml | 4 +++ .../src/deserialized_responses.rs | 33 +++++++++++++++++++ .../matrix-sdk-ui/tests/integration/main.rs | 2 -- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/matrix-sdk-base/Cargo.toml b/crates/matrix-sdk-base/Cargo.toml index 825c52406..363b1c803 100644 --- a/crates/matrix-sdk-base/Cargo.toml +++ b/crates/matrix-sdk-base/Cargo.toml @@ -30,7 +30,10 @@ uniffi = ["dep:uniffi", "matrix-sdk-crypto?/uniffi", "matrix-sdk-common/uniffi"] # Private feature, see # https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823 for the gory # details. -test-send-sync = ["matrix-sdk-crypto?/test-send-sync"] +test-send-sync = [ + "matrix-sdk-common/test-send-sync", + "matrix-sdk-crypto?/test-send-sync", +] # "message-ids" feature doesn't do anything and is deprecated. message-ids = [] diff --git a/crates/matrix-sdk-common/Cargo.toml b/crates/matrix-sdk-common/Cargo.toml index 6b7fdb0eb..a70573eaf 100644 --- a/crates/matrix-sdk-common/Cargo.toml +++ b/crates/matrix-sdk-common/Cargo.toml @@ -18,6 +18,10 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"] [features] js = ["wasm-bindgen-futures"] uniffi = ["dep:uniffi"] +# Private feature, see +# https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823 for the gory +# details. +test-send-sync = [] [dependencies] async-trait = { workspace = true } diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 0833cf3f4..694588875 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -308,6 +308,22 @@ pub struct EncryptionInfo { /// Previously, this differed from [`TimelineEvent`] by wrapping an /// [`AnySyncTimelineEvent`] instead of an [`AnyTimelineEvent`], but nowadays /// they are essentially identical, and one of them should probably be removed. +// +// 🚨 Note about this type, please read! 🚨 +// +// `SyncTimelineEvent` is heavily used across the SDK crates. In some cases, we +// are reaching a [`recursion_limit`] when the compiler is trying to figure out +// if `SyncTimelineEvent` implements `Sync` when it's embedded in other types. +// +// We want to help the compiler so that one doesn't need to increase the +// `recursion_limit`. We stop the recursive check by (un)safely implement `Sync` +// and `Send` on `SyncTimelineEvent` directly. +// +// See +// https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823 +// which has addressed this issue first +// +// [`recursion_limit`]: https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute #[derive(Clone, Debug, Serialize)] pub struct SyncTimelineEvent { /// The event itself, together with any information on decryption. @@ -318,6 +334,23 @@ pub struct SyncTimelineEvent { pub push_actions: Vec, } +// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823. +#[cfg(not(feature = "test-send-sync"))] +unsafe impl Send for SyncTimelineEvent {} + +// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823. +#[cfg(not(feature = "test-send-sync"))] +unsafe impl Sync for SyncTimelineEvent {} + +#[cfg(feature = "test-send-sync")] +#[test] +// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823. +fn test_send_sync_for_sync_timeline_event() { + fn assert_send_sync() {} + + assert_send_sync::(); +} + impl SyncTimelineEvent { /// Create a new `SyncTimelineEvent` from the given raw event. /// diff --git a/crates/matrix-sdk-ui/tests/integration/main.rs b/crates/matrix-sdk-ui/tests/integration/main.rs index 59158ecec..da8bc38c3 100644 --- a/crates/matrix-sdk-ui/tests/integration/main.rs +++ b/crates/matrix-sdk-ui/tests/integration/main.rs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![recursion_limit = "256"] - use itertools::Itertools as _; use matrix_sdk::deserialized_responses::TimelineEvent; use ruma::{events::AnyStateEvent, serde::Raw, EventId, RoomId};