Skip to content

Tracking Issue for integer_casts #157388

Open

Description

@orlp

Feature gate: #![feature(integer_casts)]

This is a tracking issue for the addition of saturating, wrapping, checked, unchecked, and strict casts between integer types.

Public API

For each built-in integer type T the following methods are added:

impl T {
    /// Converts `self` to the target integer type, saturating at the nearest edge
    /// of the target type's domain if the value does not lie in within it.
    fn saturating_cast<T: BoundedCastFromInt<Self>>(self) -> T;
    
    /// Converts `self` to the target integer type, wrapping around at the
    /// boundary of the target type.
    fn wrapping_cast<T: BoundedCastFromInt<Self>>(self) -> T;

    /// Converts `self` to the target integer type, returning `None` if the value
    /// does not lie in the target type's domain.
    fn checked_cast<T: CheckedCastFromInt<Self>>(self) -> Option<T>;

    /// Equivalent to `self.checked_cast::<Int>().unwrap()`.
    fn strict_cast<T: CheckedCastFromInt<Self>>(self) -> T;

    /// Equivalent to `self.checked_cast::<Int>().unwrap_unchecked()`.
    unsafe fn unchecked_cast<T: CheckedCastFromInt<Self>>(self) -> T;
}

Steps / History

Unresolved Questions

  • What should happen to the current unstable narrow/truncate API?
  • Should the NonZero types be included?

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 Jun 3, 2026
  2. orlp commented on Jun 3, 2026

    @orlp
    ContributorAuthor

    I can't assign myself but I will try to port the implementation from the int-cast crate to std this week.

  3. added a commit that references this issue on Jun 7, 2026
  4. added a commit that references this issue on Jun 7, 2026
  5. added 3 commits that reference this issue on Jun 8, 2026
  6. added a commit that references this issue on Jun 8, 2026
  7. added a commit that references this issue on Jun 8, 2026
  8. Apersoma commented on Jun 9, 2026

    @Apersoma
    Contributor

    The BoundedCastFromInt trait would need to be split into a saturating and wrapping variants for NonZero<uint> to ever be supported by the saturating cast since you can't implement the wrapping cast for it (except for converting between signed and unsigned nonzeros of the same size but cast_signed and cast_unsigned already exist).

  9. orlp commented on Jun 9, 2026

    @orlp
    ContributorAuthor

    @Apersoma I would never implement a wrapping variant for NonZero as it's unclear what it means. Saturating somewhat makes sense for NonZero<uint> but makes no sense for NonZero<int>.

    Personally I think it's better if we implement neither operation with NonZero as the output type.

    NonZero is fine as source type for BoundedCastFromInt, and always fine for CheckedCastFromInt both as source and target.

  10. bgkillas commented on Jun 11, 2026

    @bgkillas

    asking if we can extend support to booleans so we dont still need to do u8::from(val).strict_cast::<i32>() instead of val as i32, so we just have val.strict_cast::<i32>() where val is a bool

  11. orlp commented on Jun 11, 2026

    @orlp
    ContributorAuthor

    @bgkillas i32::from(val)?

  12. bgkillas commented on Jun 11, 2026

    @bgkillas

    dont know why i didn't think we had this im silly, still feel like for consistency it would be nice to have the applicable cast functions for bool but idk.

  13. orlp commented on Jun 11, 2026

    @orlp
    Author
  14. 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

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-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