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

How to handle doc comments and examples? #54

Closed
DerZade opened this issue Apr 11, 2023 · 4 comments
Closed

How to handle doc comments and examples? #54

DerZade opened this issue Apr 11, 2023 · 4 comments
Labels
I-support This issue (I) supports the users of the project T-accepted Triage (T): Initial review accepted issue/PR as valid

Comments

@DerZade
Copy link

DerZade commented Apr 11, 2023

I want to use duplicate to create an asynchronous and a non-asynchronous version of a public method. Since this is a public API, I would like to have a proper doc comment and example. Currently both functions get the same comment, but I would like to have some small differences (especially in the example). Is there any way to achieve this?

@DerZade DerZade added the I-support This issue (I) supports the users of the project label Apr 11, 2023
@github-actions github-actions bot added the T-new Triage (T): Has yet to be reviewed label Apr 11, 2023
@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 Apr 12, 2023
@Emoun
Copy link
Owner

Emoun commented Apr 12, 2023

That's an interesting use case that I haven't thought of before and is a bit challenging.

First, the problem is that documentation comments are interpreted as strings, which duplicate does not look into for substitution. This means using /// comments will not work.
However, you can use documentation attributes directly and substitute the whole string:

#[duplicate_item(
  name             async     comment_sub;
  [some_fn]        [ ]       ["Comment for non-async"];
  [some_async_fn]  [async]   ["Comment for async"];
)]
/// Some comment that will not be substituted.
#[doc = comment_sub] 
async fn name() {
  ...
}

Notice how duplicate_item is before the comments. Putting it after will not work (it will not see them). This will result in:

/// Some comment that will not be substituted.
/// Comment for non-async
fn some_fn() {
  ...
}
/// Some comment that will not be substituted.
/// Comment for async
async fn some_async_fn() {
  ...
}

I hope this helps. Let me know how it works out.

@DerZade
Copy link
Author

DerZade commented Apr 12, 2023

Interesting 🤔I'll try it out and let you know how and if it worked out 🙈

@DerZade
Copy link
Author

DerZade commented Apr 12, 2023

After some testing, I can say that this works great for small differences, but is a bit unwieldy when trying to add different examples or something more complex.

I will probably use duplicate only for a private _impl function and then add several public functions manually with the proper doc comments wrapping only the _impl functions.

Thanks again for the very quick reply and the great crate. I'll close this as resolved 🙃

@DerZade DerZade closed this as completed Apr 12, 2023
@Emoun Emoun mentioned this issue Apr 12, 2023
3 tasks
@Emoun
Copy link
Owner

Emoun commented Apr 12, 2023

but is a bit unwieldy when trying to add different examples or something more complex.

Yeah, I had a feeling that would be the case.
It's a nice workaround you found with _impl functions, but I'll think about whether substitutions within strings should be added to the crate. If you have feedback or examples you'd like me to take into account, feel free to drop it in #55.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-support This issue (I) supports the users of the project T-accepted Triage (T): Initial review accepted issue/PR as valid
Projects
None yet
Development

No branches or pull requests

2 participants