Debian Codegraph @

third_party/rust/wgpu-types/src/math.rs

raw
1 //! Utilitary math functions.
2
3 use core::ops::{Add, Rem, Sub};
4
5 ///
6 /// Aligns a `value` to an `alignment`.
7 ///
8 /// Returns the first number greater than or equal to `value` that is also a
9 /// multiple of `alignment`. If `value` is already a multiple of `alignment`,
10 /// `value` will be returned.
11 ///
12 /// # Examples
13 ///
14 /// ```
15 /// # use wgpu_types::math::align_to;
16 /// assert_eq!(align_to(253, 16), 256);
17 /// assert_eq!(align_to(256, 16), 256);
18 /// assert_eq!(align_to(0, 16), 0);
19 /// ```
20 ///
21 pub fn align_to<T>(value: T, alignment: T) -> T
22 where
23 T: Add<Output = T> + Copy + Default + PartialEq<T> + Rem<Output = T> + Sub<Output = T>,
24 {
25 let remainder = value % alignment;
26 if remainder == T::default() {
27 value
28 } else {
29 value + alignment - remainder
30 }
31 }