wahgex_core/
util.rs

1use std::alloc::{Layout, LayoutError};
2
3const DEFAULT_ERROR: LayoutError = const {
4    match Layout::from_size_align(0, 3) {
5        Ok(_) => panic!("expected err"),
6        Err(err) => err,
7    }
8};
9
10pub const fn repeat(layout: &Layout, n: usize) -> Result<(Layout, usize), LayoutError> {
11    let padded = layout.pad_to_align();
12    if let Ok(repeated) = repeat_packed(&padded, n) {
13        Ok((repeated, padded.size()))
14    } else {
15        Err(DEFAULT_ERROR)
16    }
17}
18
19const fn repeat_packed(layout: &Layout, n: usize) -> Result<Layout, LayoutError> {
20    if let Some(size) = layout.size().checked_mul(n) {
21        // The safe constructor is called here to enforce the isize size limit.
22        Layout::from_size_align(size, layout.align())
23    } else {
24        Err(DEFAULT_ERROR)
25    }
26}