[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DF7HEMI7I3LS.FW1IPV2QS2HB@kernel.org>
Date: Thu, 25 Dec 2025 18:42:31 +0100
From: "Benno Lossin" <lossin@...nel.org>
To: <y.j3ms.n@...il.com>, "Miguel Ojeda" <ojeda@...nel.org>, "Boqun Feng"
<boqun.feng@...il.com>, "Gary Guo" <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
"Trevor Gross" <tmgross@...ch.edu>, "Danilo Krummrich" <dakr@...nel.org>,
"Alexandre Courbot" <acourbot@...dia.com>
Cc: <linux-kernel@...r.kernel.org>, <rust-for-linux@...r.kernel.org>,
<nouveau@...ts.freedesktop.org>
Subject: Re: [PATCH v4 2/4] rust: macros: add derive macro for `TryFrom`
On Thu Dec 25, 2025 at 9:37 AM CET, Jesung Yang via B4 Relay wrote:
> +/// ## Compile-time Overflow Assertion
> +///
> +/// The following examples do not compile:
> +///
> +/// ```compile_fail
> +/// # use kernel::macros::Into;
> +/// #[derive(Into)]
> +/// #[into(u8)]
> +/// enum Foo {
> +/// // `256` is larger than `u8::MAX`.
> +/// A = 256,
> +/// }
> +/// ```
> +///
> +/// ```compile_fail
> +/// # use kernel::macros::Into;
> +/// #[derive(Into)]
> +/// #[into(u8)]
> +/// enum Foo {
> +/// // `-1` cannot be represented with `u8`.
> +/// A = -1,
> +/// }
> +/// ```
These two are copy-pasted from the `into` macro.
Cheers,
Benno
> +///
> +/// ## Unsupported Cases
> +///
> +/// The following examples do not compile:
> +///
> +/// ```compile_fail
> +/// # use kernel::macros::TryFrom;
> +/// // Tuple-like enums or struct-like enums are not allowed.
> +/// #[derive(TryFrom)]
> +/// enum Foo {
> +/// A(u8),
> +/// B { inner: u8 },
> +/// }
> +/// ```
> +///
> +/// ```compile_fail
> +/// # use kernel::macros::TryFrom;
> +/// // Structs are not allowed.
> +/// #[derive(TryFrom)]
> +/// struct Foo(u8);
> +/// ```
> +#[proc_macro_derive(TryFrom, attributes(try_from))]
> +pub fn derive_try_from(input: TokenStream) -> TokenStream {
> + let input = parse_macro_input!(input as DeriveInput);
> + convert::derive_try_from(input)
> + .unwrap_or_else(syn::Error::into_compile_error)
> + .into()
> +}
Powered by blists - more mailing lists