Skip to content

Tracking Issue for integer extension and truncation methods #154330

Description

@Amanieu

View all comments

Feature gate: #![feature(integer_extend_truncate)]

This is a tracking issue for integer extension and truncation methods.

Public API

impl iX {
    // `Target` is a signed integer with size >= Self
    fn widen<Target>(self) -> Target
        where Self: WidenTarget<Target>;

    // `Target` is a signed integer with size <= Self
    fn truncate<Target>(self) -> Target
        where Self: TruncateTarget<Target>;
    fn saturating_truncate<Target>(self) -> Target
        where Self: TruncateTarget<Target>;
    fn checked_truncate<Target>(self) -> Option<Target>
        where Self: TruncateTarget<Target>;
}

impl uX {
    // `Target` is an unsigned integer with size >= Self
    fn widen<Target>(self) -> Target
        where Self: WidenTarget<Target>;

    // `Target` is an unsigned integer with size <= Self
    fn truncate<Target>(self) -> Target
        where Self: TruncateTarget<Target>;
    fn saturating_truncate<Target>(self) -> Target
        where Self: TruncateTarget<Target>;
    fn checked_truncate<Target>(self) -> Option<Target>
        where Self: TruncateTarget<Target>;
}

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 Mar 24, 2026
  2. jhpratt commented on Mar 24, 2026

    @jhpratt
  3. Amanieu commented on Mar 24, 2026

    @Amanieu
    Author
  4. jhpratt commented on Mar 24, 2026

    @jhpratt
  5. tgross35 commented on Mar 24, 2026

    @tgross35
    Member

    Is there any reason not to include widening unsigned->signed conversions as part of ExtendTarget? E.g. u8 to i16 is lossless.

  6. joshtriplett commented on Mar 24, 2026

    @joshtriplett
    Member

    Is there any reason not to include widening unsigned->signed conversions as part of ExtendTarget? E.g. u8 to i16 is lossless.

    We discussed this in the @rust-lang/libs-api meeting, and felt that either code should write these as separate conversions (e.g. .extend().cast_signed()) or use .into() which already provides "conversion via multiple operations at once".

  7. jhpratt commented on Mar 24, 2026

    @jhpratt
    Member

    Not doing so ensures the methods serve exactly one purpose. It's trivial to chain that and .cast_signed() (already stable) in that situation. Imo it's more explicit about what is happening.

    Last I checked, time is the only code base using num-conv, so check that out if you want to see real world code. Otherwise it's just examples, realistically.

  8. joshtriplett commented on Mar 24, 2026

    @joshtriplett
  9. jhpratt commented on Mar 24, 2026

    @jhpratt
  10. joshtriplett commented on Mar 24, 2026

    @joshtriplett
  11. jhpratt commented on Mar 24, 2026

    @jhpratt
  12. joshtriplett commented on Mar 24, 2026

    @joshtriplett
  13. jhpratt commented on Mar 24, 2026

    @jhpratt
  14. 13 remaining items

  15. joshtriplett commented on May 4, 2026

    @joshtriplett
    Member

    @Qelxiros That would be nice, with clippy's usual mechanism for only issuing such lints if your MSRV allows it.

  16. bjoernager commented on May 4, 2026

    @bjoernager
    Contributor

    Has the name widen been discussed instead of extend? Keeping in mind widening_mul having similar functionality (now that it's to return a wider integer.)

  17. joshtriplett commented on May 7, 2026

    @joshtriplett
    Member

    @bjoernager That's a really good thought. widen would be more consistent, and would avoid conflicts with the meaning of extend in collections. Big 👍 to changing that. Nominating to discuss.

  18. joshtriplett commented on May 8, 2026

    @joshtriplett
    Member
  19. Amanieu commented on May 12, 2026

    @Amanieu
    MemberAuthor

    If we adopt widen then for symmetry we should also rename the truncating operations to narrow. Proposed names:

    • truncating_narrow
    • saturating_narrow
    • checked_narrow

    And possibly even an unchecked_narrow which is UB if the original value doesn't fit in the narrowed type.

  20. SimonSapin commented on May 12, 2026

    @SimonSapin
    Contributor

    is there any architecture with instructions for unchecked_narrow with some advantage over truncating_narrow?

  21. Qelxiros commented on May 12, 2026

    @Qelxiros
    Contributor

    I'd guess that truncating_narrow is a bitmask of some sort, while the caller of unchecked_narrow must guarantee that value already fits in the smaller size, so it can be a no-op (assuming the value lives in a register at that program point). I'm not 100% sure though.

  22. programmerjake commented on May 12, 2026

    @programmerjake
    Member

    unchecked_narrow improves optimizations since it tells LLVM the input is in range so if LLVM needs to widen the values to fit in registers it needs no explicit conversion and probably other optimizations too.

  23. jhpratt commented on May 18, 2026

    @jhpratt
    Member

    Updated num-conv with widen. I'm also now subscribed to this issue since apparently I wasn't before.

  24. asquared31415 commented on May 26, 2026

    @asquared31415
    Contributor

    "truncate" and "extend" are well known terms that have been used for decades to describe this specific operation, is there a reason that the libs team prefers the new names?

  25. joshtriplett commented on Jul 28, 2026

    @joshtriplett
    Member

    @asquared31415 One reason is that we already have extend methods in the standard library that means something else (adding items to a collection).

  26. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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