Skip to content

Implement Zero, One, and Num for core::num::Saturating<T> #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
rust: [
1.60.0, # MSRV
1.62.0, # has_total_cmp
1.74.0, # has_num_saturating
stable,
beta,
nightly,
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ std = []
i128 = []

[build-dependencies]
autocfg = "1"
autocfg = "1.2"
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
fn main() {
let ac = autocfg::new();
let mut ac = autocfg::new();
ac.set_no_std(true);

ac.emit_expression_cfg("1f64.total_cmp(&2f64)", "has_total_cmp"); // 1.62
ac.emit_path_cfg("core::num::Saturating", "has_num_saturating"); // 1.74

autocfg::rerun_path("build.rs");
}
2 changes: 1 addition & 1 deletion ci/rustup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
set -ex

ci=$(dirname $0)
for version in 1.60.0 1.62.0 stable beta nightly; do
for version in 1.60.0 1.62.0 1.74.0 stable beta nightly; do
rustup run "$version" "$ci/test_full.sh"
done
81 changes: 81 additions & 0 deletions src/identities.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::num::Wrapping;
use core::ops::{Add, Mul};

#[cfg(has_num_saturating)]
use core::num::Saturating;

/// Defines an additive identity element for `Self`.
///
/// # Laws
Expand Down Expand Up @@ -95,6 +98,32 @@ where
const ZERO: Self = Wrapping(T::ZERO);
}

#[cfg(has_num_saturating)]
impl<T: Zero> Zero for Saturating<T>
where
Saturating<T>: Add<Output = Saturating<T>>,
{
fn is_zero(&self) -> bool {
self.0.is_zero()
}

fn set_zero(&mut self) {
self.0.set_zero();
}

fn zero() -> Self {
Saturating(T::zero())
}
}

#[cfg(has_num_saturating)]
impl<T: ConstZero> ConstZero for Saturating<T>
where
Saturating<T>: Add<Output = Saturating<T>>,
{
const ZERO: Self = Saturating(T::ZERO);
}

/// Defines a multiplicative identity element for `Self`.
///
/// # Laws
Expand Down Expand Up @@ -196,6 +225,28 @@ where
const ONE: Self = Wrapping(T::ONE);
}

#[cfg(has_num_saturating)]
impl<T: One> One for Saturating<T>
where
Saturating<T>: Mul<Output = Saturating<T>>,
{
fn set_one(&mut self) {
self.0.set_one();
}

fn one() -> Self {
Saturating(T::one())
}
}

#[cfg(has_num_saturating)]
impl<T: ConstOne> ConstOne for Saturating<T>
where
Saturating<T>: Mul<Output = Saturating<T>>,
{
const ONE: Self = Saturating(T::ONE);
}

// Some helper functions provided for backwards compatibility.

/// Returns the additive identity, `0`.
Expand Down Expand Up @@ -236,3 +287,33 @@ fn wrapping_is_one() {
fn require_one<T: One>(_: &T) {}
require_one(&Wrapping(42));
}

#[test]
#[cfg(has_num_saturating)]
fn saturating_identities() {
macro_rules! test_saturating_identities {
($($t:ty)+) => {
$(
assert_eq!(zero::<$t>(), zero::<Saturating<$t>>().0);
assert_eq!(one::<$t>(), one::<Saturating<$t>>().0);
assert_eq!((0 as $t).is_zero(), Saturating(0 as $t).is_zero());
assert_eq!((1 as $t).is_zero(), Saturating(1 as $t).is_zero());
)+
};
}

test_saturating_identities!(isize i8 i16 i32 i64 usize u8 u16 u32 u64);
}

#[test]
#[cfg(has_num_saturating)]
fn saturating_is_zero() {
fn require_zero<T: Zero>(_: &T) {}
require_zero(&Saturating(42));
}
#[test]
#[cfg(has_num_saturating)]
fn saturating_is_one() {
fn require_one<T: One>(_: &T) {}
require_one(&Saturating(42));
}
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ where
}
}

#[cfg(has_num_saturating)]
impl<T: Num> Num for core::num::Saturating<T>
where
core::num::Saturating<T>: NumOps,
{
type FromStrRadixErr = T::FromStrRadixErr;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
T::from_str_radix(str, radix).map(core::num::Saturating)
}
}

#[derive(Debug)]
pub enum FloatErrorKind {
Empty,
Expand Down Expand Up @@ -570,6 +581,31 @@ fn wrapping_from_str_radix() {
test_wrapping_from_str_radix!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
}

#[test]
#[cfg(has_num_saturating)]
fn saturating_is_num() {
fn require_num<T: Num>(_: &T) {}
require_num(&core::num::Saturating(42_u32));
require_num(&core::num::Saturating(-42));
}

#[test]
#[cfg(has_num_saturating)]
fn saturating_from_str_radix() {
macro_rules! test_saturating_from_str_radix {
($($t:ty)+) => {
$(
for &(s, r) in &[("42", 10), ("42", 2), ("-13.0", 10), ("foo", 10)] {
let w = core::num::Saturating::<$t>::from_str_radix(s, r).map(|w| w.0);
assert_eq!(w, <$t as Num>::from_str_radix(s, r));
}
)+
};
}

test_saturating_from_str_radix!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
}

#[test]
fn check_num_ops() {
fn compute<T: Num + Copy>(x: T, y: T) -> T {
Expand Down