Re: [PATCH v7 04/10] rust: sizes: implement SizeConstants for Alignment
"Eliot Courtney" <[email protected]>
| Newsgroups | org.kernel.vger.rust-for-linux,dev.linux.lists.nova-gpu,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
On Fri Aug 21, 2026 at 4:46 PM JST, Alexandre Courbot wrote:
> On Mon Aug 17, 2026 at 4:04 PM JST, Eliot Courtney wrote:
>> Currently, constructing an alignment is quite verbose:
>>
>> `Alignment::new::<8>()`
>>
>> It's unfortunate because it disincentivizes using it at interface
>> boundaries. Implement `SizeConstants` for `Alignment` and add some more
>> size constants (mirroring include/linux/sizes.h) so we can write e.g.
>> `Alignment::SZ_8` instead.
>
> These are two good ideas, but since they are orthogonal they ought to be
> in their own separate patch. :)
>
> <...>
>> @@ -99,13 +125,22 @@ macro_rules! define_sizes {
>> /// choose the width that matches their hardware. All `SZ_*` values fit
>> /// in a [`u32`], so all implementations are lossless.
>> ///
>> + /// Also implemented for [`Alignment`], providing each size as a
>> + /// compile-time validated alignment.
>> + ///
>> + /// [`Alignment`]: crate::ptr::Alignment
>> + ///
>> /// # Examples
>> ///
>> /// ```
>> - /// use kernel::sizes::SizeConstants;
>> + /// use kernel::{
>> + /// ptr::Alignment,
>> + /// sizes::SizeConstants, //
>> + /// };
>> ///
>> /// let gpu_heap = 14 * u64::SZ_1M;
>> /// let mmio_window = u32::SZ_16M;
>> + /// let page_align = Alignment::SZ_4K;
>> /// ```
>> pub trait SizeConstants {
>> $(
>> @@ -117,6 +152,16 @@ pub trait SizeConstants {
>>
>> (@impls [] $($(#[$meta:meta])* $name:ident,)*) => {};
>>
>> + (@impl_alignment $($(#[$meta:meta])* $name:ident,)*) => {
>> + impl SizeConstants for crate::ptr::Alignment {
>> + $(
>> + $(#[$meta])*
>> + // A non-power-of-two constant will fail the build here if used.
>> + const $name: Self = crate::ptr::Alignment::new_checked(self::$name).unwrap();
>
> Since this is a const, you can use:
>
> const $name: Self = crate::ptr::Alignment::new::<{ self::$name }>();
>
> Which we should prefer over using `unwrap`, even compile-time checked ones.
Will do both of these, thanks~