reth_stages_api/
util.rs

1pub(crate) mod opt {
2    /// Get an [Option] with the maximum value, compared between the passed in value and the inner
3    /// value of the [Option]. If the [Option] is `None`, then an option containing the passed in
4    /// value will be returned.
5    pub(crate) fn max<T: Ord + Copy>(a: Option<T>, b: T) -> Option<T> {
6        a.map_or(Some(b), |v| Some(std::cmp::max(v, b)))
7    }
8
9    /// Get an [Option] with the minimum value, compared between the passed in value and the inner
10    /// value of the [Option]. If the [Option] is `None`, then an option containing the passed in
11    /// value will be returned.
12    pub(crate) fn min<T: Ord + Copy>(a: Option<T>, b: T) -> Option<T> {
13        a.map_or(Some(b), |v| Some(std::cmp::min(v, b)))
14    }
15
16    #[cfg(test)]
17    mod tests {
18        use super::*;
19
20        #[test]
21        fn opt_max() {
22            assert_eq!(max(None, 5), Some(5));
23            assert_eq!(max(Some(1), 5), Some(5));
24            assert_eq!(max(Some(10), 5), Some(10));
25        }
26
27        #[test]
28        fn opt_min() {
29            assert_eq!(min(None, 5), Some(5));
30            assert_eq!(min(Some(1), 5), Some(1));
31            assert_eq!(min(Some(10), 5), Some(5));
32        }
33    }
34}