Skip to content

Convenience trait implementations on Ident #93

Description

@hcpl

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)?

Activity

  1. alexcrichton commented on May 21, 2018

    @alexcrichton
    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

  2. dtolnay commented on May 21, 2018

    @dtolnay
    Owner

    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.

  3. hcpl commented on May 21, 2018

    @hcpl
    ContributorAuthor

    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.

  4. fmckeogh commented on Jun 5, 2018

    @fmckeogh

    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! :)

  5. hcpl commented on Jun 5, 2018

    @hcpl
    ContributorAuthor

    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.

  6. fmckeogh commented on Jun 5, 2018

    @fmckeogh

    Thank you so much! :)

  7. Repository owner locked and limited conversation to collaborators on Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions