Skip to content

Commit e9b9cdc

Browse files
committed
Merge remote-tracking branch 'upstream/master' into round_ties_even
2 parents cc8abf7 + fbdc29a commit e9b9cdc

File tree

10 files changed

+606
-25
lines changed

10 files changed

+606
-25
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
rust: [
1212
1.60.0, # MSRV
1313
1.62.0, # has_total_cmp
14+
1.74.0, # has_num_saturating
1415
stable,
1516
beta,
1617
nightly,

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ std = []
3131
i128 = []
3232

3333
[build-dependencies]
34-
autocfg = "1"
34+
autocfg = "1.2"

build.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
fn main() {
2-
let ac = autocfg::new();
2+
let mut ac = autocfg::new();
3+
ac.set_no_std(true);
34

45
ac.emit_expression_cfg("1f64.total_cmp(&2f64)", "has_total_cmp"); // 1.62
6+
ac.emit_path_cfg("core::num::Saturating", "has_num_saturating"); // 1.74
57
ac.emit_expression_cfg("1.5f64.round_ties_even()", "has_round_ties_even"); // 1.77
68

79
autocfg::rerun_path("build.rs");

ci/rustup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
set -ex
66

77
ci=$(dirname $0)
8-
for version in 1.60.0 1.62.0 stable beta nightly; do
8+
for version in 1.60.0 1.62.0 1.74.0 stable beta nightly; do
99
rustup run "$version" "$ci/test_full.sh"
1010
done

ci/test_full.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ FEATURES=(libm)
3131
echo "Testing supported features: ${FEATURES[*]}"
3232

3333
cargo generate-lockfile
34+
check_version 1.63.0 || cargo update -p libm --precise 0.2.9
3435

3536
set -x
3637

src/bounds.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use core::num::Wrapping;
2+
use core::num::{
3+
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
4+
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
5+
};
26
use core::{f32, f64};
37
use core::{i128, i16, i32, i64, i8, isize};
48
use core::{u128, u16, u32, u64, u8, usize};
@@ -68,6 +72,49 @@ bounded_impl!(i32, i32::MIN, i32::MAX);
6872
bounded_impl!(i64, i64::MIN, i64::MAX);
6973
bounded_impl!(i128, i128::MIN, i128::MAX);
7074

75+
macro_rules! bounded_impl_nonzero_const {
76+
($t:ty, $v:expr, $i:ident) => {
77+
const $i: $t = match <$t>::new($v) {
78+
Some(nz) => nz,
79+
None => panic!("bad nonzero bound!"),
80+
};
81+
};
82+
}
83+
84+
macro_rules! bounded_impl_nonzero {
85+
($t:ty, $min:expr, $max:expr) => {
86+
impl Bounded for $t {
87+
#[inline]
88+
fn min_value() -> $t {
89+
// when MSRV is 1.70 we can use $t::MIN
90+
bounded_impl_nonzero_const!($t, $min, MIN);
91+
MIN
92+
}
93+
94+
#[inline]
95+
fn max_value() -> $t {
96+
// when MSRV is 1.70 we can use $t::MAX
97+
bounded_impl_nonzero_const!($t, $max, MAX);
98+
MAX
99+
}
100+
}
101+
};
102+
}
103+
104+
bounded_impl_nonzero!(NonZeroUsize, 1, usize::MAX);
105+
bounded_impl_nonzero!(NonZeroU8, 1, u8::MAX);
106+
bounded_impl_nonzero!(NonZeroU16, 1, u16::MAX);
107+
bounded_impl_nonzero!(NonZeroU32, 1, u32::MAX);
108+
bounded_impl_nonzero!(NonZeroU64, 1, u64::MAX);
109+
bounded_impl_nonzero!(NonZeroU128, 1, u128::MAX);
110+
111+
bounded_impl_nonzero!(NonZeroIsize, isize::MIN, isize::MAX);
112+
bounded_impl_nonzero!(NonZeroI8, i8::MIN, i8::MAX);
113+
bounded_impl_nonzero!(NonZeroI16, i16::MIN, i16::MAX);
114+
bounded_impl_nonzero!(NonZeroI32, i32::MIN, i32::MAX);
115+
bounded_impl_nonzero!(NonZeroI64, i64::MIN, i64::MAX);
116+
bounded_impl_nonzero!(NonZeroI128, i128::MIN, i128::MAX);
117+
71118
impl<T: Bounded> Bounded for Wrapping<T> {
72119
fn min_value() -> Self {
73120
Wrapping(T::min_value())
@@ -146,3 +193,37 @@ fn wrapping_is_bounded() {
146193
require_bounded(&Wrapping(42_u32));
147194
require_bounded(&Wrapping(-42));
148195
}
196+
197+
#[test]
198+
fn bounded_unsigned_nonzero() {
199+
macro_rules! test_bounded_impl_unsigned_nonzero {
200+
($t:ty, $base_ty:ty) => {
201+
assert_eq!(<$t as Bounded>::min_value().get(), 1);
202+
assert_eq!(<$t as Bounded>::max_value().get(), <$base_ty>::MAX);
203+
};
204+
}
205+
206+
test_bounded_impl_unsigned_nonzero!(NonZeroUsize, usize);
207+
test_bounded_impl_unsigned_nonzero!(NonZeroU8, u8);
208+
test_bounded_impl_unsigned_nonzero!(NonZeroU16, u16);
209+
test_bounded_impl_unsigned_nonzero!(NonZeroU32, u32);
210+
test_bounded_impl_unsigned_nonzero!(NonZeroU64, u64);
211+
test_bounded_impl_unsigned_nonzero!(NonZeroU128, u128);
212+
}
213+
214+
#[test]
215+
fn bounded_signed_nonzero() {
216+
macro_rules! test_bounded_impl_signed_nonzero {
217+
($t:ty, $base_ty:ty) => {
218+
assert_eq!(<$t as Bounded>::min_value().get(), <$base_ty>::MIN);
219+
assert_eq!(<$t as Bounded>::max_value().get(), <$base_ty>::MAX);
220+
};
221+
}
222+
223+
test_bounded_impl_signed_nonzero!(NonZeroIsize, isize);
224+
test_bounded_impl_signed_nonzero!(NonZeroI8, i8);
225+
test_bounded_impl_signed_nonzero!(NonZeroI16, i16);
226+
test_bounded_impl_signed_nonzero!(NonZeroI32, i32);
227+
test_bounded_impl_signed_nonzero!(NonZeroI64, i64);
228+
test_bounded_impl_signed_nonzero!(NonZeroI128, i128);
229+
}

0 commit comments

Comments
 (0)