Skip to content

Tracking Issue for integer_casts #157388

@orlp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions