Skip to content

Make int_format_into API more flexible #143636

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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

Follow-up of #142098.
Part of #138215.

This change allows to pass NumBuffer<u128> for any smaller integers, making the API much more flexible overall.

r? @Amanieu

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 8, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jul 8, 2025

Maybe add some tests showing the error when the size was chosen wrong

@GuillaumeGomez
Copy link
Member Author

Oh right. Adding ui tests for that. Very good idea!

@rustbot

This comment was marked as outdated.

@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Jul 8, 2025
@GuillaumeGomez
Copy link
Member Author

So sadly, I couldn't make the ui test work for various reasons explained in the run-make test. If someone has an idea, I'd love to hear it. In the meantime, I guess it's good enough?

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the int_format_into-improvement branch from 5a05f0f to 25f59e2 Compare July 8, 2025 13:51
@GuillaumeGomez GuillaumeGomez force-pushed the int_format_into-improvement branch from 25f59e2 to 82d9d62 Compare July 8, 2025 13:56
@jieyouxu jieyouxu removed the A-run-make Area: port run-make Makefiles to rmake.rs label Jul 8, 2025
///
#[doc = concat!("let n = 32", stringify!($signed), ";")]
/// // We use a `NumBuffer` used to store a bigger integer.
/// let mut buf = NumBuffer::<u128>::new();

This comment was marked as resolved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i128 and u128 are not covered by this macro, they are implemented below. So this example works all the time. :)

Also, if it didn't, the doctest would fail because compilation would fail.

@@ -338,7 +338,7 @@ macro_rules! impl_Display {
/// use core::fmt::NumBuffer;
///
#[doc = concat!("let n = 0", stringify!($signed), ";")]
/// let mut buf = NumBuffer::new();
#[doc = concat!("let mut buf = NumBuffer::<", stringify!($signed), ">::new();")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needing to specify/repeat the type when creating the buffer seems like a non-trivial downside. I'm not sure if the added flexibility is worth it. Can you give a semi-realistic example of where you'd want the flexibility of formatting one of several integer types into the same buffer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to have only one buffer across your program life to fit all your integers that have different sizes, it's quite convenient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would I want such a long-lived buffer? Every usage of itoa I've seen while researching for the ACP just creates a new temporary buffer on the spot whenever it needs to format an integer. I don't think I've even seen a single buffer used twice for the same integer type. Setting up a buffer doesn't have any cost that needs to be amortized, allocating some extra uninitialized space in your stack frame is as close to free as it gets. If anything, putting the buffer somewhere else has a higher risk that it's moved around, which generally implies copying the whole buffer!

|
= note: this note originates in the macro `impl_Display` (in Nightly builds, run with -Z macro-backtrace for more info)

note: the above error was encountered while instantiating `fn core::fmt::num::imp::<impl u32>::format_into::<u8>`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That this is a post-mono error is another small downside of this PR. There's a policy that cargo check isn't required to catch all errors that cargo build catches, but it's still preferable to have it catch as many errors as reasonably possible.

@Amanieu Amanieu added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jul 15, 2025
@Amanieu
Copy link
Member

Amanieu commented Jul 15, 2025

We discussed this in the @rust-lang/libs-api meeting. Rather than making format_into generic, we thought it would be better to instead have a method on NumBuffer which converts it to a NumBuffer of a different type. This should be tracked in a separate issue from the rest of the int_format_into API.

Additionally we would prefer to avoid post-monomorphization errors here and instead use trait bounds to limit the allowed conversions.

@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jul 15, 2025
@GuillaumeGomez
Copy link
Member Author

@Amanieu Does it allow to have one NumBuffer to handle all different integer sizes across one program life? Because it's the goal of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants