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
63
64
65
66
67
68
69
70
use std::fmt;
use std::ops::Sub;
pub struct Bound<T> {
pub min: T,
pub max: T,
}
impl<T> Bound<T> where T: Copy + Sub<Output=T> {
pub fn dif(&self) -> T { self.max - self.min }
}
impl<T> Bound<T> where T: Copy {
pub fn new(min: T, max: T) -> Self { Bound { min, max } }
}
impl<T> Bound<T> where T: Default {
pub fn default() -> Self { Bound { min: T::default(), max: T::default() } }
}
impl<T> Bound<T> where T: Copy + PartialOrd {
pub fn expand(&mut self, v: &T)
{ if v < &self.min { self.min = *v } else if v > &self.max { self.max = *v } }
}
impl<T> fmt::Display for Bound<T> where T: fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Bound {{ min: {}, max: {} }}", self.min, self.max)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let bound_alpha = Bound::new(5, 7);
println!("{}, dif = {}", bound_alpha, bound_alpha.dif());
}
}