On Sun Jan 11, 2026 at 12:25 PM GMT, Benno Lossin wrote:
> Add a function to turn a `syn::Result<TokenStream>` into a
> `TokenStream`, either containing the error or the normal stream.
>
> Add a nullable custom error type wrapping `syn::Error`.
>
> Signed-off-by: Benno Lossin <lossin@kernel.org>
> ---
> Changes in v2: added this patch
> ---
> rust/pin-init/internal/src/lib.rs | 44 +++++++++++++++++++++++++++++++
> 1 file changed, 44 insertions(+)
>
> diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs
> index 4c4dc639ce82..21f5e33486ce 100644
> --- a/rust/pin-init/internal/src/lib.rs
> +++ b/rust/pin-init/internal/src/lib.rs
> @@ -36,3 +36,47 @@ pub fn derive_zeroable(input: TokenStream) -> TokenStream {
> pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
> zeroable::maybe_derive(input.into()).into()
> }
> +
> +#[expect(dead_code)]
> +fn ok_or_compile_error(res: syn::Result<proc_macro2::TokenStream>) -> TokenStream {
> + match res {
> + Ok(stream) => stream,
> + Err(error) => error.into_compile_error(),
> + }
> + .into()
> +}
> +
> +pub(crate) struct Error(Option<syn::Error>);
I don't think it's a good idea to call a type `Error` when it could just be no
error.
I think what I propose in https://lore.kernel.org/all/DFK7ITVQ97RL.2SZ2ANDIQ39H3@garyguo.net
is better than what's implemented here.
Best,
Gary
> +
> +impl From<syn::Error> for Error {
> + fn from(value: syn::Error) -> Self {
> + Self(Some(value))
> + }
> +}
> +
> +impl Error {
> + #[expect(dead_code)]
> + pub(crate) fn none() -> Self {
> + Self(None)
> + }
> +
> + #[expect(dead_code)]
> + pub(crate) fn combine(&mut self, error: impl Into<Self>) {
> + let error = error.into();
> + if let Some(this) = self.0.as_mut() {
> + if let Some(error) = error.0 {
> + this.combine(error);
> + }
> + } else {
> + self.0 = error.0;
> + }
> + }
> +}
> +
> +impl quote::ToTokens for Error {
> + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
> + if let Some(error) = self.0.as_ref() {
> + quote::ToTokens::to_tokens(&error.to_compile_error(), tokens);
> + }
> + }
> +}