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

Convenience trait implementations on Ident #93

Closed
hcpl opened this issue May 21, 2018 · 6 comments
Closed

Convenience trait implementations on Ident #93

hcpl opened this issue May 21, 2018 · 6 comments

Comments

@hcpl
Copy link
Contributor

hcpl commented May 21, 2018

Should Ident from proc-macro2 0.4 have following trait implementations like Ident from syn 0.13?

  • From<&'a str>;
  • From<Cow<'a, str>>;
  • From<String>;
  • AsRef<str> (however this one makes impl PartialEq<T: AsRef<str> + ?Sized> conflict with impl PartialEq).

Also in case of the last one, is a quote from: rust-lang/rust#50473

  • Removed APIs: Ident::as_str, use Display impl for Ident instead.

relevant here (as in Ident::as_str being equivalent to <Ident as AsRef<str>>::as_ref)?

@alexcrichton
Copy link
Contributor

The ones missing now are From and it's an explicit design decision of proc_macro the crate to always require spans to be specified when creating an Ident. I'd like to stick with that for now before we add From implementations which implicitly choose a span for you

@dtolnay
Copy link
Owner

dtolnay commented May 21, 2018

Regarding AsRef: the compiler folks are not comfortable committing to an API that does &Ident ⟶ &str. rust-lang/rust#38356 (comment) explains.

From: over time these spans will get more important and we would prefer to require them explicitly.

@dtolnay dtolnay closed this as completed May 21, 2018
@hcpl
Copy link
Contributor Author

hcpl commented May 21, 2018

Thank you for the explanation. I was just concerned about the effort needed to port from the old proc-macro & quote & syn stack, but keeping API minimal is important too, so that's fine.

@fmckeogh
Copy link

fmckeogh commented Jun 5, 2018

I'm very sorry if this is the wrong place, but I'm updating a crate to use the latest version of proc-macro2 and I have a lot of the trait 'std::convert::From<std::string::String>' is not implemented for 'proc_macro2::Ident' errors occurring for lines like

let _name = Ident::from(format!("_{}", name.as_ref()));

I understand I need to use to_string() before as_ref() but what can I do to fix the implementation?

Thanks! :)

@hcpl
Copy link
Contributor Author

hcpl commented Jun 5, 2018

I suppose name is an Ident. In that case, you can use

let _name = Ident::new(&name.to_string(), Span::call_site());

instead. Example on playground.

@fmckeogh
Copy link

fmckeogh commented Jun 5, 2018

Thank you so much! :)

Repository owner locked and limited conversation to collaborators Sep 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants