Skip to content

Commit

Permalink
feat: add BoxedProgress type that implements Progress.
Browse files Browse the repository at this point in the history
This makes working with boxed progress even more flexible.
  • Loading branch information
Byron committed Sep 5, 2023
1 parent 05bc923 commit b3cae19
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub trait DynNestedProgress: Progress + impls::Sealed {
/// An opaque type for storing [`DynNestedProgress`].
pub struct BoxedDynNestedProgress(Box<dyn DynNestedProgress>);

/// An owned version of [`Progress`] which can itself implement said trait.
pub type BoxedProgress = Box<dyn Progress>;

/// A bridge type that implements [`NestedProgress`] for any type that implements [`DynNestedProgress`].
pub struct DynNestedProgressToNestedProgress<T: ?Sized>(pub T);

Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -407,6 +410,28 @@ mod impls {
}
}

impl Progress for BoxedProgress {
fn init(&mut self, max: Option<Step>, unit: Option<Unit>) {
self.deref_mut().init(max, unit)
}

fn set_name(&mut self, name: String) {
self.deref_mut().set_name(name)
}

fn name(&self) -> Option<String> {
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)
Expand All @@ -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;

Expand Down

0 comments on commit b3cae19

Please sign in to comment.