Re: [PATCH v5 21/38] rust: ptr: add const_align_up() and enable inline_const feature

From: Alexandre Courbot

Date: Wed Mar 04 2026 - 20:24:17 EST


On Thu Mar 5, 2026 at 4:14 AM JST, John Hubbard wrote:
> On 3/4/26 11:04 AM, Gary Guo wrote:
>> On Wed Mar 4, 2026 at 6:53 PM GMT, John Hubbard wrote:
>>> On 3/4/26 3:18 AM, Gary Guo wrote:
>>>> On Wed Mar 4, 2026 at 3:47 AM GMT, John Hubbard wrote:
>>> ...
>>> +#[inline(always)]
>>> +pub const fn const_align_up<const ALIGN: usize>(value: usize) -> Option<usize> {
>>> + const { assert!(ALIGN.is_power_of_two(), "ALIGN must be a power of two") };
>>> + match value.checked_add(ALIGN - 1) {
>>> + Some(v) => Some(v & !(ALIGN - 1)),
>>> + None => None,
>>> + }
>>> +}
>>
>> I think your signature should probably just be
>>
>> pub const fn const_align_up(value: usize, align: Alignment) -> Option<usize> {
>> ...
>> }
>>
>
> OK yes that's a bit nicer. I've done that for v6, thanks!

Hold on a bit - if we are purposing this new method for use in const
contexts, what use do we have for a `None` return value? By definition
we would know both `value` and `align` and thus the result is
deterministic.

We do have an alignment method for non-const contexts already. Gary's
initial comment was:

> Either this function is always used in const context, in which case
> you take `ALIGN` as normal function parameter and use `build_assert` and
> `build_error`

So why not make both arguments generic in this new method, and fail at
build in case of overflow?