From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3B33B28688E; Thu, 29 Jan 2026 14:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769697168; cv=none; b=G/hNfLQbZH+79D1119YBg/2481sF7STFLO16QpIXmVZrWO6UDDr0i7h02zcUse9ZeFTyTrs1W2GvQ8IHMMTp6MXvQmK3RvKS8dhYtCNAQOGhXJniH+K+j6YC4uTdIVAJ/AjkWjk9UhkNZkBBHFTbXhr70FsPVvU6GglOoFsNoNY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769697168; c=relaxed/simple; bh=W/lQNM+k2nJyyhsWP+iX2X75QmPDfFgQGv2yUuk2t+c=; h=From:Subject:Date:Message-Id:MIME-Version:Content-Type:To:Cc; b=sjTPH2s+yybo9594ESIenMDfA+UM/Juz1oiwafavMcwj+p/Czcn/RMZ7/q+QwrT9EBRn4OP4dDr8Kq2EOxSTm9tY1wdTPxNV0syFbF3RyVyYpEEihqREFiwyPOiV42Sduv0pgKI2IrpjiiFwt1ftvX1Bq9IhSaOI03sRkMopjo8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oL16AibZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oL16AibZ" Received: by smtp.kernel.org (Postfix) with ESMTPS id D5F13C4CEF7; Thu, 29 Jan 2026 14:32:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769697167; bh=W/lQNM+k2nJyyhsWP+iX2X75QmPDfFgQGv2yUuk2t+c=; h=From:Subject:Date:To:Cc:Reply-To:From; b=oL16AibZN0jHuPACubdWBmgmUVZazIWIHv+mzxXoDb324dpkhbgOCLEkt8AFgOpIq WWe7t5zolX6rkCMn9kER5xaPOWTnocEs8Z4xMZ0Zt5pWCDXCS2N2ED0Ek2CYrPc0FU xRVQE5+QtBZSm8ZvR6IGeTUifTJ4nPKbTjlJk3MkakBObiMEuR2qYY/ZH2oTsw+7nX 6RPuCVrlsA5wrrR0bhVJzVIzAPDwVyc/mcsemH+dyp6FEr4Ed/JdfLSt4RIXCny4Yp OabE2XV/iaEYOGI9Nwn9d5RswgvptVSTV736RVcdUc0NMDFMQsQ3DPAkASLZS8q0hx TQ9C3sLDFSV9Q== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5AF9D6101F; Thu, 29 Jan 2026 14:32:47 +0000 (UTC) From: Jesung Yang via B4 Relay Subject: [PATCH v5 0/4] rust: add `TryFrom` and `Into` derive macros Date: Thu, 29 Jan 2026 23:32:44 +0900 Message-Id: <20260129-try-from-into-macro-v5-0-dd011008118c@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAIxve2kC/32NQQqDMBBFryKz7hSNSWq76j2KixATHWiMTEQqk rs39QBdvgf//QOSY3IJHtUB7DZKFOcC6lKBncw8OqShMIhaqEYIhSvv6DkGpHmNGIzliI3Waqi Nt952UJYLO0+fs/rqC0+U1sj7ebLJn/3f2yTWKI3S7aDut67VzzEYel9tDNDnnL9JtcWwtgAAA A== X-Change-ID: 20251225-try-from-into-macro-1665d0afcfc8 To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Alexandre Courbot Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, nouveau@lists.freedesktop.org, Jesung Yang X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=5049; i=y.j3ms.n@gmail.com; h=from:subject:message-id; bh=W/lQNM+k2nJyyhsWP+iX2X75QmPDfFgQGv2yUuk2t+c=; b=owJ4nAFtApL9kA0DAAoBhnBYoOPQ2LoByyZiAGl7b47aya1K7Gld9Pr15f5ILWyBdh7l0GBPa 6HOxnu/aM43uYkCMwQAAQoAHRYhBNJuBqTTLsbEgOaQ0IZwWKDj0Ni6BQJpe2+OAAoJEIZwWKDj 0Ni6aioQALMVekoonpoiaF5wtL63jMIje/TKyxuKccuJJgghAmjDvYFM4Ij7lhMP9rjvb4NJ0rE BQw56rPhtTrad1s2AeJkx8Aie2GJd0fyrjwdSFUG3OrlAdZgqrVAP3irlL5ZJ+FK3FHr6nbgE18 JEyiO4/UF21wCOi1YaF9nWSjj2uLisRsPUdlDaAqHzgSHGVbahFctM0006DjSIZ8d3Mzv3ualFl KJubhQGMISfbbqAlc1iU/WTInrVhSpUuHDvk40aQhwdVyfBmsEK0UxybwYgsW6Z/FooynBug22r QXXSpKORx8PldH4mlr7smdXgfAdqOBs5oa0hUs+yrbfqozj7gw9RsNes+KdiSYRxVbrQhmwYtcm 7PFwGpYnazCtS1IwFYP3f2nuXgFlDGbNapgxI5qGObbnoe5DvWhMZA6owKOfC9KJ07+7mulQwts aCXS6J2y5yTIVLb4pSfUgrD9+MEWu8rvh8oW873hdc5Lq8qGOYe7KP+zXsXhEemm6BTkLxaKE9T VZI3lE85DzjmFW+sbbeo/Edf8/6yCl/iHgJS7j+P1Omf4bB6Rw9HdB+1J0ngAxBZQN8n3DiABQh 1cveTR6Nm29OnkogajCL53/BC+89P9xNdlOnBuEXCZAbokQ/yF/IRRzShLHx9UdViEPSpUYRe4q O1VgLCpp6xCsEOkH5dbAwfw== X-Developer-Key: i=y.j3ms.n@gmail.com; a=openpgp; fpr=D26E06A4D32EC6C480E690D0867058A0E3D0D8BA X-Endpoint-Received: by B4 Relay for y.j3ms.n@gmail.com/default with auth_id=602 X-Original-From: Jesung Yang Reply-To: y.j3ms.n@gmail.com This patch series introduces derive macros for the `TryFrom` and `Into` traits. I dropped support for `#[repr(C)]` fieldless enums (i.e., disallow deriving `TryFrom` and `Into` on those enums), since the actual representation of discriminants is defined by rustc internally, and documentation around it is not yet settled [1][2]. I would like to highlight some feedback from the previous series where I have decided to retain the original behavior in this series. In part, I feel more community feedback is needed to reach the right design; I've also elaborated on my personal reasoning for each point below: 1) Require explicit `#[repr(..)]` for enums using the macros. Given the drop of `#[repr(C)]` support, I believe defaulting to `isize` when no `#[repr(..)]` is specified is safe, as it follows the compiler's (documented) default behavior [3]. By "safe", I mean I see no potential regressions or hidden issues; we even have a compile-time overflow assertion which ensure all enum discriminants fit within the types involved in the conversion. 2) Generate trait implementation for a type in `#[repr(..)]` even when the helper attribute is present. For example: #[derive(TryFrom)] #[try_from(bool)] #[repr(u8)] enum E { A = 0, B = 1, } make the above snippet generate both `TryFrom` and `TryFrom` implementations. However, sometimes users might want to prevent generating trait implementations for the type in `#[repr(..)]`, especially when the enum encodes types like `bool` or `Bounded` that cannot be used in `#[repr(..)]`, like the above snippet. If we were to follow the feedback directly, users would end up with an unnecessary `TryFrom` implementation. Therefore, I think it is reasonable to treat the helper attribute as an override that ignores the type in `#[repr(..)]` when present. One last point: the current `Into` implementation relies on `Bounded::from_expr()`, which utilizes `build_assert!()`. So the expanded result looks roughly like this: impl From for Bounded { fn from(value: Enum) -> Bounded { // Compile-time assertions to guarantee `value` fits within // `u8` (Omitted) Bounded::::from_expr(value as u8) } } After some experimentation, it appears that the compiler correctly optimizes out the assertion if (and only if) the `Bounded` type covers all enum discriminants, though I'm not 100% certain of this behavior in all cases. Can we approach this better? Appreciate any feedback or suggestions. [1] https://github.com/rust-lang/rust/issues/124403 [2] https://github.com/rust-lang/rust/pull/147017 [3] https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.repr-rust Signed-off-by: Jesung Yang --- Changes in v5: - Drop support for `#[repr(C)]` enums. (Benno Lossin) - Allow `#[repr(C, {primitive_int_type})]` for future-proofing. (Benno Lossin) - Parse types in helper attributes into `syn::Type` prior to validation. (Benno Lossin) - Replace doctests for `TryFrom` with correct examples. (Benno Lossin) - Reorganize error reporting structure. - Add documentation about `#[repr(C)]` fieldless enums. - Rebase on commit a7c013f77953 ("Merge patch series "refactor Rust proc macros with `syn`"") - Link to v4: https://lore.kernel.org/r/20251225-try-from-into-macro-v4-0-4a563d597836@gmail.com Changes in v4: - Fix typos. - Link to (broken) v3: https://lore.kernel.org/rust-for-linux/cover.1766544407.git.y.j3ms.n@gmail.com/ Changes in v3: - Use the vendored `syn` and `quote` crates. - Support `kernel::num::Bounded`. - Add compile-time overflow assertion. - Add a comment about `#[repr(C)]` enums. - Drop Tested-by and Reviewed-by tags, as the code structure has changed substantially. (Thanks for the previous reviews and testing!) - Link to v2: https://lore.kernel.org/rust-for-linux/cover.1755235180.git.y.j3ms.n@gmail.com/ Changes in v2 (no functional changes): - Split the patch "rust: macros: extend custom `quote!()` macro" into two separate patches. - Remove unnecessary spaces between tags. - Use a consistent commit subject prefix: "rust: macros:". - Add Tested-by tags. - Link to v1: https://lore.kernel.org/rust-for-linux/cover.1754228164.git.y.j3ms.n@gmail.com/ --- Jesung Yang (4): rust: macros: add derive macro for `Into` rust: macros: add derive macro for `TryFrom` rust: macros: add private doctests for `Into` derive macro rust: macros: add private doctests for `TryFrom` derive macro rust/macros/convert.rs | 1599 ++++++++++++++++++++++++++++++++++++++++++++++++ rust/macros/lib.rs | 349 ++++++++++- 2 files changed, 1947 insertions(+), 1 deletion(-) --- base-commit: a7c013f779530190d0c1e1aa5e7c8a61f0bd479e change-id: 20251225-try-from-into-macro-1665d0afcfc8 Best regards, -- Jesung Yang