Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl ToTokens for syn::Error and syn::Result<T: ToTokens>? #1784

Closed
marcianx opened this issue Nov 21, 2024 · 1 comment
Closed

Impl ToTokens for syn::Error and syn::Result<T: ToTokens>? #1784

marcianx opened this issue Nov 21, 2024 · 1 comment

Comments

@marcianx
Copy link

I had a foo: syn::Result<Expr> and wrote:

foo
    .map(ToTokens::into_token_stream)
    .unwrap_or_else(syn::Error::into_compile_error)
    .into()

and it made me wonder:

  1. Why can't we just do quote! { #foo } given that syn::Error has an idiomatic conversion to a TokenStream -- IOW, implement ToTokens for syn::Result<T: ToTokens>.
  2. Does it make sense to also implement ToTokens for syn::Error?

Thoughts?

@dtolnay
Copy link
Owner

dtolnay commented Dec 1, 2024

I think this would be a similar design mistake as Serde implementing Serialize and Deserialize for Result<T, E>. The way it would play out in practice is the intentional uses would be rare and the mistaken uses (someone wrote too few ?) would be rampant and hard to identify. I think .unwrap_or_else(syn::Error::into_compile_error) is the right level of easy and clear for the cases where this is the behavior someone wants.

@dtolnay dtolnay closed this as completed Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants