fast_pull/core/
mod.rs

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