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

Substitution/concatenation of token names #52

Open
oeed opened this issue Nov 28, 2022 · 3 comments
Open

Substitution/concatenation of token names #52

oeed opened this issue Nov 28, 2022 · 3 comments
Labels
D-discussion A decision (D) has not been made yet and is open to discussion I-feature This issue (I) regards a (potential) feature in the project T-accepted Triage (T): Initial review accepted issue/PR as valid

Comments

@oeed
Copy link

oeed commented Nov 28, 2022

Short Description:

I was hoping to have a way to substitute a value in to the name of a token. For example:

#[duplicate_item(
  int_type  max_value;
  [ u8 ]    [ 255 ];
  [ u16 ]   [ 65_535 ];
  [ u32 ]   [ 4_294_967_295 ];
)]
fn is_[int_type]_max(&self) -> bool {
  *self == max_value
}

// Generates...
fn is_u8_max(&self) -> bool {
  *self == 255
}

fn is_u16_max(&self) -> bool {
  *self == 65_535
}
// etc.

I've read through the docs and can't seem to find a way to do so without restoring to using something like paste!. Is that possible?

@oeed oeed added D-discussion A decision (D) has not been made yet and is open to discussion I-feature This issue (I) regards a (potential) feature in the project labels Nov 28, 2022
@github-actions github-actions bot added the T-new Triage (T): Has yet to be reviewed label Nov 28, 2022
@Emoun Emoun added T-accepted Triage (T): Initial review accepted issue/PR as valid and removed T-new Triage (T): Has yet to be reviewed labels Nov 28, 2022
@Emoun
Copy link
Owner

Emoun commented Nov 28, 2022

Using paste! is my usual go-to for cases like this. duplicate doesn't have a facility built in to do it right now.

I don't see a technical reason we couldn't add this (e.g. the module_disambiguation feature does something similar).
However, the question is whether it is necessary. Is it really a problem to use paste!?

@oeed
Copy link
Author

oeed commented Nov 29, 2022

Yeah that's totally fair, no specific issues with using paste! - although as I haven't had to use it before I had to Google around for a while before discovering how to do so. Might just be a matter of an example in the docs.

@Emoun
Copy link
Owner

Emoun commented Nov 29, 2022

I've thought a little bit about this since yesterday.

The module_disambiguation is kind of like this feature: automatically renaming an item based on the substitutions.
I have clearly previously found enough value in it to add it.
Maybe there is value in a more general feature, perhaps called item_disambiguation, that works the same as module_disambiguation but for any kind of item.
This would cover the above example:

#[duplicate_item(
  int_type  max_value;
  [ u8 ]    [ 255 ];
  [ u16 ]   [ 65_535 ];
  [ u32 ]   [ 4_294_967_295 ];
)]
fn is_max(&self) -> bool {
  *self == max_value
}

// Generates...
fn is_max_u8(&self) -> bool {
  *self == 255
}

fn is_max_u16(&self) -> bool {
  *self == 65_535
}
// etc.

Pros:

  • An extension to an existing feature.
  • Limited in scope.
  • Conceivably useful for many users. Here is another example.

Cons:

  • Significant added complexity, as it requires the crate to analyze the code to check if the body is an item and find the name.
  • It requires us to add the exact concatenation rules to the stability guarantees, such that people can call the resulting functions or structs without worrying the name might change in the future.

Question:
@oeed how would you feel about not being able to control exactly where in the name the substitution is added? E.g. that it will always be at the end: is_max_u8, is_max_u16 etc.

If this was available to you, would you have used it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-discussion A decision (D) has not been made yet and is open to discussion I-feature This issue (I) regards a (potential) feature in the project T-accepted Triage (T): Initial review accepted issue/PR as valid
Projects
None yet
Development

No branches or pull requests

2 participants