From b3cae191413653feef62808b60b7eea52145e55e Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 5 Sep 2023 16:25:46 +0200 Subject: [PATCH] feat: add `BoxedProgress` type that implements `Progress`. This makes working with boxed progress even more flexible. --- src/lib.rs | 4 ++-- src/traits.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 70e9d44..8b35afc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,8 +65,8 @@ pub mod progress; mod traits; pub use traits::{ - BoxedDynNestedProgress, Count, DynNestedProgress, DynNestedProgressToNestedProgress, NestedProgress, Progress, - Root, WeakRoot, + BoxedDynNestedProgress, BoxedProgress, Count, DynNestedProgress, DynNestedProgressToNestedProgress, NestedProgress, + Progress, Root, WeakRoot, }; mod throughput; diff --git a/src/traits.rs b/src/traits.rs index 13a93e6..d65a80a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -64,6 +64,9 @@ pub trait DynNestedProgress: Progress + impls::Sealed { /// An opaque type for storing [`DynNestedProgress`]. pub struct BoxedDynNestedProgress(Box); +/// An owned version of [`Progress`] which can itself implement said trait. +pub type BoxedProgress = Box; + /// A bridge type that implements [`NestedProgress`] for any type that implements [`DynNestedProgress`]. pub struct DynNestedProgressToNestedProgress(pub T); @@ -231,7 +234,7 @@ mod impls { time::Instant, }; - use crate::traits::Progress; + use crate::traits::{BoxedProgress, Progress}; use crate::{ messages::MessageLevel, progress::{Id, Step, StepShared}, @@ -407,6 +410,28 @@ mod impls { } } + impl Progress for BoxedProgress { + fn init(&mut self, max: Option, unit: Option) { + self.deref_mut().init(max, unit) + } + + fn set_name(&mut self, name: String) { + self.deref_mut().set_name(name) + } + + fn name(&self) -> Option { + self.deref().name() + } + + fn id(&self) -> Id { + self.deref().id() + } + + fn message(&self, level: MessageLevel, message: String) { + self.deref().message(level, message) + } + } + impl Count for BoxedDynNestedProgress { fn set(&self, step: Step) { self.0.set(step) @@ -429,6 +454,28 @@ mod impls { } } + impl Count for BoxedProgress { + fn set(&self, step: Step) { + self.deref().set(step) + } + + fn step(&self) -> Step { + self.deref().step() + } + + fn inc_by(&self, step: Step) { + self.deref().inc_by(step) + } + + fn inc(&self) { + self.deref().inc() + } + + fn counter(&self) -> StepShared { + self.deref().counter() + } + } + impl NestedProgress for BoxedDynNestedProgress { type SubProgress = Self;