Skip to content

Commit 238ec47

Browse files
authored
Merge pull request #13 from cuviper/msrv-1.91
Add new methods for MSRV 1.91
2 parents cec1cef + 14334c2 commit 238ec47

7 files changed

Lines changed: 113 additions & 7 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
rust: [
15-
1.90.0, # MSRV
15+
1.91.0, # MSRV
1616
stable,
1717
beta,
1818
nightly,
@@ -52,7 +52,7 @@ jobs:
5252
runs-on: ubuntu-latest
5353
steps:
5454
- uses: actions/checkout@v4
55-
- uses: dtolnay/rust-toolchain@1.90.0
55+
- uses: dtolnay/rust-toolchain@1.91.0
5656
with:
5757
components: rustfmt
5858
- run: cargo fmt --all --check

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "num-primitive"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
description = "Traits for primitive numeric types"
55
repository = "https://github.com/rust-num/num-primitive"
66
license = "MIT OR Apache-2.0"
77
keywords = ["generic", "mathematics", "numerics", "primitive"]
88
categories = ["algorithms", "science", "no-std"]
99
edition = "2024"
10-
rust-version = "1.90"
10+
rust-version = "1.91"
1111

1212
[features]
1313
default = ["std"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![crate](https://img.shields.io/crates/v/num-primitive.svg)](https://crates.io/crates/num-primitive)
44
[![documentation](https://docs.rs/num-primitive/badge.svg)](https://docs.rs/num-primitive)
5-
[![minimum rustc 1.90](https://img.shields.io/badge/rustc-1.90+-red.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html)
5+
[![minimum rustc 1.91](https://img.shields.io/badge/rustc-1.91+-red.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html)
66
[![build status](https://github.com/rust-num/num-primitive/workflows/CI/badge.svg)](https://github.com/rust-num/num-primitive/actions)
77

88
Traits for primitive numeric types in Rust.
@@ -61,7 +61,7 @@ Release notes are available in [RELEASES.md](RELEASES.md).
6161

6262
## Compatibility
6363

64-
The `num-primitive` crate is currently tested for Rust 1.90 and greater. This
64+
The `num-primitive` crate is currently tested for Rust 1.91 and greater. This
6565
minimum-supported Rust version (MSRV) may be increased at any time to add
6666
support for newly-stabilized functionality from the standard library. Changes
6767
will be documented prominently in the release notes.

RELEASES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Release 0.3.2 (2025-12-16)
2+
3+
- Updated to MSRV 1.91.
4+
- Added `PrimitiveInteger::strict_{add,div,{div,rem}_euclid,mul,neg,pow,rem,shl,shr,sub}`
5+
- Added `PrimitiveSigned::strict_{abs,{add,sub}_unsigned}`
6+
- Added `PrimitiveUnsigned::strict_{abs,{add,sub}_signed}`
7+
- Added `PrimitiveUnsigned::{borrowing_sub,carrying_{add,mul,mul_add},checked_signed_diff}`
8+
19
# Release 0.3.1 (2025-12-16)
210

311
- Updated to MSRV 1.90.

src/integer.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ pub trait PrimitiveInteger:
221221
/// occurred.
222222
fn checked_mul(self, rhs: Self) -> Option<Self>;
223223

224-
/// Checked negation. Computes -self, returning `None` if `self == MIN`.
224+
/// Checked negation. Computes -self, returning `None` if `self == MIN` for signed integers,
225+
/// or for any non-zero unsigned integer.
225226
fn checked_neg(self) -> Option<Self>;
226227

227228
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if overflow occurred.
@@ -367,6 +368,44 @@ pub trait PrimitiveInteger:
367368
/// instead of overflowing.
368369
fn saturating_sub(self, rhs: Self) -> Self;
369370

371+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
372+
fn strict_add(self, rhs: Self) -> Self;
373+
374+
/// Strict integer division. Computes `self / rhs`, panicking if overflow occurred.
375+
fn strict_div(self, rhs: Self) -> Self;
376+
377+
/// Strict Euclidean division. Computes `self.div_euclid(rhs)`, panicking if overflow occurred.
378+
fn strict_div_euclid(self, rhs: Self) -> Self;
379+
380+
/// Strict integer multiplication. Computes `self * rhs`, panicking if overflow occurred.
381+
fn strict_mul(self, rhs: Self) -> Self;
382+
383+
/// Strict negation. Computes `-self`, panicking if `self == MIN` for signed integers,
384+
/// or for any non-zero unsigned integer.
385+
fn strict_neg(self) -> Self;
386+
387+
/// Strict exponentiation. Computes `self.pow(exp)`, panicking if overflow occurred.
388+
fn strict_pow(self, exp: u32) -> Self;
389+
390+
/// Strict integer remainder. Computes `self % rhs`, panicking if
391+
/// the division results in overflow.
392+
fn strict_rem(self, rhs: Self) -> Self;
393+
394+
/// Strict Euclidean remainder. Computes `self.rem_euclid(rhs)`, panicking if
395+
/// the division results in overflow.
396+
fn strict_rem_euclid(self, rhs: Self) -> Self;
397+
398+
/// Strict shift left. Computes `self << rhs`, panicking if `rhs` is larger
399+
/// than or equal to the number of bits in `self`.
400+
fn strict_shl(self, rhs: u32) -> Self;
401+
402+
/// Strict shift right. Computes `self >> rhs`, panicking if `rhs` is
403+
/// larger than or equal to the number of bits in `self`.
404+
fn strict_shr(self, rhs: u32) -> Self;
405+
406+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
407+
fn strict_sub(self, rhs: Self) -> Self;
408+
370409
/// Reverses the byte order of the integer.
371410
fn swap_bytes(self) -> Self;
372411

@@ -589,6 +628,17 @@ macro_rules! impl_integer {
589628
fn saturating_mul(self, rhs: Self) -> Self;
590629
fn saturating_pow(self, exp: u32) -> Self;
591630
fn saturating_sub(self, rhs: Self) -> Self;
631+
fn strict_add(self, rhs: Self) -> Self;
632+
fn strict_div(self, rhs: Self) -> Self;
633+
fn strict_div_euclid(self, rhs: Self) -> Self;
634+
fn strict_mul(self, rhs: Self) -> Self;
635+
fn strict_neg(self) -> Self;
636+
fn strict_pow(self, exp: u32) -> Self;
637+
fn strict_rem(self, rhs: Self) -> Self;
638+
fn strict_rem_euclid(self, rhs: Self) -> Self;
639+
fn strict_shl(self, rhs: u32) -> Self;
640+
fn strict_shr(self, rhs: u32) -> Self;
641+
fn strict_sub(self, rhs: Self) -> Self;
592642
fn swap_bytes(self) -> Self;
593643
fn to_be(self) -> Self;
594644
fn to_le(self) -> Self;

src/signed.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ pub trait PrimitiveSigned: PrimitiveInteger + From<i8> + core::ops::Neg<Output =
114114
/// Returns a number representing sign of `self`.
115115
fn signum(self) -> Self;
116116

117+
/// Strict absolute value. Computes `self.abs()`, panicking if `self == MIN`.
118+
fn strict_abs(self) -> Self;
119+
120+
/// Strict addition with an unsigned integer. Computes `self + rhs`,
121+
/// panicking if overflow occurred.
122+
fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self;
123+
124+
/// Strict subtraction with an unsigned integer. Computes `self - rhs`,
125+
/// panicking if overflow occurred.
126+
fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
127+
117128
/// Computes the absolute value of `self` without any wrapping or panicking.
118129
fn unsigned_abs(self) -> Self::Unsigned;
119130

@@ -159,6 +170,9 @@ macro_rules! impl_signed {
159170
fn saturating_neg(self) -> Self;
160171
fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
161172
fn signum(self) -> Self;
173+
fn strict_abs(self) -> Self;
174+
fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self;
175+
fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
162176
fn unsigned_abs(self) -> Self::Unsigned;
163177
fn wrapping_abs(self) -> Self;
164178
fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;

src/unsigned.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ pub trait PrimitiveUnsigned: PrimitiveInteger + From<u8> {
4040
/// Computes the absolute difference between `self` and `other`.
4141
fn abs_diff(self, other: Self) -> Self;
4242

43+
/// Calculates `self` &minus; `rhs` &minus; `borrow` and returns a tuple
44+
/// containing the difference and the output borrow.
45+
fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool);
46+
47+
/// Calculates `self` + `rhs` + `carry` and returns a tuple containing
48+
/// the sum and the output carry (in that order).
49+
fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool);
50+
51+
/// Calculates the "full multiplication" `self * rhs + carry`
52+
/// without the possibility to overflow.
53+
fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self);
54+
55+
/// Calculates the "full multiplication" `self * rhs + carry + add`.
56+
fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self);
57+
4358
/// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
4459
fn cast_signed(self) -> Self::Signed;
4560

@@ -56,6 +71,10 @@ pub trait PrimitiveUnsigned: PrimitiveInteger + From<u8> {
5671
/// wrapped in Some.
5772
fn checked_next_power_of_two(self) -> Option<Self>;
5873

74+
/// Checked integer subtraction. Computes `self - rhs` and checks if the result fits into a
75+
/// signed integer of the same size, returning `None` if overflow occurred.
76+
fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed>;
77+
5978
/// Checked subtraction with a signed integer. Computes `self - rhs`,
6079
/// returning `None` if overflow occurred.
6180
fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
@@ -91,6 +110,14 @@ pub trait PrimitiveUnsigned: PrimitiveInteger + From<u8> {
91110
/// the numeric bounds instead of overflowing.
92111
fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
93112

113+
/// Strict addition with a signed integer. Computes `self + rhs`,
114+
/// panicking if overflow occurred.
115+
fn strict_add_signed(self, rhs: Self::Signed) -> Self;
116+
117+
/// Strict subtraction with a signed integer. Computes `self - rhs`,
118+
/// panicking if overflow occurred.
119+
fn strict_sub_signed(self, rhs: Self::Signed) -> Self;
120+
94121
/// Wrapping (modular) addition with a signed integer. Computes `self + rhs`, wrapping around
95122
/// at the boundary of the type.
96123
fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
@@ -113,10 +140,15 @@ macro_rules! impl_unsigned {
113140

114141
forward! {
115142
fn abs_diff(self, other: Self) -> Self;
143+
fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool);
144+
fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool);
145+
fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self);
146+
fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self);
116147
fn cast_signed(self) -> Self::Signed;
117148
fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
118149
fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
119150
fn checked_next_power_of_two(self) -> Option<Self>;
151+
fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed>;
120152
fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
121153
fn div_ceil(self, rhs: Self) -> Self;
122154
fn is_multiple_of(self, rhs: Self) -> bool;
@@ -127,6 +159,8 @@ macro_rules! impl_unsigned {
127159
fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool);
128160
fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
129161
fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
162+
fn strict_add_signed(self, rhs: Self::Signed) -> Self;
163+
fn strict_sub_signed(self, rhs: Self::Signed) -> Self;
130164
fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
131165
fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self;
132166
}

0 commit comments

Comments
 (0)