Skip to main content

nekolib/utils/
make_minmax.rs

1pub trait MakeMin: PartialOrd + Sized {
2    fn make_min(&mut self, other: Self) -> bool {
3        let tmp = *self > other;
4        if tmp {
5            *self = other;
6        }
7        tmp
8    }
9}
10
11pub trait MakeMax: PartialOrd + Sized {
12    fn make_max(&mut self, other: Self) -> bool {
13        let tmp = *self < other;
14        if tmp {
15            *self = other;
16        }
17        tmp
18    }
19}
20
21impl<T: PartialOrd + Sized> MakeMin for T {}
22impl<T: PartialOrd + Sized> MakeMax for T {}
23
24#[test]
25fn test() {
26    let mut a = 10_i32;
27    a.make_max(20);
28    assert_eq!(a, 20);
29    a.make_max(15);
30    assert_eq!(a, 20);
31    a.make_min(10);
32    assert_eq!(a, 10);
33    a.make_min(15);
34    assert_eq!(a, 10);
35}