Rust for Linux List
 help / color / mirror / Atom feed
From: Gary Guo <gary@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: [PATCH] rust: mem: add `Align` type
Date: Tue,  9 Jun 2026 13:03:24 +0100	[thread overview]
Message-ID: <20260609120325.233721-1-gary@kernel.org> (raw)

From: Gary Guo <gary@garyguo.net>

Rust's `repr(align())` only works with integers. Add an `Align` type so it
can also be specified via const generics.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/kernel/lib.rs |  1 +
 rust/kernel/mem.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 rust/kernel/mem.rs

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 9512af7156df..0a12c3011c61 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -92,6 +92,7 @@
 pub mod kunit;
 pub mod list;
 pub mod maple_tree;
+pub mod mem;
 pub mod miscdevice;
 pub mod mm;
 pub mod module_param;
diff --git a/rust/kernel/mem.rs b/rust/kernel/mem.rs
new file mode 100644
index 000000000000..32e2fe547cfd
--- /dev/null
+++ b/rust/kernel/mem.rs
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Utilities handling things related to memory layouts.
+
+// TODO: `Alignment` should be moved here as well, Rust moved it upstream in 1.96.
+pub use crate::ptr::Alignment;
+
+/// Trait indicating that [`Align<N>`] is a valid alignment.
+pub trait ValidAlign {
+    #[doc(hidden)]
+    type Repr;
+}
+
+/// Zero-sized type that provides the alignment specified via the generic parameter.
+///
+/// # Examples
+///
+/// ```
+/// # use kernel::mem::Align;
+/// const ALIGN: usize = 128;
+/// struct MyStruct {
+///     align: Align<ALIGN>,
+/// }
+/// assert_eq!(align_of::<MyStruct>(), ALIGN);
+/// ```
+#[repr(transparent)]
+#[derive(Default)]
+pub struct Align<const N: usize>([<Self as ValidAlign>::Repr; 0])
+where
+    Self: ValidAlign;
+
+impl<const N: usize> Align<N>
+where
+    Self: ValidAlign,
+{
+    /// Create a new [`Align<N>`].
+    #[inline]
+    pub const fn new() -> Self {
+        Align([])
+    }
+}
+
+macro_rules! impl_align {
+    () => {};
+    ($a:literal $($rest:literal)*) => {
+        const _: () = {
+            #[repr(align($a))]
+            pub struct Repr;
+
+            impl ValidAlign for Align<$a> {
+                type Repr = Repr;
+            }
+        };
+
+        impl_align!($($rest)*);
+    }
+}
+
+impl_align!(1 2 4 8 16 32 64 128 256 512 1024 2048 4096);

base-commit: a87737435cfa134f9cdcc696ba3080759d04cf72
prerequisite-patch-id: 0000000000000000000000000000000000000000
-- 
2.54.0


             reply	other threads:[~2026-06-09 12:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-09 12:03 Gary Guo [this message]
     [not found] ` <20260609121103.385001F00898@smtp.kernel.org>
     [not found]   ` <CANiq72kAvoGgG2jG+bHs56z0C=Gk6UUHKQTcuTAK4+WqnVePzg@mail.gmail.com>
2026-06-09 13:42     ` [PATCH] rust: mem: add `Align` type Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260609120325.233721-1-gary@kernel.org \
    --to=gary@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox