Skip to content

Tracking Issue for #![feature(mixed_integer_ops)] #87840

Closed
@a1phyr

Description

@a1phyr
Contributor

Feature gate: #![feature(mixed_integer_ops)]

Public API

// `uX` is `u8`, `u16`, `u32`, `u64`,`u128`, `usize`
impl uX {
    pub const fn checked_add_signed(self, iX) -> Option<Self>;
    pub const fn overflowing_add_signed(self, iX) -> (Self, bool);
    pub const fn saturating_add_signed(self, iX) -> Self;
    pub const fn wrapping_add_signed(self, iX) -> Self;
}

impl iX {
    pub const fn checked_add_unsigned(self, uX) -> Option<Self>;
    pub const fn overflowing_add_unsigned(self, uX) -> (Self, bool);
    pub const fn saturating_add_unsigned(self, uX) -> Self;
    pub const fn wrapping_add_unsigned(self, uX) -> Self;

    pub const fn checked_sub_unsigned(self, uX) -> Option<Self>;
    pub const fn overflowing_sub_unsigned(self, uX) -> (Self, bool);
    pub const fn saturating_sub_unsigned(self, uX) -> Self;
    pub const fn wrapping_sub_unsigned(self, uX) -> Self;
}

Steps / History

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Aug 7, 2021
leonardo-m

leonardo-m commented on Jan 26, 2022

@leonardo-m

Somewhere you should write about use cases & purposes of these ops :)

brendanzab

brendanzab commented on Mar 10, 2022

@brendanzab
Member

Somewhere you should write about use cases & purposes of these ops :)

I've needed checked_add_signed for working with io::SeekFrom, for example! Had to roll my own (and I'm not sure how correct it is), inspired by this implementation:

fn checked_add_signed(pos: u64, offset: i64) -> Option<u64> {
    if offset >= 0 {
        u64::checked_add(pos, offset as u64)
    } else {
        u64::checked_sub(pos, offset.unsigned_abs())
    }
}

It would be great to have this in the stdlib!

a1phyr

a1phyr commented on Mar 10, 2022

@a1phyr
ContributorAuthor

This is exactly what prompted me to write these !

Also it is what std uses for Cursor now.

tjallingt

tjallingt commented on Jun 7, 2022

@tjallingt

Would love to see this get stabilized. Not sure what the right process is to start an FCP

avl

avl commented on Jul 23, 2022

@avl

I love these methods!

I would love to see *_sub_signed and friends too. I just had a use for saturating_sub_signed (subtracting a signed residual from an unsigned value to create a corrected value). In my case, I can just negate the residual. However, that might cause overflow, in which case the final calculated value would not be correct. This won't really matter in practice, but saturating_sub_signed still would have been nice!

joshtriplett

joshtriplett commented on Aug 10, 2022

@joshtriplett
Member

I just ran into a use case for these as well, in Cargo, for working with CPU counts (e.g. "use all but 1 CPU").

joshtriplett

joshtriplett commented on Aug 10, 2022

@joshtriplett
Member

These have been around for a long time, and it seems like several people are interested in them.

Shall we stabilize these?

@rfcbot merge

24 remaining items

Loading
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 RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @avl@joshtriplett@brendanzab@tjallingt@apiraino

      Issue actions

        Tracking Issue for `#![feature(mixed_integer_ops)]` · Issue #87840 · rust-lang/rust