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
#![no_std]
use core::{cmp::PartialOrd, time::Duration};
pub fn clamp<T>(value: T, min: T, max: T) -> T
where
T: PartialOrd,
{
assert!(min <= max);
if value < min {
min
} else if value > max {
max
} else {
value
}
}
pub enum SlotTime {
UserSpecified(Duration),
AutoGenerated(Duration),
}
#[cfg(test)]
mod tests {
use super::*;
mod clamp {
use super::clamp;
mod i32 {
use super::clamp;
#[test]
fn inside_range() {
assert_eq!(clamp(1_i32, 0_i32, 2_i32), 1_i32);
}
#[test]
fn below_range() {
assert_eq!(clamp(-1_i32, 0_i32, 2_i32), 0_i32);
}
#[test]
fn above_range() {
assert_eq!(clamp(3_i32, 0_i32, 2_i32), 2_i32);
}
}
#[test]
#[should_panic]
fn panics_if_not_ordered_properly() {
clamp(1_i32, 2_i32, 0_i32);
}
}
}