irox_tools/primitives/
u64.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2023 IROX Contributors
3
4//!
5//! A collection of utilities for the u64 built-in
6//!
7
8use crate::{ToF64, WrappingAdd, WrappingMul, WrappingSub};
9
10///
11/// Finds the minimum and maximum value in the provided iterator.
12/// Example:
13/// ```
14/// let values : Vec<u64> = vec![0, 5, 30, 20, 2];
15/// let (min, max) = irox_tools::u64::min_max(&values);
16///
17/// assert_eq!(min, 0);
18/// assert_eq!(max, 30);
19/// ```
20#[must_use]
21pub fn min_max(iter: &[u64]) -> (u64, u64) {
22    let mut min = u64::MAX;
23    let mut max = u64::MIN;
24
25    for val in iter {
26        min = min.min(*val);
27        max = max.max(*val);
28    }
29
30    (min, max)
31}
32
33impl WrappingAdd for u64 {
34    fn wrapping_add(&self, rhs: Self) -> Self {
35        u64::wrapping_add(*self, rhs)
36    }
37}
38impl WrappingSub for u64 {
39    fn wrapping_sub(&self, rhs: Self) -> Self {
40        u64::wrapping_sub(*self, rhs)
41    }
42}
43impl WrappingMul for u64 {
44    fn wrapping_mul(&self, rhs: Self) -> Self {
45        u64::wrapping_mul(*self, rhs)
46    }
47}
48impl WrappingAdd for i64 {
49    fn wrapping_add(&self, rhs: Self) -> Self {
50        i64::wrapping_add(*self, rhs)
51    }
52}
53impl WrappingSub for i64 {
54    fn wrapping_sub(&self, rhs: Self) -> Self {
55        i64::wrapping_sub(*self, rhs)
56    }
57}
58impl WrappingMul for i64 {
59    fn wrapping_mul(&self, rhs: Self) -> Self {
60        i64::wrapping_mul(*self, rhs)
61    }
62}
63
64impl ToF64 for u64 {
65    fn to_f64(&self) -> f64 {
66        *self as f64
67    }
68}
69impl ToF64 for i64 {
70    fn to_f64(&self) -> f64 {
71        *self as f64
72    }
73}