matrix_sdk/sliding_sync/
utils.rs

1//! Moaaar features for Sliding Sync.
2
3#![cfg(feature = "e2e-encryption")]
4
5use std::{
6    future::Future,
7    pin::Pin,
8    task::{Context, Poll},
9};
10
11use matrix_sdk_common::executor::{JoinError, JoinHandle};
12
13/// Private type to ensure a task is aborted on drop.
14pub(crate) struct AbortOnDrop<T>(JoinHandle<T>);
15
16impl<T> AbortOnDrop<T> {
17    fn new(join_handle: JoinHandle<T>) -> Self {
18        Self(join_handle)
19    }
20}
21
22impl<T> Drop for AbortOnDrop<T> {
23    fn drop(&mut self) {
24        self.0.abort();
25    }
26}
27
28impl<T: 'static> Future for AbortOnDrop<T> {
29    type Output = Result<T, JoinError>;
30
31    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
32        Pin::new(&mut self.0).poll(cx)
33    }
34}
35
36/// Private trait to create a `AbortOnDrop` from a `JoinHandle`.
37pub(crate) trait JoinHandleExt<T> {
38    fn abort_on_drop(self) -> AbortOnDrop<T>;
39}
40
41impl<T> JoinHandleExt<T> for JoinHandle<T> {
42    fn abort_on_drop(self) -> AbortOnDrop<T> {
43        AbortOnDrop::new(self)
44    }
45}