diff --git a/src/progress/mod.rs b/src/progress/mod.rs index f43ae1e..bea3315 100644 --- a/src/progress/mod.rs +++ b/src/progress/mod.rs @@ -27,7 +27,10 @@ use std::{ }; mod tasklist; -pub use tasklist::TaskId; +pub use tasklist::{ + TaskId, + IdNotFoundError, +}; /// Command to send to worker task. #[derive(Debug)] @@ -39,7 +42,15 @@ pub enum CommandKind Bump(isize), BumpHigh(isize), + /// Add a task to the tasklist + /// + /// # Response + /// Will respond with the task's `TaskId`. AddTask(String), + /// Remove a task from the tasklist. + /// + /// # Response + /// Will respond with `Result` of the removal operation RemoveTask(TaskId), Shutdown, @@ -184,12 +195,32 @@ impl Handle self.state.read().await } - /// Get an upgraded reference to the bar itself, if it still exists. + + /// Act on a mutable reference to the bar within this closure + /// + /// # Notes + /// Acquiring this will prevent the worker from exiting until the closure finishes. + pub async fn with_bar_mut(&self, fun: F) -> Result + where F: FnOnce(&'_ mut B) -> Fn, + Fn: Future + { + let handle = self.bar.upgrade().ok_or(WorkerCommError)?; + let mut h = handle.write().await; + use std::ops::DerefMut; + Ok(fun(h.deref_mut()).await) + } + /// Act on a reference to the bar within this closure /// - /// This kinda messes things up... Hiding for now. - async fn bar_ref(&self) -> Result, WorkerCommError> + /// # Notes + /// Acquiring this will prevent the worker from exiting until the closure finishes. + pub async fn with_bar(&self, fun: F) -> Result + where F: FnOnce(&'_ B) -> Fn, + Fn: Future { - Ok(BarRef(self.bar.upgrade().ok_or(WorkerCommError)?)) + let handle = self.bar.upgrade().ok_or(WorkerCommError)?; + let h = handle.read().await; + use std::ops::Deref; + Ok(fun(h.deref()).await) } }