1extern crate alloc;
2use crate::Event;
3use alloc::sync::Arc;
4use core::{
5 fmt::Debug,
6 sync::atomic::{AtomicBool, Ordering},
7};
8use futures::lock::Mutex;
9use kanal::AsyncReceiver;
10use tokio::task::{JoinError, JoinHandle};
11
12mod macros;
13#[cfg(test)]
14pub mod mock;
15pub mod multi;
16pub mod single;
17
18#[derive(Debug, Clone)]
19pub struct DownloadResult<ReadError, WriteError> {
20 pub event_chain: AsyncReceiver<Event<ReadError, WriteError>>,
21 handle: Arc<Mutex<Option<JoinHandle<()>>>>,
22 is_running: Arc<AtomicBool>,
23}
24
25impl<ReadError, WriteError> DownloadResult<ReadError, WriteError> {
26 pub fn new(
27 event_chain: AsyncReceiver<Event<ReadError, WriteError>>,
28 handle: JoinHandle<()>,
29 is_running: Arc<AtomicBool>,
30 ) -> Self {
31 Self {
32 event_chain,
33 handle: Arc::new(Mutex::new(Some(handle))),
34 is_running,
35 }
36 }
37
38 pub async fn join(&self) -> Result<(), JoinError> {
39 if let Some(handle) = self.handle.lock().await.take() {
40 handle.await?
41 }
42 Ok(())
43 }
44
45 pub fn cancel(&self) {
47 self.is_running.store(false, Ordering::Relaxed);
48 }
49}