matrix_sdk_ffi/
task_handle.rs

1use tokio::task::JoinHandle;
2use tracing::debug;
3
4/// A task handle is a way to keep the handle a task running by itself in
5/// detached mode.
6///
7/// It's a thin wrapper around [`JoinHandle`].
8#[derive(uniffi::Object)]
9pub struct TaskHandle {
10    handle: JoinHandle<()>,
11}
12
13impl TaskHandle {
14    // Create a new task handle.
15    pub fn new(handle: JoinHandle<()>) -> Self {
16        Self { handle }
17    }
18}
19
20#[matrix_sdk_ffi_macros::export]
21impl TaskHandle {
22    // Cancel a task handle.
23    pub fn cancel(&self) {
24        debug!("Cancelling the task handle");
25
26        self.handle.abort();
27    }
28
29    /// Check whether the handle is finished.
30    pub fn is_finished(&self) -> bool {
31        self.handle.is_finished()
32    }
33}
34
35impl Drop for TaskHandle {
36    fn drop(&mut self) {
37        self.cancel();
38    }
39}