Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: mem: add `Align` type
@ 2026-06-09 12:03 Gary Guo
       [not found] ` <20260609121103.385001F00898@smtp.kernel.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Gary Guo @ 2026-06-09 12:03 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: linux-kernel, rust-for-linux

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-06-09 13:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-09 12:03 [PATCH] rust: mem: add `Align` type Gary Guo
     [not found] ` <20260609121103.385001F00898@smtp.kernel.org>
     [not found]   ` <CANiq72kAvoGgG2jG+bHs56z0C=Gk6UUHKQTcuTAK4+WqnVePzg@mail.gmail.com>
2026-06-09 13:42     ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox