Skip to content

Implement int_format_into feature #142098

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

@GuillaumeGomez GuillaumeGomez commented Jun 5, 2025

I took over #138338 with @madhav-madhusoodanan's approval.

Since #136264, a lot of changes happened so I made use of them to reduce the number of changes.

ACP approval: rust-lang/libs-team#546 (comment)

Associated Issue

r? @hanna-kruppe

@rustbot
Copy link
Collaborator

rustbot commented Jun 5, 2025

Failed to set assignee to hanna-kruppe: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@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 Jun 5, 2025
assert_eq!(ret, value);

let mut buffer = NumBuffer::<$int>::new();
assert_eq!(value.format_into(&mut buffer), s.as_str());
Copy link
Member Author

Choose a reason for hiding this comment

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

Since format_into is not part of a trait, I needed to switch from a function to a macro to avoid the generics limitations.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could write and implement a trait right here, in the test - right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I could indeed, but would be better than the current code?

Copy link
Contributor

Choose a reason for hiding this comment

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

Less churn; other than that I don't have strong feelings.

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 someone else agrees with you I'll make the change (lazyness++ 😆).

#[derive(Debug)]
pub struct NumBuffer<T: NumBufferTrait> {
// FIXME: Once const generics feature is working, use `T::BUF_SIZE` instead of 40.
pub(crate) buf: [MaybeUninit::<u8>; 40],
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a great frustration to discover that I couldn't just use [MaybeUninit::<u8>; T::BUF_SIZE] so until it's actually supported, they will all use the max size. 😞

Copy link
Contributor

Choose a reason for hiding this comment

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

@programmerjake sketched an alternative in the ACP that works without const generic expressions. But it's complicated enough that I don't think it's worth it as long as the maximum is just 40 bytes. Between that and the fact that the type parameter on NumBuffer is only an unresolved question, not part of the approved ACP, I don't really see the point of including it in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

@lqd provided me a way to do it so all solved now. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah sadly it breaks something else so it cannot. For reference here is the code showing the feature that was supposed to unlock the situation: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=11cfd87eaeeef6481be6433123fc0caf (the min_generic_const_args feature).

@GuillaumeGomez
Copy link
Member Author

r? @Amanieu

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the int_format_into branch 2 times, most recently from d11cb44 to 70ab4e0 Compare June 6, 2025 09:44
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez changed the title Int format into Implement int_format_into feature Jun 6, 2025
@GuillaumeGomez
Copy link
Member Author

Applied suggestions and opened another PR for the number of digits of u128 computation to not add even more changes to this PR.

unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
// SAFETY: All buf content since offset is set.
let written = unsafe { buf.get_unchecked(offset..) };
// SAFETY: Writes use ASCII from the lookup table exclusively.
Copy link
Contributor

Choose a reason for hiding this comment

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

this must be part of the precondition....? and the comment above should reference the precondition as well.

@bors
Copy link
Collaborator

bors commented Jun 7, 2025

☔ The latest upstream changes (presumably #142133) made this pull request unmergeable. Please resolve the merge conflicts.

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.

7 participants