1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Render reporting.
use std::time::{Duration, Instant};
/// Stores report state.
pub enum ReportState {
/// No state.
None,
/// The state for reporting every Nth row.
Row(u32),
/// The last instant been reported.
Duration(Instant),
}
/// Specifies report settings when rendering.
#[derive(Copy, Clone)]
pub enum Report {
/// No report.
None,
/// Report every Nth row.
Row(u32),
/// Report every duration.
Duration(Duration),
}
impl Report {
/// Gets the start state of reporting.
pub fn start(&self) -> ReportState {
match self {
Report::None => ReportState::None,
Report::Row(_) => ReportState::Row(0),
Report::Duration(_) => ReportState::Duration(Instant::now()),
}
}
/// Update the report state and return `true` if should report.
pub fn update(&self, state: &mut ReportState, row: u32) -> bool {
use ReportState::*;
match (self, state) {
(Report::None, None) => false,
(Report::Row(n), Row(last)) => {
if row >= *last + n {
*last += n;
true
} else {false}
}
(Report::Duration(dur), Duration(last)) => {
let now = Instant::now();
if now >= *last + *dur {
*last = now;
true
} else {false}
}
_ => false,
}
}
}