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;
12pub mod mock;
13pub mod multi;
14pub mod single;
15
16#[derive(Debug)]
17pub struct DownloadResult<PullError, PushError> {
18 pub event_chain: AsyncReceiver<Event<PullError, PushError>>,
19 handle: Arc<Mutex<Option<JoinHandle<()>>>>,
20 abort_handles: Arc<[AbortHandle]>,
21}
22
23impl<RE, WE> Clone for DownloadResult<RE, WE> {
24 fn clone(&self) -> Self {
25 Self {
26 event_chain: self.event_chain.clone(),
27 handle: self.handle.clone(),
28 abort_handles: self.abort_handles.clone(),
29 }
30 }
31}
32
33impl<PullError, PushError> DownloadResult<PullError, PushError> {
34 pub fn new(
35 event_chain: AsyncReceiver<Event<PullError, PushError>>,
36 handle: JoinHandle<()>,
37 abort_handles: &[AbortHandle],
38 ) -> Self {
39 Self {
40 event_chain,
41 abort_handles: Arc::from(abort_handles),
42 handle: Arc::new(Mutex::new(Some(handle))),
43 }
44 }
45
46 pub async fn join(&self) -> Result<(), JoinError> {
48 if let Some(handle) = self.handle.lock().await.take() {
49 handle.await?
50 }
51 Ok(())
52 }
53
54 pub fn abort(&self) {
55 for abort_handle in self.abort_handles.iter() {
56 abort_handle.abort();
57 }
58 }
59}