1use super::errors::{Errno, errno};
6
7pub fn round_up_to_increment<N, M>(size: N, increment: M) -> Result<N, Errno>
8where
9 N: TryInto<u64>,
10 N: TryFrom<u64>,
11 M: TryInto<u64>,
12{
13 let size: u64 = size.try_into().map_err(|_| errno!(EINVAL))?;
14 let increment: u64 = increment.try_into().map_err(|_| errno!(EINVAL))?;
15 let spare = size % increment;
16 let result = if spare > 0 {
17 size.checked_add(increment - spare).ok_or_else(|| errno!(EINVAL))?
18 } else {
19 size
20 };
21 N::try_from(result).map_err(|_| errno!(EINVAL))
22}
23
24pub fn round_down_to_increment<N, M>(size: N, increment: M) -> Result<N, Errno>
25where
26 N: TryInto<u64>,
27 N: TryFrom<u64>,
28 M: TryInto<u64>,
29{
30 let size: u64 = size.try_into().map_err(|_| errno!(EINVAL))?;
31 let increment: u64 = increment.try_into().map_err(|_| errno!(EINVAL))?;
32 let result = size - (size % increment);
33 N::try_from(result).map_err(|_| errno!(EINVAL))
34}