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

Formatter macros #2279

Open
ghost opened this issue Sep 15, 2023 · 10 comments · May be fixed by #2314
Open

Formatter macros #2279

ghost opened this issue Sep 15, 2023 · 10 comments · May be fixed by #2314
Assignees

Comments

@ghost
Copy link

ghost commented Sep 15, 2023

What would you like implemented? What do you want that Twilight lacks?

I would like to see the addition of macros like bold!, heading!, code_block!, and spoiler! in the Twilight library. These macros should allow developers to easily format text for Discord messages without the need for manual string manipulation. This feature would enhance the library's capabilities and improve the developer experience.

Are you willing to help towards contributing this feature?

Yes, I am enthusiastic about contributing to the implementation of these macros. I am willing to assist with code contributions, documentation updates, and any other tasks necessary to bring this feature to fruition.

Is there any other information that we should know?

  • I believe that adding these macros will not only benefit me but also enhance the usability of Twilight for a broader community of Discord bot developers.
  • I am open to discussing and collaborating on the design and implementation details of these macros with the Twilight development team.

Reference Documentation:
You can find more information about Discord message formatting in the official Discord Developer Portal documentation. This reference will help users understand the standard formatting options used in Discord and how the proposed macros would align with these conventions.

Thank you for considering this feature request. I believe it will be a valuable addition to Twilight and greatly benefit the broader Discord bot development community.

@laralove143
Copy link
Member

laralove143 commented Sep 15, 2023

Thank you for this! It could be part of twilight-util under another feature gate.

@ghost
Copy link
Author

ghost commented Sep 15, 2023

I think so too.

@AEnterprise
Copy link
Member

why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?

@ghost
Copy link
Author

ghost commented Sep 15, 2023

why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?

IMHO;

  • Macros are evaluated at compile time, which means that the formatting code is processed before the program runs. In contrast, regular functions are executed at runtime. This compile-time evaluation can lead to performance benefits, especially when formatting is used extensively, as macros can potentially be inlined by the compiler, resulting in more efficient code.
  • When formatting is performed through functions, there is an overhead associated with function calls. While modern compilers can inline small functions, macros ensure that there is no function call overhead at all, which can be important for frequently used formatting operations in Discord bot messages.

If there's any point where I'm mistaken, I would appreciate it if you could correct me. Thank you for reading.

@laralove143
Copy link
Member

laralove143 commented Sep 15, 2023

Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?

@ghost
Copy link
Author

ghost commented Sep 15, 2023

Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?

There are ways to achieve similar functionality using Rust macros by using token manipulation and code generation. You can generate code or tokens based on certain input, but this is not the same as creating truly dynamic strings within the macro.

macro_rules! bold {
    ($content:expr) => {
        format!("**{}**", $content)
    };
}

@ghost
Copy link
Author

ghost commented Sep 15, 2023

But yes, I agree that macros tend to be less general-purpose, and functions can often serve as more versatile and maintainable alternatives. Functions are typically more suitable for common tasks and can be used in a wider range of scenarios. However, macros still have their place in Rust for code generation, metaprogramming, and other compile-time tasks where their capabilities shine. I still believe that macros are useful and can be used effectively.

@AEnterprise
Copy link
Member

just strapping an #[inline] on the function will cause it to be inlined, thus making it functionally equivalent to the macro, but much more readable imo

macro's are useful but i don't think they offer any benefit here over just a macro

@Gelbpunkt
Copy link
Member

why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?

IMHO;

* Macros are evaluated at compile time, which means that the formatting code is processed before the program runs. In contrast, regular functions are executed at runtime. This compile-time evaluation can lead to performance benefits, especially when formatting is used extensively, as macros can potentially be inlined by the compiler, resulting in more efficient code.

* When formatting is performed through functions, there is an overhead associated with function calls. While modern compilers can inline small functions, macros ensure that there is no function call overhead at all, which can be important for frequently used formatting operations in Discord bot messages.

If there's any point where I'm mistaken, I would appreciate it if you could correct me. Thank you for reading.

You will get zero performance benefits for using format!. The std::fmt APIs are much more overhead than just prepending and appending the string. It also won't be evaluated at compile time since the APIs aren't const. What you probably want is to use concat!.

@ghost
Copy link
Author

ghost commented Sep 15, 2023

Yes, I agree. Thank you for sharing your perspective. So functions are the best approach to these requirements, right?

@HTGAzureX1212 HTGAzureX1212 self-assigned this Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants