[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250326171411.590681-3-remo@buenzli.dev>
Date: Wed, 26 Mar 2025 18:13:41 +0100
From: Remo Senekowitsch <remo@...nzli.dev>
To: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Daniel Scally <djrscally@...il.com>,
Heikki Krogerus <heikki.krogerus@...ux.intel.com>,
Sakari Ailus <sakari.ailus@...ux.intel.com>,
Rob Herring <robh@...nel.org>
Cc: Dirk Behme <dirk.behme@...bosch.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>,
Danilo Krummrich <dakr@...nel.org>,
Saravana Kannan <saravanak@...gle.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Remo Senekowitsch <remo@...nzli.dev>,
linux-kernel@...r.kernel.org,
linux-acpi@...r.kernel.org,
devicetree@...r.kernel.org,
rust-for-linux@...r.kernel.org
Subject: [PATCH 02/10] rust: Add an Integer trait
From: "Rob Herring (Arm)" <robh@...nel.org>
Add an "Integer" trait similar to crate::num::Integer. This is useful
for implementing generic methods which operate on different sizes of
integers. One example is reading DT/ACPI firmware properties.
This was originally proposed by Alice Ryhl[1].
[1] https://lore.kernel.org/rust-for-linux/CAH5fLgiXPZqKpWSSNdx-Ww-E9h2tOLcF3_8Y4C_JQ0eU8EMwFw@mail.gmail.com/
Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
Signed-off-by: Rob Herring (Arm) <robh@...nel.org>
---
rust/kernel/types.rs | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 2bbaab83b..21647b7ba 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -3,10 +3,11 @@
//! Kernel types.
use crate::init::{self, PinInit};
+use crate::transmute::{AsBytes, FromBytes};
use core::{
cell::UnsafeCell,
marker::{PhantomData, PhantomPinned},
- mem::{ManuallyDrop, MaybeUninit},
+ mem::{size_of, ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
ptr::NonNull,
};
@@ -553,6 +554,25 @@ pub enum Either<L, R> {
Right(R),
}
+/// Trait defined for all integer types similar to `crate::num::Integer`
+pub trait Integer: FromBytes + AsBytes + Copy {
+ /// Size of the integer in bytes
+ const SIZE: usize;
+}
+
+macro_rules! impl_int {
+ ($($typ:ty),* $(,)?) => {$(
+ impl Integer for $typ {
+ const SIZE: usize = size_of::<Self>();
+ }
+ )*};
+}
+
+impl_int! {
+ u8, u16, u32, u64, usize,
+ i8, i16, i32, i64, isize,
+}
+
/// Zero-sized type to mark types not [`Send`].
///
/// Add this type as a field to your struct if your type should not be sent to a different task.
--
2.49.0
Powered by blists - more mailing lists