Skip to content

Commit

Permalink
refactor: use PrimInt to reduce number of trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Sep 13, 2024
1 parent 825bfd3 commit 69eabc1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 52 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ module_name_repetitions = "allow"
[features]
serde = ["dep:serde"]

[[bench]]
name = "main"
path = "benches/main.rs"
harness = false

[dependencies]
num-traits = "0.2.19"
paste = "1.0.15"
Expand Down Expand Up @@ -55,3 +50,8 @@ inherits = "release"
lto = "fat"
codegen-units = 1
incremental = false

[[bench]]
name = "main"
path = "benches/main.rs"
harness = false
5 changes: 3 additions & 2 deletions benches/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use const_decimal::{Decimal, Integer, Primitive};
use const_decimal::{Decimal, Integer};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
use prop::strategy::ValueTree;
use prop::test_runner::TestRunner;
use proptest::prelude::*;
Expand All @@ -15,7 +16,7 @@ where

fn bench_primitive_add<I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: Primitive + Arbitrary,
I: PrimInt + Arbitrary,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
5 changes: 3 additions & 2 deletions benches/div.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fmt::Debug;
use std::ops::Div;

use const_decimal::{Decimal, Integer, Primitive};
use const_decimal::{Decimal, Integer};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::{ConstOne, ConstZero, PrimInt};
use prop::strategy::ValueTree;
use prop::test_runner::TestRunner;
use proptest::prelude::*;
Expand All @@ -26,7 +27,7 @@ fn primitive_div<I>(
strategy: impl Strategy<Value = I> + Clone,
strategy_label: &str,
) where
I: Primitive + Div<Output = I>,
I: PrimInt + ConstZero + ConstOne,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
2 changes: 2 additions & 0 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() {
bench_integers::<9, u64>(
&mut criterion.benchmark_group("u64_9"),
0..(u32::MAX as u64),
// TODO: This upper bound is lower than necessary.
((u32::MAX as u64) - 10u64.pow(9) + 1)..(u64::MAX / 10u64.pow(9)),
(u64::MAX / 10u64.pow(9) + 1)..u64::MAX,
);
Expand All @@ -44,6 +45,7 @@ fn main() {
bench_integers::<18, u128>(
&mut criterion.benchmark_group("u128_18"),
0..(u64::MAX as u128),
// TODO: This upper bound is lower than necessary.
((u64::MAX as u128) - 10u128.pow(18) + 1)..(u128::MAX / 10u128.pow(18)),
(u128::MAX / 10u128.pow(18) + 1)..u128::MAX,
);
Expand Down
5 changes: 3 additions & 2 deletions benches/mul.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::fmt::Debug;

use const_decimal::{Decimal, Integer, Primitive};
use const_decimal::{Decimal, Integer};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
use prop::strategy::ValueTree;
use prop::test_runner::TestRunner;
use proptest::prelude::*;
Expand All @@ -25,7 +26,7 @@ fn primitive_mul<I>(
strategy: impl Strategy<Value = I> + Clone,
strategy_label: &str,
) where
I: Primitive,
I: PrimInt,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
5 changes: 3 additions & 2 deletions benches/sub.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use const_decimal::{Decimal, Integer, Primitive};
use const_decimal::{Decimal, Integer};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
use prop::strategy::ValueTree;
use prop::test_runner::TestRunner;
use proptest::prelude::*;
Expand All @@ -15,7 +16,7 @@ where

fn bench_primitive_sub<I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: Primitive + Arbitrary,
I: PrimInt + Arbitrary,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
4 changes: 3 additions & 1 deletion src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ where
let shortfall = D as usize - fractional_s_len;

// TODO: Remove the `checked_mul` in favor of ensuring `D` cannot overflow.
fractional.checked_mul(&I::pow(I::TEN, shortfall)).unwrap()
fractional
.checked_mul(&I::pow(I::TEN, shortfall.try_into().unwrap()))
.unwrap()
}
Ordering::Greater => return Err(ParseDecimalError::PrecisionLoss(fractional_s.len())),
};
Expand Down
49 changes: 11 additions & 38 deletions src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,43 @@ use std::num::ParseIntError;
use std::ops::{Not, Shr};
use std::str::FromStr;

use num_traits::{
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedSub, ConstOne,
ConstZero, One, Pow, WrappingAdd,
};
use num_traits::{CheckedNeg, CheckedRem, ConstOne, ConstZero, One, PrimInt, WrappingAdd};

use crate::cheats::Cheats;
use crate::full_mul_div::FullMulDiv;

pub trait Primitive:
// `num-traits`
ConstZero
pub trait Integer<const D: u8>:
PrimInt
+ ConstZero
+ ConstOne
+ One
+ Bounded
// `std`
+ CheckedAdd<Output = Self>
+ WrappingAdd<Output = Self>
+ CheckedSub<Output = Self>
+ CheckedMul<Output = Self>
+ CheckedDiv<Output = Self>
+ CheckedRem<Output = Self>
+ Not<Output = Self>
+ Pow<usize, Output = Self>
+ Shr<u32, Output = Self>
+ Clone
+ Copy
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Display
+ FromStr<Err = ParseIntError>
+ Cheats<D>
+ FullMulDiv
{
}

impl<T> Primitive for T where
T: ConstZero
impl<I, const D: u8> Integer<D> for I where
I: PrimInt
+ ConstZero
+ ConstOne
+ One
+ Bounded
+ CheckedAdd<Output = Self>
+ WrappingAdd<Output = Self>
+ CheckedSub<Output = Self>
+ CheckedMul<Output = Self>
+ CheckedDiv<Output = Self>
+ CheckedRem<Output = Self>
+ Not<Output = Self>
+ Pow<usize, Output = Self>
+ Shr<u32, Output = Self>
+ Clone
+ Copy
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Display
+ FromStr<Err = ParseIntError>
+ Cheats<D>
+ FullMulDiv
{
}

pub trait Integer<const D: u8>: Cheats<D> + FullMulDiv + Primitive {}

impl<I, const D: u8> Integer<D> for I where I: Cheats<D> + FullMulDiv + Primitive {}

pub trait SignedInteger<const D: u8>: Integer<D> + CheckedNeg {}

impl<I, const D: u8> SignedInteger<D> for I where I: Integer<D> + CheckedNeg {}

0 comments on commit 69eabc1

Please sign in to comment.