From patchwork Wed Mar 26 17:13:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Remo Senekowitsch X-Patchwork-Id: 14030392 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02972202F93; Wed, 26 Mar 2025 17:23:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743009807; cv=none; b=olvEwnyS0GIfdoHej4j+/8/hygLy++V/voCKZdABy2jkgZHDFI1BkQ813TsoaIAsij/BJ4NW8/m3c6d/zfJSCrzfOs0Uzr0Rlmg/SccTMA4GR+BWIybIh1LWbIC/7UgPQqhVWglkt1n/sjJ1rXPwZfY3nLbTaDBaVdDAeHnjY4g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743009807; c=relaxed/simple; bh=rs9bOE/ilaGLGVSwM1Ef1ijs+11j18W4WaeUmdZRUUo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SLZ9WzrwboipooHn3uO5C4vLkKyo+6ypxwPg5T+e2SdopmJq1WGhmCIMmnnRiqfCUu/gUfM6BbmEN721ExRatk3N244PuB6wZj0+Rv7IVPVKvu87rI0uEY6JcAuEJ9dUKOSywSX+uFAoKomHddXzrRN+cTO5mQojW9uH0prakaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=buenzli.dev; spf=pass smtp.mailfrom=buenzli.dev; arc=none smtp.client-ip=80.241.56.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=buenzli.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=buenzli.dev Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4ZND1N0Jbqz9smk; Wed, 26 Mar 2025 18:14:48 +0100 (CET) From: Remo Senekowitsch To: Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , Rob Herring Cc: Dirk Behme , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Saravana Kannan , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Remo Senekowitsch , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, devicetree@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH 02/10] rust: Add an Integer trait Date: Wed, 26 Mar 2025 18:13:41 +0100 Message-ID: <20250326171411.590681-3-remo@buenzli.dev> In-Reply-To: <20250326171411.590681-1-remo@buenzli.dev> References: <20250326171411.590681-1-remo@buenzli.dev> Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Queue-Id: 4ZND1N0Jbqz9smk From: "Rob Herring (Arm)" 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 Signed-off-by: Rob Herring (Arm) --- 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 { 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::(); + } + )*}; +} + +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.