Skip to content

Commit 6aba6e3

Browse files
committed
feat!: split Progress into various super-traits to allow most of them to be dyn-safe.
`Progress` is now `NestedProgress`, `RawProgress` is now `Progress`, and there is a new `Count` trait for solely counting things.
1 parent abcc61b commit 6aba6e3

File tree

12 files changed

+312
-446
lines changed

12 files changed

+312
-446
lines changed

benches/usage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn usage(c: &mut Criterion) {
2828
.throughput(Throughput::Elements(5))
2929
.bench_function("set tree 5 times", |b| {
3030
let root = small_tree();
31-
let mut progress = root.add_child("the one");
31+
let progress = root.add_child("the one");
3232
progress.init(Some(20), Some("element".into()));
3333
b.iter(|| {
3434
progress.set(1);
@@ -44,7 +44,7 @@ fn usage(c: &mut Criterion) {
4444
"send one message with a full message buffer (worst case performance)",
4545
|b| {
4646
let root = small_tree();
47-
let mut progress = root.add_child("the one");
47+
let progress = root.add_child("the one");
4848
progress.init(Some(20), Some("element".into()));
4949
b.iter(|| {
5050
progress.message(MessageLevel::Success, "for testing");

examples/units.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ fn main() -> Result {
3131
}
3232

3333
fn work_for_a_long_time_blocking(root: Arc<Tree>) {
34-
let mut bytes = root.add_child_with_id("download unknown", *b"DLUK");
34+
let bytes = root.add_child_with_id("download unknown", *b"DLUK");
3535
bytes.init(
3636
None,
3737
Some(unit::dynamic_and_mode(
3838
unit::Bytes,
3939
unit::display::Mode::with_throughput(),
4040
)),
4141
);
42-
let mut bytes_max = root.add_child_with_id("download", *b"DLKN");
42+
let bytes_max = root.add_child_with_id("download", *b"DLKN");
4343
bytes_max.init(
4444
Some(100_000_000),
4545
Some(unit::dynamic_and_mode(
@@ -48,9 +48,9 @@ fn work_for_a_long_time_blocking(root: Arc<Tree>) {
4848
)),
4949
);
5050

51-
let mut duration = root.add_child_with_id("duration unknown", *b"DRUK");
51+
let duration = root.add_child_with_id("duration unknown", *b"DRUK");
5252
duration.init(None, Some(unit::dynamic(unit::Duration)));
53-
let mut duration_max = root.add_child_with_id("duration", *b"DRKN");
53+
let duration_max = root.add_child_with_id("duration", *b"DRKN");
5454
duration_max.init(
5555
Some(60 * 60 * 24),
5656
Some(unit::dynamic_and_mode(
@@ -64,15 +64,15 @@ fn work_for_a_long_time_blocking(root: Arc<Tree>) {
6464
f.with_decimals(decimals);
6565
f
6666
}
67-
let mut human_count = root.add_child_with_id("item count unknown", *b"ITUK");
67+
let human_count = root.add_child_with_id("item count unknown", *b"ITUK");
6868
human_count.init(
6969
None,
7070
Some(unit::dynamic_and_mode(
7171
unit::Human::new(formatter(0), "items"),
7272
unit::display::Mode::with_throughput(),
7373
)),
7474
);
75-
let mut human_count_max = root.add_child_with_id("item count", *b"ITKN");
75+
let human_count_max = root.add_child_with_id("item count", *b"ITKN");
7676
human_count_max.init(
7777
Some(7_542_241),
7878
Some(unit::dynamic_and_mode(
@@ -81,15 +81,15 @@ fn work_for_a_long_time_blocking(root: Arc<Tree>) {
8181
)),
8282
);
8383

84-
let mut steps = root.add_child_with_id("steps to take unknown", *b"STUK");
84+
let steps = root.add_child_with_id("steps to take unknown", *b"STUK");
8585
steps.init(
8686
None,
8787
Some(unit::dynamic_and_mode(
8888
unit::Range::new("steps"),
8989
unit::display::Mode::with_throughput(),
9090
)),
9191
);
92-
let mut steps_max = root.add_child_with_id("steps to take", *b"STKN");
92+
let steps_max = root.add_child_with_id("steps to take", *b"STKN");
9393
steps_max.init(
9494
Some(100),
9595
Some(unit::dynamic_and_mode(

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub mod messages;
6464
pub mod progress;
6565

6666
mod traits;
67-
pub use traits::{Progress, RawProgress, Root, WeakRoot};
67+
pub use traits::{Count, NestedProgress, Progress, Root, WeakRoot};
6868

6969
mod throughput;
7070
pub use crate::throughput::Throughput;

src/progress/log.rs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use std::{
99
use crate::{
1010
messages::MessageLevel,
1111
progress::{Id, Step, StepShared},
12-
Progress, Unit,
12+
Count, NestedProgress, Progress, Unit,
1313
};
1414

15-
/// A [`Progress`] implementation which displays progress as it happens without the use of a renderer.
15+
/// A [`NestedProgress`] implementation which displays progress as it happens without the use of a renderer.
1616
///
1717
/// Note that this incurs considerable performance cost as each progress calls ends up getting the system time
1818
/// to see if progress information should actually be emitted.
@@ -73,36 +73,31 @@ impl Log {
7373
}
7474
}
7575

76-
impl Progress for Log {
77-
type SubProgress = Log;
76+
impl Count for Log {
77+
fn set(&self, step: Step) {
78+
self.step.store(step, Ordering::SeqCst);
79+
self.maybe_log()
80+
}
7881

79-
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
80-
self.add_child_with_id(name, crate::progress::UNKNOWN)
82+
fn step(&self) -> usize {
83+
self.step.load(Ordering::Relaxed)
8184
}
8285

83-
fn add_child_with_id(&mut self, name: impl Into<String>, id: Id) -> Self::SubProgress {
84-
Log {
85-
name: format!("{}{}{}", self.name, SEP, Into::<String>::into(name)),
86-
id,
87-
current_level: self.current_level + 1,
88-
max_level: self.max_level,
89-
step: Default::default(),
90-
max: None,
91-
unit: None,
92-
trigger: Arc::clone(&self.trigger),
93-
}
86+
fn inc_by(&self, step: Step) {
87+
self.step.fetch_add(step, Ordering::SeqCst);
88+
self.maybe_log()
9489
}
9590

91+
fn counter(&self) -> Option<StepShared> {
92+
Some(self.step.clone())
93+
}
94+
}
95+
96+
impl Progress for Log {
9697
fn init(&mut self, max: Option<Step>, unit: Option<Unit>) {
9798
self.max = max;
9899
self.unit = unit;
99100
}
100-
101-
fn set(&mut self, step: Step) {
102-
self.step.store(step, Ordering::SeqCst);
103-
self.maybe_log()
104-
}
105-
106101
fn unit(&self) -> Option<Unit> {
107102
self.unit.clone()
108103
}
@@ -117,17 +112,7 @@ impl Progress for Log {
117112
prev
118113
}
119114

120-
fn step(&self) -> usize {
121-
self.step.load(Ordering::Relaxed)
122-
}
123-
124-
fn inc_by(&mut self, step: Step) {
125-
self.step.fetch_add(step, Ordering::SeqCst);
126-
self.maybe_log()
127-
}
128-
129-
fn set_name(&mut self, name: impl Into<String>) {
130-
let name = name.into();
115+
fn set_name(&mut self, name: String) {
131116
self.name = self
132117
.name
133118
.split("::")
@@ -144,16 +129,32 @@ impl Progress for Log {
144129
self.id
145130
}
146131

147-
fn message(&self, level: MessageLevel, message: impl Into<String>) {
148-
let message: String = message.into();
132+
fn message(&self, level: MessageLevel, message: String) {
149133
match level {
150134
MessageLevel::Info => log::info!("ℹ{} → {}", self.name, message),
151135
MessageLevel::Failure => log::error!("𐄂{} → {}", self.name, message),
152136
MessageLevel::Success => log::info!("✓{} → {}", self.name, message),
153137
}
154138
}
139+
}
155140

156-
fn counter(&self) -> Option<StepShared> {
157-
Some(self.step.clone())
141+
impl NestedProgress for Log {
142+
type SubProgress = Log;
143+
144+
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
145+
self.add_child_with_id(name, crate::progress::UNKNOWN)
146+
}
147+
148+
fn add_child_with_id(&mut self, name: impl Into<String>, id: Id) -> Self::SubProgress {
149+
Log {
150+
name: format!("{}{}{}", self.name, SEP, Into::<String>::into(name)),
151+
id,
152+
current_level: self.current_level + 1,
153+
max_level: self.max_level,
154+
step: Default::default(),
155+
max: None,
156+
unit: None,
157+
trigger: Arc::clone(&self.trigger),
158+
}
158159
}
159160
}

0 commit comments

Comments
 (0)