Skip to content

Commit 6c9038e

Browse files
committed
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.
1 parent 2b9b4cc commit 6c9038e

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

crates/matrix-sdk-common/src/executor.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414

1515
//! Abstraction over an executor so we can spawn tasks under Wasm the same way
1616
//! we do usually.
17-
17+
//!
1818
//! On non Wasm platforms, this re-exports parts of tokio directly. For Wasm,
1919
//! we provide a single-threaded solution that matches the interface that tokio
2020
//! provides as a drop in replacement.
2121
22+
use std::{
23+
future::Future,
24+
pin::Pin,
25+
task::{Context, Poll},
26+
};
27+
2228
#[cfg(not(target_family = "wasm"))]
2329
mod sys {
2430
pub use tokio::{
@@ -141,6 +147,41 @@ mod sys {
141147

142148
pub use sys::*;
143149

150+
/// A type ensuring a task is aborted on drop.
151+
#[derive(Debug)]
152+
pub struct AbortOnDrop<T>(JoinHandle<T>);
153+
154+
impl<T> AbortOnDrop<T> {
155+
pub fn new(join_handle: JoinHandle<T>) -> Self {
156+
Self(join_handle)
157+
}
158+
}
159+
160+
impl<T> Drop for AbortOnDrop<T> {
161+
fn drop(&mut self) {
162+
self.0.abort();
163+
}
164+
}
165+
166+
impl<T: 'static> Future for AbortOnDrop<T> {
167+
type Output = Result<T, JoinError>;
168+
169+
fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
170+
Pin::new(&mut self.0).poll(context)
171+
}
172+
}
173+
174+
/// Trait to create an [`AbortOnDrop`] from a [`JoinHandle`].
175+
pub trait JoinHandleExt<T> {
176+
fn abort_on_drop(self) -> AbortOnDrop<T>;
177+
}
178+
179+
impl<T> JoinHandleExt<T> for JoinHandle<T> {
180+
fn abort_on_drop(self) -> AbortOnDrop<T> {
181+
AbortOnDrop::new(self)
182+
}
183+
}
184+
144185
#[cfg(test)]
145186
mod tests {
146187
use assert_matches::assert_matches;

crates/matrix-sdk/src/sliding_sync/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ mod client;
2121
mod error;
2222
mod list;
2323
mod sticky_parameters;
24-
mod utils;
2524

2625
use std::{
2726
collections::{btree_map::Entry, BTreeMap},
@@ -35,6 +34,8 @@ use async_stream::stream;
3534
pub use client::{Version, VersionBuilder};
3635
use futures_core::stream::Stream;
3736
use matrix_sdk_base::RequestedRequiredStates;
37+
#[cfg(feature = "e2e-encryption")]
38+
use matrix_sdk_common::executor::JoinHandleExt as _;
3839
use matrix_sdk_common::{executor::spawn, timer};
3940
use ruma::{
4041
api::client::{error::ErrorKind, sync::sync_events::v5 as http},
@@ -47,8 +48,6 @@ use tokio::{
4748
};
4849
use tracing::{debug, error, info, instrument, trace, warn, Instrument, Span};
4950

50-
#[cfg(feature = "e2e-encryption")]
51-
use self::utils::JoinHandleExt as _;
5251
pub use self::{builder::*, client::VersionBuilderError, error::*, list::*};
5352
use self::{
5453
cache::restore_sliding_sync_state,

crates/matrix-sdk/src/sliding_sync/utils.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)