|
| 1 | +/*! |
| 2 | +# FYI Msg - Progless Increment Guard. |
| 3 | +*/ |
| 4 | + |
| 5 | +use std::sync::Arc; |
| 6 | +use super::ProglessInner; |
| 7 | + |
| 8 | +#[expect(unused_imports, reason = "For docs.")] |
| 9 | +use super::Progless; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +/// # Progless Task Guard. |
| 15 | +/// |
| 16 | +/// This guard is returned by [`Progless::task`]. |
| 17 | +/// |
| 18 | +/// When dropped, the task will automatically be removed from the [`Progless`] |
| 19 | +/// output and the done count will be increased by one. |
| 20 | +/// |
| 21 | +/// To drop it _without_ incrementing the done count — because you e.g. |
| 22 | +/// changed your mind or need to come back to it later — use |
| 23 | +/// [`ProglessTaskGuard::cancel`]. |
| 24 | +pub struct ProglessTaskGuard<'a> { |
| 25 | + /// # Task Name. |
| 26 | + task: String, |
| 27 | + |
| 28 | + /// # Increment on Drop? |
| 29 | + inc: bool, |
| 30 | + |
| 31 | + /// # Cycle Number. |
| 32 | + /// |
| 33 | + /// This value is used to prevent a guard from one [`Progless`] cycle |
| 34 | + /// affecting the totals of a subsequent one (after e.g. a reset). |
| 35 | + cycle: u8, |
| 36 | + |
| 37 | + /// # Linked [`Progless`] Instance. |
| 38 | + progress: &'a Arc<ProglessInner>, |
| 39 | +} |
| 40 | + |
| 41 | +impl Drop for ProglessTaskGuard<'_> { |
| 42 | + #[inline] |
| 43 | + fn drop(&mut self) { |
| 44 | + self.progress.remove_guard(self.task.as_str(), self.cycle, self.inc); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<'a> ProglessTaskGuard<'a> { |
| 49 | + /// # From Parts. |
| 50 | + pub(super) const fn from_parts( |
| 51 | + task: String, |
| 52 | + cycle: u8, |
| 53 | + progress: &'a Arc<ProglessInner>, |
| 54 | + ) -> Self { |
| 55 | + Self { task, inc: true, cycle, progress } |
| 56 | + } |
| 57 | + |
| 58 | + #[inline] |
| 59 | + /// # Cancel Guard. |
| 60 | + /// |
| 61 | + /// Remove the task from the [`Progless`] output _without_ incrementing |
| 62 | + /// the done count. |
| 63 | + pub fn cancel(mut self) { |
| 64 | + self.inc = false; |
| 65 | + drop(self); |
| 66 | + } |
| 67 | +} |
0 commit comments