gilrs/
utils.rs

1// Copyright 2016-2018 Mateusz Sieczko and other GilRs Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8pub use gilrs_core::utils::*;
9
10/// Like `(a: f32 / b).ceil()` but for integers.
11pub fn ceil_div(a: u32, b: u32) -> u32 {
12    if a == 0 {
13        0
14    } else {
15        1 + ((a - 1) / b)
16    }
17}
18
19pub fn clamp(x: f32, min: f32, max: f32) -> f32 {
20    x.clamp(min, max)
21}
22
23#[cfg(path_separator = "backslash")]
24macro_rules! PATH_SEPARATOR {
25    () => {
26        r"\"
27    };
28}
29
30#[cfg(path_separator = "slash")]
31macro_rules! PATH_SEPARATOR {
32    () => {
33        r"/"
34    };
35}
36
37pub(crate) use PATH_SEPARATOR;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn t_clamp() {
45        assert_eq!(clamp(-1.0, 0.0, 1.0), 0.0);
46        assert_eq!(clamp(0.5, 0.0, 1.0), 0.5);
47        assert_eq!(clamp(2.0, 0.0, 1.0), 1.0);
48    }
49}