diff --git a/fav_core/src/local.rs b/fav_core/src/local.rs index b6bb3e3..5956ff9 100644 --- a/fav_core/src/local.rs +++ b/fav_core/src/local.rs @@ -64,17 +64,16 @@ impl ProtoLocal for T where T: PathInfo + MessageFull {} /// Make it able to save the resource to local pub trait SaveLocal: Net { /// Save the resource to local. - /// `F: Fn() -> Future<...>`, if Future is ready, one can cleanup and + /// `cancelled: Future<...>`, if Future is ready, one can cleanup and /// shutdown gracefully, then return `FavCoreError::Cancel`. - fn download( + fn download( &self, res: &mut R, urls: Vec, - cancelled: F, + cancelled: Fut, ) -> impl Future> where R: Res, - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send; } diff --git a/fav_core/src/ops.rs b/fav_core/src/ops.rs index be19e0f..22604c5 100644 --- a/fav_core/src/ops.rs +++ b/fav_core/src/ops.rs @@ -83,11 +83,10 @@ pub trait LocalSetOps: Net + HttpConfig { /// The set type the operations on type Set: Set; /// Fetch one resource set, - /// `F: Fn() -> Future<...>`, if Future is ready, one can cleanup and + /// `cancelled: Future<...>`, if Future is ready, one can cleanup and /// shutdown gracefully, then return `FavCoreError::Cancel`. - async fn fetch_set(&self, set: &mut Self::Set, cancelled: F) -> FavCoreResult<()> + async fn fetch_set(&self, set: &mut Self::Set, cancelled: Fut) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send; } @@ -101,27 +100,25 @@ pub trait LocalResOps: Net + HttpConfig { /// The resource type the operations on type Res: Res; /// Fetch one resource, - /// `F: Fn() -> Future<...>`, if Future is ready, one can cleanup and + /// `cancelled: Future<...>`, if Future is ready, one can cleanup and /// shutdown gracefully, then return `FavCoreError::Cancel`. - fn fetch_res( + async fn fetch_res( &self, resource: &mut Self::Res, - cancelled: F, - ) -> impl Future> + cancelled: Fut, + ) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send; /// Pull one resource, - /// `F: Fn() -> Future<...>`, if Future is ready, one can cleanup and + /// `cancelled: Future<...>`, if Future is ready, one can cleanup and /// shutdown gracefully, then return `FavCoreError::Cancel`. - async fn pull_res( + async fn pull_res( &self, resource: &mut Self::Res, - cancelled: F, + cancelled: Fut, ) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send; } @@ -146,7 +143,7 @@ pub trait SetOpsExt: SetOps { where SS: Sets, { - batch_op_set(sets, |r, f| self.fetch_set(r, f)) + batch_op_set(sets, |s, fut| self.fetch_set(s, fut)) } } @@ -157,14 +154,13 @@ pub trait SetOpsExt: SetOps { pub async fn batch_op_set<'a, SS, F, T>(sets: &'a mut SS, mut f: F) -> FavCoreResult<()> where SS: Sets + 'a, - F: FnMut(&'a mut SS::Set, Box WaitForCancellationFutureOwned + Send>) -> T, + F: FnMut(&'a mut SS::Set, WaitForCancellationFutureOwned) -> T, T: Future>, { let token = CancellationToken::new(); let mut stream = tokio_stream::iter(sets.iter_mut()) .map(|s| { - let token1 = token.clone(); - let shutdown = Box::new(move || token1.cancelled_owned()); + let shutdown = token.clone().cancelled_owned(); let fut = f(s, shutdown); let token = token.clone(); async move { @@ -221,7 +217,7 @@ pub trait ResOpsExt: ResOps { where S: Set, { - batch_op_res(set, |r, f| self.fetch_res(r, f)) + batch_op_res(set, |r, fut| self.fetch_res(r, fut)) } /// **Asynchronously** pull resourses in set using [`ResOps::pull_res`]. @@ -229,7 +225,7 @@ pub trait ResOpsExt: ResOps { where S: Set, { - batch_op_res(set, |r, f| self.pull_res(r, f)) + batch_op_res(set, |r, fut| self.pull_res(r, fut)) } } @@ -270,14 +266,13 @@ pub trait ResOpsExt: ResOps { pub async fn batch_op_res<'a, S, F, T>(set: &'a mut S, mut f: F) -> FavCoreResult<()> where S: Set + 'a, - F: FnMut(&'a mut S::Res, Box WaitForCancellationFutureOwned + Send>) -> T, + F: FnMut(&'a mut S::Res, WaitForCancellationFutureOwned) -> T, T: Future>, { let token = CancellationToken::new(); let mut stream = tokio_stream::iter(set.iter_mut()) .map(|s| { - let token1 = token.clone(); - let shutdown = Box::new(move || token1.cancelled_owned()); + let shutdown = token.clone().cancelled_owned(); let fut = f(s, shutdown); let token = token.clone(); async move { diff --git a/fav_core/src/test_utils/impls.rs b/fav_core/src/test_utils/impls.rs index 225edd4..5013d48 100644 --- a/fav_core/src/test_utils/impls.rs +++ b/fav_core/src/test_utils/impls.rs @@ -41,18 +41,16 @@ impl AuthOps for App { impl ResOps for App { type Res = TestRes; - async fn fetch_res(&self, _: &mut Self::Res, _: F) -> FavCoreResult<()> + async fn fetch_res(&self, _: &mut Self::Res, _: Fut) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { todo!() } - async fn pull_res(&self, _: &mut Self::Res, _: F) -> FavCoreResult<()> + async fn pull_res(&self, _: &mut Self::Res, _: Fut) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { @@ -63,9 +61,8 @@ impl ResOps for App { impl SetOps for App { type Set = TestSet; - async fn fetch_set(&self, _: &mut Self::Set, _: F) -> FavCoreResult<()> + async fn fetch_set(&self, _: &mut Self::Set, _: Fut) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { diff --git a/fav_utils/src/bili/local.rs b/fav_utils/src/bili/local.rs index 3c7fc17..a2de6e0 100644 --- a/fav_utils/src/bili/local.rs +++ b/fav_utils/src/bili/local.rs @@ -20,15 +20,14 @@ impl PathInfo for BiliSets { } impl SaveLocal for Bili { - async fn download( + async fn download( &self, res: &mut R, urls: Vec, - f: F, + cancelled: Fut, ) -> FavCoreResult<()> where R: Res, - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { @@ -76,7 +75,7 @@ impl SaveLocal for Bili { res.on_status(StatusFlags::SAVED); Ok(()) }, - _ = f() => { + _ = cancelled => { file_v.into_inner().unwrap().close()?; file_a.into_inner().unwrap().close()?; Err(FavCoreError::Cancel) diff --git a/fav_utils/src/bili/ops.rs b/fav_utils/src/bili/ops.rs index 640782f..88c7ef6 100644 --- a/fav_utils/src/bili/ops.rs +++ b/fav_utils/src/bili/ops.rs @@ -65,9 +65,8 @@ impl SetsOps for Bili { impl SetOps for Bili { type Set = BiliSet; - async fn fetch_set(&self, set: &mut Self::Set, cancelled: F) -> FavCoreResult<()> + async fn fetch_set(&self, set: &mut Self::Set, cancelled: Fut) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { @@ -94,7 +93,7 @@ impl SetOps for Bili { } => { res } - _ = cancelled() => Err(FavCoreError::Cancel) + _ = cancelled => Err(FavCoreError::Cancel) } } } @@ -102,9 +101,12 @@ impl SetOps for Bili { impl ResOps for Bili { type Res = BiliRes; - async fn fetch_res(&self, resource: &mut Self::Res, f: F) -> FavCoreResult<()> + async fn fetch_res( + &self, + resource: &mut Self::Res, + cancelled: Fut, + ) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { @@ -119,15 +121,18 @@ impl ResOps for Bili { resource.on_status(StatusFlags::FETCHED); Ok(()) }, - _ = f() => { + _ = cancelled => { Err(FavCoreError::Cancel) } } } - async fn pull_res(&self, resource: &mut Self::Res, f: F) -> FavCoreResult<()> + async fn pull_res( + &self, + resource: &mut Self::Res, + cancelled: Fut, + ) -> FavCoreResult<()> where - F: FnOnce() -> Fut + Send, Fut: Future + Send, Any: Send, { @@ -157,7 +162,8 @@ impl ResOps for Bili { } Err(e) => return Err(e), }; - self.download(resource, vec![audio, video], f).await?; + self.download(resource, vec![audio, video], cancelled) + .await?; Ok(()) } } @@ -267,7 +273,7 @@ mod tests { let mut sets = BiliSets::default(); bili.fetch_sets(&mut sets).await.unwrap(); let set = sets.iter_mut().min_by_key(|s| s.media_count).unwrap(); - bili.fetch_set(set, tokio::signal::ctrl_c).await.unwrap(); + bili.fetch_set(set, tokio::signal::ctrl_c()).await.unwrap(); bili.batch_fetch_res(set).await.unwrap(); bili.batch_pull_res(set).await.unwrap(); sets.write().unwrap(); @@ -280,7 +286,7 @@ mod tests { let mut sets = BiliSets::read().unwrap(); bili.fetch_sets(&mut sets).await.unwrap(); let set = sets.iter_mut().min_by_key(|s| s.media_count).unwrap(); - bili.fetch_set(set, tokio::signal::ctrl_c).await.unwrap(); + bili.fetch_set(set, tokio::signal::ctrl_c()).await.unwrap(); set.on_res_status(StatusFlags::TRACK); let mut sub = set.subset(|r| r.check_status(StatusFlags::TRACK)); bili.batch_fetch_res(&mut sub).await.unwrap();