crevice/
internal.rs

1//! This module is internal to crevice but used by its derive macro. No
2//! guarantees are made about its contents.
3
4pub use bytemuck;
5
6/// Gives the number of bytes needed to make `offset` be aligned to `alignment`.
7pub const fn align_offset(offset: usize, alignment: usize) -> usize {
8    if alignment == 0 || offset % alignment == 0 {
9        0
10    } else {
11        alignment - offset % alignment
12    }
13}
14
15/// Max of two `usize`. Implemented because the `max` method from `Ord` cannot
16/// be used in const fns.
17pub const fn max(a: usize, b: usize) -> usize {
18    if a > b {
19        a
20    } else {
21        b
22    }
23}
24
25/// Max of an array of `usize`. This function's implementation is funky because
26/// we have no for loops!
27pub const fn max_arr<const N: usize>(input: [usize; N]) -> usize {
28    let mut max = 0;
29    let mut i = 0;
30
31    while i < N {
32        if input[i] > max {
33            max = input[i];
34        }
35
36        i += 1;
37    }
38
39    max
40}