Skip to content

Conversation

madhav-madhusoodanan
Copy link
Contributor

@madhav-madhusoodanan madhav-madhusoodanan commented Mar 11, 2025

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

Context

  1. Implemented integer formatting into a fixed-size buffer without relying on fmt.
  2. Added a NumBuffer type to help with this process.

Associated Issue

r? @hanna-kruppe

@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 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 Mar 11, 2025
@madhav-madhusoodanan
Copy link
Contributor Author

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 11, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Mar 22, 2025

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

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 24, 2025
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 24, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

/// `MaybeUninit<u8>`.
#[unstable(feature = "int_format_into", issue = "138215")]
#[derive(Debug)]
pub struct NumBuffer {
Copy link
Member

Choose a reason for hiding this comment

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

as mentioned on the ACP and listed as an unresolved issue, I think NumBuffer should have a generic parameter for the type of integer being formatted (we could extend this to support floats later), so when we add future types like u8192 you don't have to have a huge buffer for u32 even though u8192 needs a huge buffer.

so like:

#[unstable(...)] // impl details
/// Safety: `Buffer` must be `[MaybeUninit<u8>; N]` where `N` is big enough to fit any value of `Self` when formatted.
pub unsafe trait NumBufferTrait {
    type Buffer: AsMut<[MaybeUninit<u8>]> + AsRef<[MaybeUninit<u8>]> + 'static + Sync + Send;
    const UNINIT_BUFFER: Self::Buffer;
}

// impl NumBufferTrait for u8, i8, u16, i16, ...

pub struct NumBuffer<T: NumBufferTrait> {
    contents: T::Buffer,
}

impl<T: NumBufferTrait> NumBuffer<T> {
    pub const fn new() -> Self {
        Self { contents: T::UNINIT_BUFFER }
    }

    pub fn len(&self) -> usize {
        self.contents.as_ref().len()
    }

    pub(crate) unsafe fn extract<I>(&self, index: I) -> &I::Output
    where
        I: SliceIndex<[MaybeUninit<u8>]>,
    {
        // SAFETY: `index` is bound-checked by the caller.
        unsafe { self.contents.as_ref().get_unchecked(index) }
    }

    #[cfg(feature = "optimize_for_size")]
    pub(crate) fn extract_start_mut_ptr(buf: &mut Self) -> *mut u8 {
        MaybeUninit::slice_as_mut_ptr(buf.contents.as_mut())
    }

    pub(crate) unsafe fn write(&mut self, offset: usize, data: u8) {
        self.contents.as_mut()[offset].write(data);
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be done in a follow up PR.

@bors
Copy link
Collaborator

bors commented May 16, 2025

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

@GuillaumeGomez
Copy link
Member

Now that #136264 was merged, maybe you can reuse the common code (this one) to make your PR smaller?

@GuillaumeGomez
Copy link
Member

Hi, any news on this? I'd really love to have this feature. If you don't have time, I can finish it if you want?

@madhav-madhusoodanan
Copy link
Contributor Author

madhav-madhusoodanan commented Jun 5, 2025

Hi @GuillaumeGomez

Yes please, I’m slightly occupied this moment. I would be grateful to you if this could be completed.

@GuillaumeGomez
Copy link
Member

I opened #142098. Closing this one then.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 8, 2025
…Amanieu

Implement `int_format_into` feature

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

Since rust-lang#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
- rust-lang#138215

r? `@hanna-kruppe`
rust-timer added a commit that referenced this pull request Jul 8, 2025
Rollup merge of #142098 - GuillaumeGomez:int_format_into, r=Amanieu

Implement `int_format_into` feature

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
- #138215

r? `@hanna-kruppe`
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jul 11, 2025
…Amanieu

Implement `int_format_into` feature

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

Since rust-lang#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
- rust-lang#138215

r? `@hanna-kruppe`
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.

9 participants