Improvements to generic alignment function?

I've just written a generic alignment function, and I'm not too happy with the val % val, so wondering if anybody has any ideas for obvious improvements. (Preferably improvements that don't bring in any additional dependencies.)

/// Returns the next value greater than or equal to `val` that is divisible by `alignment`.
pub fn aligned<T>(val: T, alignment: T) -> T 
    where T: Add<T, Output=T> 
        + Mul<T, Output=T>
        + Div<T, Output=T>
        + Rem<T, Output=T>
        + PartialOrd
        + Copy
{
    let zero = val % val;

    alignment * (val / alignment)
        + if val % alignment > zero { alignment } else { zero }
}

EDIT:
I think I've got something better. I walked away thinking "It's basically just an an inverted remainder..." and I'm pretty sure this is the standard way to do it. It doesn't help to be tired.

pub fn aligned<T>(val: T, alignment: T) -> T 
    where T: Add<T, Output=T> 
        + Mul<T, Output=T>
        + Sub<T, Output=T>
        + Rem<T, Output=T>
        + Copy
{
    val + ((alignment - (val % alignment)) % alignment)
}

If your function does what it should, I think it's fine. Optimizing code, that doesn't need it is considered bad practice, unless it also improves maintainability. My only advice is, add the #[inline] attribute to the function.

New copies of a generic function are created in the caller's crate where LLVM can see the implementation. So it's effectively already #[inline].

You should look inti num_traits, to generalize over numbers

You're right. I dug a bit around and found this bit of helpful documentation:

You shouldn't need #[inline] :

  • On methods that have any generics in scope.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.