1extern crate alloc;
2use crate::Event;
3use alloc::sync::Arc;
4use core::fmt::Debug;
5use kanal::AsyncReceiver;
6use tokio::{
7 sync::Mutex,
8 task::{AbortHandle, JoinError, JoinHandle},
9};
10
11mod macros;
12#[cfg(test)]
13pub mod mock;
14pub mod multi;
15pub mod single;
16
17#[derive(Debug)]
18pub struct DownloadResult<PullError, PushError> {
19 pub event_chain: AsyncReceiver<Event<PullError, PushError>>,
20 handle: Arc<Mutex<Option<JoinHandle<()>>>>,
21 abort_handles: Arc<[AbortHandle]>,
22}
23
24impl<RE, WE> Clone for DownloadResult<RE, WE> {
25 fn clone(&self) -> Self {
26 Self {
27 event_chain: self.event_chain.clone(),
28 handle: self.handle.clone(),
29 abort_handles: self.abort_handles.clone(),
30 }
31 }
32}
33
34impl<PullError, PushError> DownloadResult<PullError, PushError> {
35 pub fn new(
36 event_chain: AsyncReceiver<Event<PullError, PushError>>,
37 handle: JoinHandle<()>,
38 abort_handles: &[AbortHandle],
39 ) -> Self {
40 Self {
41 event_chain,
42 abort_handles: Arc::from(abort_handles),
43 handle: Arc::new(Mutex::new(Some(handle))),
44 }
45 }
46
47 pub async fn join(&self) -> Result<(), JoinError> {
49 if let Some(handle) = self.handle.lock().await.take() {
50 handle.await?
51 }
52 Ok(())
53 }
54
55 pub fn abort(&self) {
56 for abort_handle in self.abort_handles.iter() {
57 abort_handle.abort();
58 }
59 }
60}