1use patterns_macros::because;
2
3pub fn clamp_upper<T: PartialOrd>(v: T, hi: T) -> T {
4 if v > hi {
5 hi
6 } else {
7 v
8 }
9}
10
11pub fn clamp_lower<T: PartialOrd>(v: T, lo: T) -> T {
12 if v < lo {
13 lo
14 } else {
15 v
16 }
17}
18
19pub fn clamp_range<T: PartialOrd>(v: T, lo: T, hi: T) -> T {
20 clamp_upper(clamp_lower(v, lo), hi)
21}
22because!(clamp_upper, "a ceiling applied on its own, because a value with a maximum and no minimum is common enough to deserve its own name");
23because!(clamp_lower, "a floor applied on its own, because a value with a minimum and no maximum is as common as the other way round");
24because!(clamp_range, "both bounds at once, which is not clamp_lower composed with clamp_upper for the caller because writing it twice invites writing it in the wrong order");