From 6c9038eb4fca70f24cc511c02076dc7cdba3590f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 3 Jun 2025 13:37:07 +0200 Subject: [PATCH] refactor(sdk,common): Move `JoinHandleExt` inside `matrix-sdk-common`. This patch moves the `JoinHandleExt` trait and the `AbortOnDrop` type from `matrix_sdk::sliding_sync::utils` into `matrix_sdk_common::executor`. This is going to be useful for other modules. --- crates/matrix-sdk-common/src/executor.rs | 43 +++++++++++++++++++- crates/matrix-sdk/src/sliding_sync/mod.rs | 5 +-- crates/matrix-sdk/src/sliding_sync/utils.rs | 45 --------------------- 3 files changed, 44 insertions(+), 49 deletions(-) delete mode 100644 crates/matrix-sdk/src/sliding_sync/utils.rs diff --git a/crates/matrix-sdk-common/src/executor.rs b/crates/matrix-sdk-common/src/executor.rs index cd359adb8..fa31d62ca 100644 --- a/crates/matrix-sdk-common/src/executor.rs +++ b/crates/matrix-sdk-common/src/executor.rs @@ -14,11 +14,17 @@ //! Abstraction over an executor so we can spawn tasks under Wasm the same way //! we do usually. - +//! //! On non Wasm platforms, this re-exports parts of tokio directly. For Wasm, //! we provide a single-threaded solution that matches the interface that tokio //! provides as a drop in replacement. +use std::{ + future::Future, + pin::Pin, + task::{Context, Poll}, +}; + #[cfg(not(target_family = "wasm"))] mod sys { pub use tokio::{ @@ -141,6 +147,41 @@ mod sys { pub use sys::*; +/// A type ensuring a task is aborted on drop. +#[derive(Debug)] +pub struct AbortOnDrop(JoinHandle); + +impl AbortOnDrop { + pub fn new(join_handle: JoinHandle) -> Self { + Self(join_handle) + } +} + +impl Drop for AbortOnDrop { + fn drop(&mut self) { + self.0.abort(); + } +} + +impl Future for AbortOnDrop { + type Output = Result; + + fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll { + Pin::new(&mut self.0).poll(context) + } +} + +/// Trait to create an [`AbortOnDrop`] from a [`JoinHandle`]. +pub trait JoinHandleExt { + fn abort_on_drop(self) -> AbortOnDrop; +} + +impl JoinHandleExt for JoinHandle { + fn abort_on_drop(self) -> AbortOnDrop { + AbortOnDrop::new(self) + } +} + #[cfg(test)] mod tests { use assert_matches::assert_matches; diff --git a/crates/matrix-sdk/src/sliding_sync/mod.rs b/crates/matrix-sdk/src/sliding_sync/mod.rs index 09efd26a9..869bc217e 100644 --- a/crates/matrix-sdk/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk/src/sliding_sync/mod.rs @@ -21,7 +21,6 @@ mod client; mod error; mod list; mod sticky_parameters; -mod utils; use std::{ collections::{btree_map::Entry, BTreeMap}, @@ -35,6 +34,8 @@ use async_stream::stream; pub use client::{Version, VersionBuilder}; use futures_core::stream::Stream; use matrix_sdk_base::RequestedRequiredStates; +#[cfg(feature = "e2e-encryption")] +use matrix_sdk_common::executor::JoinHandleExt as _; use matrix_sdk_common::{executor::spawn, timer}; use ruma::{ api::client::{error::ErrorKind, sync::sync_events::v5 as http}, @@ -47,8 +48,6 @@ use tokio::{ }; use tracing::{debug, error, info, instrument, trace, warn, Instrument, Span}; -#[cfg(feature = "e2e-encryption")] -use self::utils::JoinHandleExt as _; pub use self::{builder::*, client::VersionBuilderError, error::*, list::*}; use self::{ cache::restore_sliding_sync_state, diff --git a/crates/matrix-sdk/src/sliding_sync/utils.rs b/crates/matrix-sdk/src/sliding_sync/utils.rs deleted file mode 100644 index 6c699a16a..000000000 --- a/crates/matrix-sdk/src/sliding_sync/utils.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Moaaar features for Sliding Sync. - -#![cfg(feature = "e2e-encryption")] - -use std::{ - future::Future, - pin::Pin, - task::{Context, Poll}, -}; - -use matrix_sdk_common::executor::{JoinError, JoinHandle}; - -/// Private type to ensure a task is aborted on drop. -pub(crate) struct AbortOnDrop(JoinHandle); - -impl AbortOnDrop { - fn new(join_handle: JoinHandle) -> Self { - Self(join_handle) - } -} - -impl Drop for AbortOnDrop { - fn drop(&mut self) { - self.0.abort(); - } -} - -impl Future for AbortOnDrop { - type Output = Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Pin::new(&mut self.0).poll(cx) - } -} - -/// Private trait to create a `AbortOnDrop` from a `JoinHandle`. -pub(crate) trait JoinHandleExt { - fn abort_on_drop(self) -> AbortOnDrop; -} - -impl JoinHandleExt for JoinHandle { - fn abort_on_drop(self) -> AbortOnDrop { - AbortOnDrop::new(self) - } -}