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.
This commit is contained in:
Ivan Enderlin
2025-06-03 13:37:07 +02:00
parent 2b9b4cc589
commit 6c9038eb4f
3 changed files with 44 additions and 49 deletions

View File

@@ -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<T>(JoinHandle<T>);
impl<T> AbortOnDrop<T> {
pub fn new(join_handle: JoinHandle<T>) -> Self {
Self(join_handle)
}
}
impl<T> Drop for AbortOnDrop<T> {
fn drop(&mut self) {
self.0.abort();
}
}
impl<T: 'static> Future for AbortOnDrop<T> {
type Output = Result<T, JoinError>;
fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(context)
}
}
/// Trait to create an [`AbortOnDrop`] from a [`JoinHandle`].
pub trait JoinHandleExt<T> {
fn abort_on_drop(self) -> AbortOnDrop<T>;
}
impl<T> JoinHandleExt<T> for JoinHandle<T> {
fn abort_on_drop(self) -> AbortOnDrop<T> {
AbortOnDrop::new(self)
}
}
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;

View File

@@ -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,

View File

@@ -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<T>(JoinHandle<T>);
impl<T> AbortOnDrop<T> {
fn new(join_handle: JoinHandle<T>) -> Self {
Self(join_handle)
}
}
impl<T> Drop for AbortOnDrop<T> {
fn drop(&mut self) {
self.0.abort();
}
}
impl<T: 'static> Future for AbortOnDrop<T> {
type Output = Result<T, JoinError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(cx)
}
}
/// Private trait to create a `AbortOnDrop` from a `JoinHandle`.
pub(crate) trait JoinHandleExt<T> {
fn abort_on_drop(self) -> AbortOnDrop<T>;
}
impl<T> JoinHandleExt<T> for JoinHandle<T> {
fn abort_on_drop(self) -> AbortOnDrop<T> {
AbortOnDrop::new(self)
}
}