Skip to contentSkip to conversation

rust: mem: add `Align` type

PATCH In review Gary Guo 2 participants 3 messages Jun 9 1 message not in the archive Rust

lore
Share & tools
lore

Gary Guo

author
#0 lore raw

PATCH 1/1 rust: mem: add `Align` type

From: Gary Guo <[email protected]>

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

---
 rust/kernel/lib.rs |  1 +
 rust/kernel/mem.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 rust/kernel/mem.rs
rust/kernel/lib.rs +1 -0
index 9512af7156df..0a12c3011c61 100644
@@ -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;
rust/kernel/mem.rs new file +59 -0

59 lines not on this page · raw message

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

Signature · 1 line
2.54.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 14 lines · On Tue, Jun 9, 2026 at 2:11 PM <[email protected]> wrote:

rust/kernel/mem.rs line 24

Quoted 10 lines
rust/kernel/mem.rs new file +9 -0
+/// # Examples
+///
+/// ```
+/// # use kernel::mem::Align;
+/// const ALIGN: usize = 128;
+/// struct MyStruct {
+///     align: Align<ALIGN>,
+/// }
+/// assert_eq!(align_of::<MyStruct>(), ALIGN);

[Severity: Low]
Does this doctest fail to compile during documentation testing?

Quoted 3 lines

Because align_of is a function from core::mem and is not in the standard Rust prelude, it looks like it needs an explicit import, such as

# use core::mem::align_of; to avoid a build failure.

Ok, I added a section about the prelude for Sashiko:

https://github.com/masoncl/review-prompts/pull/75

I also noticed we can clean up the prelude nowadays, i.e. essentially undo commit 72b04a8af7fb ("rust: prelude: re-export core::mem::{align,size}_of{,_val}") and clean a few other cases. I will send a quick patch.

Cheers,
Miguel

#3 lore raw
Quoted 20 lines · Miguel Ojeda
Quoted 14 lines · On Tue, Jun 9, 2026 at 2:11 PM <[email protected]> wrote:

rust/kernel/mem.rs line 24

Quoted 10 lines
rust/kernel/mem.rs new file +9 -0
+/// # Examples
+///
+/// ```
+/// # use kernel::mem::Align;
+/// const ALIGN: usize = 128;
+/// struct MyStruct {
+///     align: Align<ALIGN>,
+/// }
+/// assert_eq!(align_of::<MyStruct>(), ALIGN);

[Severity: Low]
Does this doctest fail to compile during documentation testing?

Quoted 3 lines

Because align_of is a function from core::mem and is not in the standard Rust prelude, it looks like it needs an explicit import, such as

# use core::mem::align_of; to avoid a build failure.
Quoted 8 lines

Ok, I added a section about the prelude for Sashiko:

https://github.com/masoncl/review-prompts/pull/75

I also noticed we can clean up the prelude nowadays, i.e. essentially undo commit 72b04a8af7fb ("rust: prelude: re-export core::mem::{align,size}_of{,_val}") and clean a few other cases. I will send a quick patch.

Cc'ing the list too.

Cheers,
Miguel