Skip to content

Tracking Issue for clamp_magnitude method on float and signed integer primitives #148519

Open

Description

@IntegralPilot

Feature gate: #![feature(clamp_magnitude)]

This is a tracking issue for the clamp_magnitude method on primitive signed integer and floating-point types.

This method provides an ergonomic and intention-revealing way to clamp a numeric value to a symmetric range [-limit, limit]. This is a common operation in domains like graphics, physics, and signal processing, and is currently written as value.clamp(-limit, limit). The dedicated method improves clarity, reduces the chance of typos, and better conveys the developer's intent.

Public API

The proposed API adds the clamp_magnitude method to all primitive signed integer and floating-point types.

// For floating-point types
impl f16 {
    /// Clamps this number to a symmetric range centred around zero.
    ///
    /// The method clamps the number's magnitude (absolute value) to be at most `limit`.
    ///
    /// This is functionally equivalent to `self.clamp(-limit, limit)`, but is more
    /// explicit about the intent.
    ///
    /// # Panics
    ///
    /// Panics if `limit` is negative or NaN, as this indicates a logic error.
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: f16) -> f16;
}
impl f32 {
    /// Same doc-comment as f16
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: f32) -> f32;
}

impl f64 {
    /// Same doc-comment as f16
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: f64) -> f64;
}

impl f128 {
    /// Same doc-comment as f16
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: f128) -> f128;
}

// For signed integer types
impl i8 {
    /// Clamps this number to a symmetric range centred around zero.
    ///
    /// The method clamps the number's magnitude (absolute value) to be at most `limit`.
    ///
    /// This is functionally equivalent to `self.clamp(-limit, limit)`, but is more
    /// explicit about the intent.
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: u8) -> i8;
}

impl i16 {
    /// Same doc-comment as i8
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: u16) -> i16;
}

impl i32 {
    /// Same doc-comment as i8
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: u32) -> i32;
}

impl i64 {
    /// Same doc-comment as i8
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: u64) -> i64;
}

impl i128 {
    /// Same doc-comment as i8
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: u128) -> i128;
}

impl isize {
    /// Same doc-comment as i8
    #[must_use = "this returns the clamped value and does not modify the original"]
    pub fn clamp_magnitude(self, limit: usize) -> isize;
}

Steps / History

(Remember to update the S-tracking-* label when checking boxes.)

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Nov 5, 2025
  2. IntegralPilot commented on Nov 5, 2025

    @IntegralPilot
    ContributorAuthor

    @rustbot claim

  3. interactive-toast commented on Nov 6, 2025

    @interactive-toast

    If/when this is stabilized, should a style lint be added to clippy to suggest this method instead of val.clamp(-limit, limit)?

    Apologies if this isn't the right place for clippy discussion.

  4. IntegralPilot commented on Nov 7, 2025

    @IntegralPilot
    ContributorAuthor

    Yes, that's a great idea. I mentioned it in the original ACP, I think it would be a good way to drive implementation of the new, more ergonomic method. :)

  5. added a commit that references this issue on Dec 1, 2025
  6. added a commit that references this issue on Dec 1, 2025
  7. IntegralPilot commented on Dec 2, 2025

    @IntegralPilot
    ContributorAuthor

    @rustbot label +S-tracking-needs-to-bake -S-tracking-unimplemented

  8. added
    S-tracking-needs-to-bakeStatus: The implementation is "complete" but it needs time to bake.
    and removed on Dec 2, 2025
  9. IntegralPilot commented on Dec 2, 2025

    @IntegralPilot
    ContributorAuthor

    Will try and put some calls for testing out and see if there's any feedback or suggestions for changes for a few weeks before stabilising, S-tracking-needs-to-bake seemed the most appropriate S-tracking-* tag.

  10. added a commit that references this issue on Jan 20, 2026
  11. joseph-b-baud commented on Jun 16, 2026

    @joseph-b-baud

    It seems like you may have forgotten about this issue. That's a shame, since I think it would be nice if this was stabilized. I've noticed one problem with the method, and have two suggestions:

    • When you pass in NaN as limit in the float versions of clamp_magnitude, the error message is still the one used when limit is negative (limit must be non-negative) because the check for limit being non-negative is limit >= 0.0, which is false for NaN since it is unordered.
    • Since clamp is const, clamp_magnitude should also be const.
    • NonZero should also have clamp_magnitude.

    I can implement some changes if you're very busy.

  12. IntegralPilot commented on Jun 18, 2026

    @IntegralPilot
    ContributorAuthor

    Oh yes, sorry this did slip my mind! Thank you for the suggestions, I think they sound good and agree with them! I am a bit busy (IBDP arghhh), so would really appreciate you implementing them if you have the time! :)

  13. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
  14. added 5 commits that reference this issue on Sep 29, 2026
  15. added a commit that references this issue on Oct 1, 2026
  16. added a commit that references this issue on Oct 1, 2026
  17. Eleocraft commented on Oct 2, 2026

    @Eleocraft
    Contributor
    • When you pass in NaN as limit in the float versions of clamp_magnitude, the error message is still the one used when limit is negative (limit must be non-negative) because the check for limit being non-negative is limit >= 0.0, which is false for NaN since it is unordered.
    • Since clamp is const, clamp_magnitude should also be const.
    • NonZero should also have clamp_magnitude.

    These issues should be closed with the merging of PR #163279. I think this feature is ready for the next steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-to-bakeStatus: The implementation is "complete" but it needs time to bake.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions