Coverage Report

Created: 2024-08-22 06:13

/rust/registry/src/index.crates.io-6f17d22bba15001f/prodash-29.0.0/src/progress/utils.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::{messages::MessageLevel, progress::Id, Count, NestedProgress, Progress, Unit};
2
use std::sync::atomic::AtomicUsize;
3
use std::sync::Arc;
4
5
/// An implementation of [`NestedProgress`] which discards all calls.
6
pub struct Discard;
7
8
impl Count for Discard {
9
0
    fn set(&self, _step: usize) {}
10
11
0
    fn step(&self) -> usize {
12
0
        0
13
0
    }
14
15
0
    fn inc_by(&self, _step: usize) {}
16
17
0
    fn counter(&self) -> StepShared {
18
0
        Arc::new(AtomicUsize::default())
19
0
    }
20
}
21
22
impl Progress for Discard {
23
0
    fn init(&mut self, _max: Option<usize>, _unit: Option<Unit>) {}
24
25
0
    fn set_max(&mut self, _max: Option<Step>) -> Option<Step> {
26
0
        None
27
0
    }
28
0
    fn set_name(&mut self, _name: String) {}
29
30
0
    fn name(&self) -> Option<String> {
31
0
        None
32
0
    }
33
34
0
    fn id(&self) -> Id {
35
0
        crate::progress::UNKNOWN
36
0
    }
37
38
0
    fn message(&self, _level: MessageLevel, _message: String) {}
39
}
40
41
impl NestedProgress for Discard {
42
    type SubProgress = Self;
43
44
0
    fn add_child(&mut self, _name: impl Into<String>) -> Self {
45
0
        Discard
46
0
    }
47
48
0
    fn add_child_with_id(&mut self, _name: impl Into<String>, _id: Id) -> Self {
49
0
        Discard
50
0
    }
51
}
52
53
/// An implementation of [`NestedProgress`] showing either one or the other implementation.
54
///
55
/// Useful in conjunction with [`Discard`] and a working implementation, making it as a form of `Option<Progress>` which
56
/// can be passed to methods requiring `impl Progress`.
57
/// See [`DoOrDiscard`] for an incarnation of this.
58
#[allow(missing_docs)]
59
pub enum Either<L, R> {
60
    Left(L),
61
    Right(R),
62
}
63
64
impl<L, R> Count for Either<L, R>
65
where
66
    L: Count,
67
    R: Count,
68
{
69
0
    fn set(&self, step: usize) {
70
0
        match self {
71
0
            Either::Left(l) => l.set(step),
72
0
            Either::Right(r) => r.set(step),
73
        }
74
0
    }
75
0
    fn step(&self) -> usize {
76
0
        match self {
77
0
            Either::Left(l) => l.step(),
78
0
            Either::Right(r) => r.step(),
79
        }
80
0
    }
81
0
    fn inc_by(&self, step: usize) {
82
0
        match self {
83
0
            Either::Left(l) => l.inc_by(step),
84
0
            Either::Right(r) => r.inc_by(step),
85
        }
86
0
    }
87
0
    fn counter(&self) -> StepShared {
88
0
        match self {
89
0
            Either::Left(l) => l.counter(),
90
0
            Either::Right(r) => r.counter(),
91
        }
92
0
    }
93
}
94
95
impl<L, R> Progress for Either<L, R>
96
where
97
    L: Progress,
98
    R: Progress,
99
{
100
0
    fn init(&mut self, max: Option<usize>, unit: Option<Unit>) {
101
0
        match self {
102
0
            Either::Left(l) => l.init(max, unit),
103
0
            Either::Right(r) => r.init(max, unit),
104
        }
105
0
    }
106
107
0
    fn unit(&self) -> Option<Unit> {
108
0
        match self {
109
0
            Either::Left(l) => l.unit(),
110
0
            Either::Right(r) => r.unit(),
111
        }
112
0
    }
113
114
0
    fn max(&self) -> Option<usize> {
115
0
        match self {
116
0
            Either::Left(l) => l.max(),
117
0
            Either::Right(r) => r.max(),
118
        }
119
0
    }
120
121
0
    fn set_max(&mut self, max: Option<Step>) -> Option<Step> {
122
0
        match self {
123
0
            Either::Left(l) => l.set_max(max),
124
0
            Either::Right(r) => r.set_max(max),
125
        }
126
0
    }
127
128
0
    fn set_name(&mut self, name: String) {
129
0
        match self {
130
0
            Either::Left(l) => l.set_name(name),
131
0
            Either::Right(r) => r.set_name(name),
132
        }
133
0
    }
134
135
0
    fn name(&self) -> Option<String> {
136
0
        match self {
137
0
            Either::Left(l) => l.name(),
138
0
            Either::Right(r) => r.name(),
139
        }
140
0
    }
141
142
0
    fn id(&self) -> Id {
143
0
        match self {
144
0
            Either::Left(l) => l.id(),
145
0
            Either::Right(r) => r.id(),
146
        }
147
0
    }
148
149
0
    fn message(&self, level: MessageLevel, message: String) {
150
0
        match self {
151
0
            Either::Left(l) => l.message(level, message),
152
0
            Either::Right(r) => r.message(level, message),
153
        }
154
0
    }
155
}
156
157
impl<L, R> NestedProgress for Either<L, R>
158
where
159
    L: NestedProgress,
160
    R: NestedProgress,
161
{
162
    type SubProgress = Either<L::SubProgress, R::SubProgress>;
163
164
0
    fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
165
0
        match self {
166
0
            Either::Left(l) => Either::Left(l.add_child(name)),
167
0
            Either::Right(r) => Either::Right(r.add_child(name)),
168
        }
169
0
    }
170
171
0
    fn add_child_with_id(&mut self, name: impl Into<String>, id: Id) -> Self::SubProgress {
172
0
        match self {
173
0
            Either::Left(l) => Either::Left(l.add_child_with_id(name, id)),
174
0
            Either::Right(r) => Either::Right(r.add_child_with_id(name, id)),
175
        }
176
0
    }
177
}
178
179
/// An implementation of `Progress` which can be created easily from `Option<impl Progress>`.
180
pub struct DoOrDiscard<T>(Either<T, Discard>);
181
182
impl<T> From<Option<T>> for DoOrDiscard<T>
183
where
184
    T: NestedProgress,
185
{
186
0
    fn from(p: Option<T>) -> Self {
187
0
        match p {
188
0
            Some(p) => DoOrDiscard(Either::Left(p)),
189
0
            None => DoOrDiscard(Either::Right(Discard)),
190
        }
191
0
    }
192
}
193
194
impl<T: NestedProgress> DoOrDiscard<T> {
195
    /// Obtain either the original [`NestedProgress`] implementation or `None`.
196
0
    pub fn into_inner(self) -> Option<T> {
197
0
        match self {
198
0
            DoOrDiscard(Either::Left(p)) => Some(p),
199
0
            DoOrDiscard(Either::Right(_)) => None,
200
        }
201
0
    }
202
203
    /// Take out the implementation of [`NestedProgress`] and replace it with [`Discard`].
204
0
    pub fn take(&mut self) -> Option<T> {
205
0
        let this = std::mem::replace(self, DoOrDiscard::from(None));
206
0
        match this {
207
0
            DoOrDiscard(Either::Left(p)) => Some(p),
208
0
            DoOrDiscard(Either::Right(_)) => None,
209
        }
210
0
    }
211
}
212
213
impl<T> Count for DoOrDiscard<T>
214
where
215
    T: Count,
216
{
217
0
    fn set(&self, step: usize) {
218
0
        self.0.set(step)
219
0
    }
220
0
    fn step(&self) -> usize {
221
0
        self.0.step()
222
0
    }
223
224
0
    fn inc_by(&self, step: usize) {
225
0
        self.0.inc_by(step)
226
0
    }
227
228
0
    fn counter(&self) -> StepShared {
229
0
        self.0.counter()
230
0
    }
231
}
232
233
impl<T> Progress for DoOrDiscard<T>
234
where
235
    T: Progress,
236
{
237
0
    fn init(&mut self, max: Option<usize>, unit: Option<Unit>) {
238
0
        self.0.init(max, unit)
239
0
    }
240
241
0
    fn unit(&self) -> Option<Unit> {
242
0
        self.0.unit()
243
0
    }
244
245
0
    fn max(&self) -> Option<usize> {
246
0
        self.0.max()
247
0
    }
248
249
0
    fn set_max(&mut self, max: Option<Step>) -> Option<Step> {
250
0
        self.0.set_max(max)
251
0
    }
252
253
0
    fn set_name(&mut self, name: String) {
254
0
        self.0.set_name(name);
255
0
    }
256
257
0
    fn name(&self) -> Option<String> {
258
0
        self.0.name()
259
0
    }
260
261
0
    fn id(&self) -> Id {
262
0
        self.0.id()
263
0
    }
264
265
0
    fn message(&self, level: MessageLevel, message: String) {
266
0
        self.0.message(level, message)
267
0
    }
268
}
269
270
impl<T> NestedProgress for DoOrDiscard<T>
271
where
272
    T: NestedProgress,
273
{
274
    type SubProgress = DoOrDiscard<T::SubProgress>;
275
276
0
    fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
277
0
        DoOrDiscard(self.0.add_child(name))
278
0
    }
279
280
0
    fn add_child_with_id(&mut self, name: impl Into<String>, id: Id) -> Self::SubProgress {
281
0
        DoOrDiscard(self.0.add_child_with_id(name, id))
282
0
    }
283
}
284
285
use std::time::Instant;
286
287
use crate::progress::{Step, StepShared};
288
289
/// Emit a message with throughput information when the instance is dropped.
290
pub struct ThroughputOnDrop<T: NestedProgress>(T, Instant);
291
292
impl<T: NestedProgress> ThroughputOnDrop<T> {
293
    /// Create a new instance by providing the `inner` [`NestedProgress`] implementation.
294
0
    pub fn new(inner: T) -> Self {
295
0
        ThroughputOnDrop(inner, Instant::now())
296
0
    }
297
}
298
299
impl<T: NestedProgress> Count for ThroughputOnDrop<T> {
300
0
    fn set(&self, step: usize) {
301
0
        self.0.set(step)
302
0
    }
303
304
0
    fn step(&self) -> usize {
305
0
        self.0.step()
306
0
    }
307
308
0
    fn inc_by(&self, step: usize) {
309
0
        self.0.inc_by(step)
310
0
    }
311
312
0
    fn counter(&self) -> StepShared {
313
0
        self.0.counter()
314
0
    }
315
}
316
317
impl<T: NestedProgress> Progress for ThroughputOnDrop<T> {
318
0
    fn init(&mut self, max: Option<usize>, unit: Option<Unit>) {
319
0
        self.0.init(max, unit)
320
0
    }
321
322
0
    fn unit(&self) -> Option<Unit> {
323
0
        self.0.unit()
324
0
    }
325
326
0
    fn max(&self) -> Option<usize> {
327
0
        self.0.max()
328
0
    }
329
330
0
    fn set_max(&mut self, max: Option<Step>) -> Option<Step> {
331
0
        self.0.set_max(max)
332
0
    }
333
334
0
    fn set_name(&mut self, name: String) {
335
0
        self.0.set_name(name)
336
0
    }
337
338
0
    fn name(&self) -> Option<String> {
339
0
        self.0.name()
340
0
    }
341
342
0
    fn id(&self) -> Id {
343
0
        self.0.id()
344
0
    }
345
346
0
    fn message(&self, level: MessageLevel, message: String) {
347
0
        self.0.message(level, message)
348
0
    }
349
}
350
351
impl<T: NestedProgress> NestedProgress for ThroughputOnDrop<T> {
352
    type SubProgress = ThroughputOnDrop<T::SubProgress>;
353
354
0
    fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
355
0
        ThroughputOnDrop::new(self.0.add_child(name))
356
0
    }
357
358
0
    fn add_child_with_id(&mut self, name: impl Into<String>, id: Id) -> Self::SubProgress {
359
0
        ThroughputOnDrop::new(self.0.add_child_with_id(name, id))
360
0
    }
361
}
362
363
impl<T: NestedProgress> Drop for ThroughputOnDrop<T> {
364
0
    fn drop(&mut self) {
365
0
        self.0.show_throughput(self.1)
366
0
    }
367
}