Skip to content

ACP: [perf] proc-macro Ident should have method as_str #573

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

Open
loynoir opened this issue Apr 17, 2025 · 4 comments
Open

ACP: [perf] proc-macro Ident should have method as_str #573

loynoir opened this issue Apr 17, 2025 · 4 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@loynoir
Copy link

loynoir commented Apr 17, 2025

Proposal

Problem statement

Motivating examples or use cases

According to https://doc.rust-lang.org/proc_macro/struct.Ident.html

There are

  • pub fn new(string: &str, span: Span) -> Ident

  • ident.to_string()

Should provide below for performance

  • ident.as_str()

Very common actual usage

ident_string = ident.to_string()

match ident_string.as_str() { ... }

Solution sketch

Expect usage

match ident.as_str() { ... }

Alternatives

Links and related work

rust-lang/rust#139953

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@loynoir loynoir added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Apr 17, 2025
@loynoir loynoir changed the title [perf] proc-macro Ident should have method as_str ACP: [perf] proc-macro Ident should have method as_str Apr 17, 2025
@kennytm
Copy link
Member

kennytm commented Apr 17, 2025

if ident.to_string() and ident.as_str() are expected to have the same content, it would require a lot of refactoring to implement .as_str() for raw identifiers because it is expected that

let ident = Ident::new_raw("fn", Span::call_site());
assert_eq!(&ident.to_string(), "r#fn");

@loynoir
Copy link
Author

loynoir commented Apr 17, 2025

it would require a lot of refactoring

Emm, understand, but also quite surprising me that, the most important type, ident, does not provide low cost string comparison.

@tgross35
Copy link

Maybe we could implement PartialEq against both Ident and &str (or anything AsRef<[u8]> to cover BStr and others)? So we can keep the details more internal but still make comparison easier.

@tgross35
Copy link

tgross35 commented Apr 17, 2025

Actually it's probably worth considering a full suite of traits. proc-macro2 provides the following:

  • Hash
  • PartialEq<Self>
  • PartialEq<impl AsRef<str>>
  • Eq
  • PartialOrd
  • Ord

on Ident, there likely isn't any reason we couldn't do the same to also enable storage in maps/sets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants