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
58
59
60
61
62
use std::marker::PhantomData;
use mln_util::Color;
/// The value types a binder keeps statistics for: only `float` tracks a
/// maximum (the C++ `PaintPropertyStatistics<float>` specialization).
pub(crate) trait StatisticValue {
/// `Some(value)` for `float`, `None` for the types whose statistics are
/// the empty primary template.
fn statistic(&self) -> Option<f32>;
}
impl StatisticValue for f32 {
fn statistic(&self) -> Option<f32> {
Some(*self)
}
}
impl StatisticValue for Color {
fn statistic(&self) -> Option<f32> {
None
}
}
#[derive(Clone, Debug, mln_wire::Wire)]
pub(crate) struct PaintPropertyStatistics<T> {
max: Option<f32>,
#[wire(skip)]
_t: PhantomData<T>,
}
impl<T> Default for PaintPropertyStatistics<T> {
fn default() -> Self {
PaintPropertyStatistics { max: None, _t: PhantomData }
}
}
impl<T: StatisticValue> PaintPropertyStatistics<T> {
/// The largest value added; always `None` for value types other than
/// `f32`.
#[must_use]
pub(crate) fn max(&self) -> Option<f32> {
self.max
}
/// `add(value)`: `_max = _max ? std::max(*_max, value) : value`.
pub(crate) fn add(&mut self, value: &T) {
if let Some(value) = value.statistic() {
self.max = Some(match self.max {
// std::max(a, b) = (a < b) ? b : a
Some(max) => {
if max < value {
value
} else {
max
}
}
None => value,
});
}
}
}