Skip to content

Commit fe7f946

Browse files
authored
Replace const generic ranges with generic ranges (#63)
* wip commit * wip * Fix stuffs * Fix some tests * More generic `AsRepr` impls * Run `cargo fmt` * Add test * Add `types` module * Start adding `_to()` to the end of affected methods * Dedup `mul_ranged` -> `mul_ranged_to` * `pow_ranged()` -> `pow_ranged_to()` * `div_ranged()` -> `div_ranged_to()` * Switch more away from `const OUT_MAX` * `OUTPUT` -> `OUT` * Remove rest of generic MIN/MAX pairs * Revert `clamp_to()` change * Fix tests
1 parent aaa7741 commit fe7f946

39 files changed

+1375
-4087
lines changed

src/bitops.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ macro_rules! bitops_impl {
273273
/// ```rust
274274
/// # use ranch::bitwise::{I7, I13};
275275
/// assert_eq!(
276-
/// I7::new::<0b10_1011>().expanding_shl::<6, I13>(),
276+
/// I7::new::<0b10_1011>().expanding_shl_to::<6, I13>(),
277277
/// I13::new::<0b1010_1100_0000>(),
278278
/// );
279279
/// ```
280280
#[must_use = "this returns the result of the operation, \
281281
without modifying the original"]
282-
pub const fn expanding_shl<const N: u32, T>(self) -> T
282+
pub const fn expanding_shl_to<const N: u32, T>(self) -> T
283283
where
284284
<T as FromRepr>::Repr: DowncastShl,
285285
T: FromRepr + BitwiseSigned<<T as FromRepr>::Repr>,
@@ -304,13 +304,13 @@ macro_rules! bitops_impl {
304304
/// ```rust
305305
/// # use ranch::bitwise::{I7, I13};
306306
/// assert_eq!(
307-
/// I13::new::<0b1010_1100_0000>().shrinking_shr::<6, I7>(),
307+
/// I13::new::<0b1010_1100_0000>().shrinking_shr_to::<6, I7>(),
308308
/// I7::new::<0b10_1011>(),
309309
/// );
310310
/// ```
311311
#[must_use = "this returns the result of the operation, \
312312
without modifying the original"]
313-
pub const fn shrinking_shr<const N: u32, T>(self) -> T
313+
pub const fn shrinking_shr_to<const N: u32, T>(self) -> T
314314
where
315315
T: FromRepr + BitwiseSigned<<T as FromRepr>::Repr>,
316316
Self: AsPrimitive<<T as FromRepr>::Repr>,
@@ -780,13 +780,13 @@ macro_rules! bitops_impl {
780780
/// ```rust
781781
/// # use ranch::bitwise::{U6, U12};
782782
/// assert_eq!(
783-
/// U6::new::<0b10_1011>().expanding_shl::<6, U12>(),
783+
/// U6::new::<0b10_1011>().expanding_shl_to::<6, U12>(),
784784
/// U12::new::<0b1010_1100_0000>(),
785785
/// );
786786
/// ```
787787
#[must_use = "this returns the result of the operation, \
788788
without modifying the original"]
789-
pub const fn expanding_shl<const N: u32, T>(self) -> T
789+
pub const fn expanding_shl_to<const N: u32, T>(self) -> T
790790
where
791791
<T as FromRepr>::Repr: DowncastShl,
792792
T: FromRepr + BitwiseUnsigned<<T as FromRepr>::Repr>,
@@ -811,13 +811,13 @@ macro_rules! bitops_impl {
811811
/// ```rust
812812
/// # use ranch::bitwise::{U6, U12};
813813
/// assert_eq!(
814-
/// U12::new::<0b1010_1100_0000>().shrinking_shr::<6, U6>(),
814+
/// U12::new::<0b1010_1100_0000>().shrinking_shr_to::<6, U6>(),
815815
/// U6::new::<0b10_1011>(),
816816
/// );
817817
/// ```
818818
#[must_use = "this returns the result of the operation, \
819819
without modifying the original"]
820-
pub const fn shrinking_shr<const N: u32, T>(self) -> T
820+
pub const fn shrinking_shr_to<const N: u32, T>(self) -> T
821821
where
822822
T: FromRepr + BitwiseUnsigned<<T as FromRepr>::Repr>,
823823
Self: AsPrimitive<<T as FromRepr>::Repr>,

src/cast/as_primitive.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![allow(unsafe_code)]
22

3-
use core::{mem, ptr};
3+
use core::{mem, num::NonZero, ptr};
44

55
use as_repr::AsRepr;
66

77
use crate::{
8-
multirange::MultiRange, num::rangeable_primitive::RangeablePrimitive, *,
8+
multirange::{MultiRange, Ranged},
9+
num::rangeable_primitive::RangeablePrimitive,
910
};
1011

1112
pub trait Primitive: RangeablePrimitive<ZeroablePrimitive = Self> {}
@@ -27,11 +28,21 @@ pub unsafe trait AsPrimitive<T>:
2728
}
2829

2930
macro_rules! as_primitive {
30-
($type:ident, $p:ty, $signed:literal) => {
31-
unsafe impl<const MIN: $p, const MAX: $p, T> AsPrimitive<T>
32-
for $type<MIN, MAX>
31+
($p:ty, $signed:literal) => {
32+
unsafe impl<T, R> AsPrimitive<T> for Ranged<$p, R>
3333
where
3434
T: Primitive,
35+
R: MultiRange<$p>,
36+
{
37+
type Repr = $p;
38+
39+
const SIGNED: bool = $signed;
40+
}
41+
42+
unsafe impl<T, R> AsPrimitive<T> for Ranged<NonZero<$p>, R>
43+
where
44+
T: Primitive,
45+
R: MultiRange<$p>,
3546
{
3647
type Repr = $p;
3748

@@ -40,26 +51,16 @@ macro_rules! as_primitive {
4051
};
4152
}
4253

43-
as_primitive!(RangedU8, u8, false);
44-
as_primitive!(RangedU16, u16, false);
45-
as_primitive!(RangedU32, u32, false);
46-
as_primitive!(RangedU64, u64, false);
47-
as_primitive!(RangedU128, u128, false);
48-
as_primitive!(RangedI8, i8, true);
49-
as_primitive!(RangedI16, i16, true);
50-
as_primitive!(RangedI32, i32, true);
51-
as_primitive!(RangedI64, i64, true);
52-
as_primitive!(RangedI128, i128, true);
53-
as_primitive!(RangedNonZeroU8, u8, false);
54-
as_primitive!(RangedNonZeroU16, u16, false);
55-
as_primitive!(RangedNonZeroU32, u32, false);
56-
as_primitive!(RangedNonZeroU64, u64, false);
57-
as_primitive!(RangedNonZeroU128, u128, false);
58-
as_primitive!(RangedNonZeroI8, i8, true);
59-
as_primitive!(RangedNonZeroI16, i16, true);
60-
as_primitive!(RangedNonZeroI32, i32, true);
61-
as_primitive!(RangedNonZeroI64, i64, true);
62-
as_primitive!(RangedNonZeroI128, i128, true);
54+
as_primitive!(u8, false);
55+
as_primitive!(u16, false);
56+
as_primitive!(u32, false);
57+
as_primitive!(u64, false);
58+
as_primitive!(u128, false);
59+
as_primitive!(i8, true);
60+
as_primitive!(i16, true);
61+
as_primitive!(i32, true);
62+
as_primitive!(i64, true);
63+
as_primitive!(i128, true);
6364

6465
pub(crate) const fn as_primitive_shrinking<T, V>(value: V) -> T
6566
where

src/cast/as_repr.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,54 @@ use core::num::NonZero;
55
use as_repr::AsRepr;
66

77
use crate::{
8+
cast::{as_primitive::Primitive, to::IsNonZero},
89
multirange::{MultiRange, Ranged},
910
num::rangeable_primitive::RangeablePrimitive,
10-
*,
1111
};
1212

13-
// unsafe: `repr(transparent)` on `Ranged` is `repr(primitive)`
14-
unsafe impl<T, R> AsRepr<T> for Ranged<T, R>
13+
// unsafe: `repr(transparent)` on `Ranged<T, R>` is `repr(T)`
14+
unsafe impl<T, R, A> AsRepr<A> for Ranged<T, R>
1515
where
16-
T: RangeablePrimitive,
16+
T: RangeablePrimitive + AsRepr<A>,
1717
R: MultiRange<T::ZeroablePrimitive>,
18+
A: RangeablePrimitive,
1819
{
1920
}
2021

21-
macro_rules! as_repr {
22-
($nonzero:ident, $ranged:ident, $p:ident $(,)?) => {
23-
// unsafe: `repr(primitive)` implies `repr(Option<NonZero<primitive>>)`
24-
unsafe impl<const MIN: $p, const MAX: $p> AsRepr<Option<NonZero<$p>>>
25-
for $ranged<MIN, MAX>
26-
{
27-
}
28-
29-
// unsafe: `repr(NonZero<primitive>)` implies `repr(primitive)`
30-
unsafe impl<const MIN: $p, const MAX: $p> AsRepr<$p>
31-
for $nonzero<MIN, MAX>
32-
{
33-
}
22+
// unsafe: `repr(primitive)` implies `repr(Option<NonZero<primitive>>)`
23+
unsafe impl<N, P, R> AsRepr<Option<N>> for Ranged<P, R>
24+
where
25+
N: RangeablePrimitive<ZeroablePrimitive = P> + IsNonZero,
26+
P: Primitive,
27+
R: MultiRange<P>,
28+
{
29+
}
3430

31+
macro_rules! as_repr {
32+
($p:ident $(,)?) => {
3533
// unsafe: `repr(primitive)` implies `repr(Option<NonZero<primitive>>)`
36-
unsafe impl<const MIN: $p, const MAX: $p>
37-
AsRepr<Option<$nonzero<MIN, MAX>>> for $ranged<MIN, MAX>
34+
unsafe impl<R> AsRepr<Option<Ranged<NonZero<$p>, R>>> for Ranged<$p, R>
35+
where
36+
R: MultiRange<$p>
3837
{
3938
}
4039

4140
// unsafe: `repr(NonZero<primitive>)` implies `repr(primitive)`
42-
unsafe impl<const MIN: $p, const MAX: $p> AsRepr<$ranged<MIN, MAX>>
43-
for $nonzero<MIN, MAX>
41+
unsafe impl<R> AsRepr<Ranged<$p, R>> for Ranged<NonZero<$p>, R>
42+
where
43+
R: MultiRange<$p>
4444
{
4545
}
4646
};
4747
}
4848

49-
as_repr!(RangedNonZeroU8, RangedU8, u8);
50-
as_repr!(RangedNonZeroU16, RangedU16, u16);
51-
as_repr!(RangedNonZeroU32, RangedU32, u32);
52-
as_repr!(RangedNonZeroU64, RangedU64, u64);
53-
as_repr!(RangedNonZeroU128, RangedU128, u128);
54-
as_repr!(RangedNonZeroI8, RangedI8, i8);
55-
as_repr!(RangedNonZeroI16, RangedI16, i16);
56-
as_repr!(RangedNonZeroI32, RangedI32, i32);
57-
as_repr!(RangedNonZeroI64, RangedI64, i64);
58-
as_repr!(RangedNonZeroI128, RangedI128, i128);
49+
as_repr!(u8);
50+
as_repr!(u16);
51+
as_repr!(u32);
52+
as_repr!(u64);
53+
as_repr!(u128);
54+
as_repr!(i8);
55+
as_repr!(i16);
56+
as_repr!(i32);
57+
as_repr!(i64);
58+
as_repr!(i128);

src/cast/to.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ macro_rules! to {
1717
($nonzero:ident, $type:ident, $p:ty, $f:ty, $g:ty) => {
1818
impl IsNonZero for NonZero<$p> {}
1919

20-
impl<const MIN: $p, const MAX: $p> $nonzero<MIN, MAX> {
20+
impl<Rn> Ranged<NonZero<$p>, Rn>
21+
where
22+
Rn: Range<$p>
23+
{
2124
/// Convert to a new [`Ranged`] type, optionally expanding the
2225
/// range.
2326
///
@@ -53,10 +56,10 @@ macro_rules! to {
5356
where
5457
T: RangeablePrimitive<ZeroablePrimitive = T> + Cmp,
5558
R: Range<T>,
56-
$type<MIN, MAX>: AsPrimitive<T>,
59+
Ranged<$p, Rn>: AsPrimitive<T>,
5760
Ranged<T, R>: AsPrimitive<$p>,
5861
{
59-
let ranged: $type<MIN, MAX> = as_repr::as_repr(self);
62+
let ranged: Ranged<$p, Rn> = as_repr::as_repr(self);
6063

6164
ranged.to_ranged()
6265
}
@@ -99,11 +102,11 @@ macro_rules! to {
99102
RangeablePrimitive<ZeroablePrimitive = T::ZeroablePrimitive>
100103
+ Cmp,
101104
R: Range<T::ZeroablePrimitive>,
102-
$type<MIN, MAX>: AsPrimitive<T::ZeroablePrimitive>,
105+
Ranged<$p, Rn>: AsPrimitive<T::ZeroablePrimitive>,
103106
Ranged<T::ZeroablePrimitive, R>: AsPrimitive<$p>,
104107
Ranged<T::ZeroablePrimitive, R>: AsRepr<Option<Ranged<T, R>>>,
105108
{
106-
let ranged: $type<MIN, MAX> = as_repr::as_repr(self);
109+
let ranged: Ranged<$p, Rn> = as_repr::as_repr(self);
107110
let Some(ranged) = ranged.to_ranged_nonzero() else {
108111
unreachable!()
109112
};
@@ -112,7 +115,10 @@ macro_rules! to {
112115
}
113116
}
114117

115-
impl<const MIN: $p, const MAX: $p> $type<MIN, MAX> {
118+
impl<Rn> Ranged<$p, Rn>
119+
where
120+
Rn: Range<$p>
121+
{
116122
/// Convert to a new [`Ranged`] type, optionally expanding the
117123
/// range.
118124
///
@@ -181,11 +187,11 @@ macro_rules! to {
181187
Ranged::<T, R>::MAX,
182188
);
183189

184-
if cmp::gt(min, MIN) {
190+
if cmp::gt(min, Rn::MIN) {
185191
panic!("minimum must be lower or match");
186192
}
187193

188-
if cmp::lt(max, MAX) {
194+
if cmp::lt(max, Rn::MAX) {
189195
panic!("maximum must be higher or match");
190196
}
191197
} else {
@@ -276,14 +282,20 @@ macro_rules! to {
276282
Ranged::<T::ZeroablePrimitive, R>::MAX,
277283
);
278284

279-
if cmp::gt(min, MIN) && MIN != 0 && min - 1 != 0 {
285+
if cmp::gt(min, Rn::MIN)
286+
&& Rn::MIN != 0
287+
&& min - 1 != 0
288+
{
280289
panic!(
281290
"minimum must be lower or match or exclude \
282291
zero",
283292
);
284293
}
285294

286-
if cmp::lt(max, MAX) && MAX != 0 && max + 1 != 0 {
295+
if cmp::lt(max, Rn::MAX)
296+
&& Rn::MAX != 0
297+
&& max + 1 != 0
298+
{
287299
panic!(
288300
"maximum must be higher or match or exclude \
289301
zero",
@@ -299,7 +311,7 @@ macro_rules! to {
299311
);
300312

301313
if cmp::gt(R::MIN, min)
302-
&& MIN != 0
314+
&& Rn::MIN != 0
303315
&& !cmp::is_one(R::MIN)
304316
{
305317
panic!(
@@ -309,7 +321,7 @@ macro_rules! to {
309321
}
310322

311323
if cmp::lt(R::MAX, max)
312-
&& MAX != 0
324+
&& Rn::MAX != 0
313325
&& !cmp::is_minus_one(R::MAX)
314326
{
315327
panic!(

src/impl_ascii.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use core::ascii::EscapeDefault;
22

3-
use super::{
3+
use crate::{
44
ascii::{Char, Digit, Graphic, Lowercase, NonNul, Uppercase},
5+
range::RangeU8,
56
*,
67
};
78

@@ -130,7 +131,7 @@ impl Digit {
130131
/// );
131132
/// ```
132133
pub const fn from_digit(digit: RangedU8<0, 9>) -> Self {
133-
Self::from_ranged(digit.add::<0x30, 0x30, 0x39>())
134+
Self::from_ranged(digit.add_to::<0x30, RangeU8<0x30, 0x39>>())
134135
}
135136

136137
/// Convert from ASCII digit to numeric digit.
@@ -140,7 +141,8 @@ impl Digit {
140141
/// assert_eq!(Digit::new::<b'5'>().to_digit(), 5);
141142
/// ```
142143
pub const fn to_digit(self) -> RangedU8<0, 9> {
143-
as_repr::as_repr::<RangedU8<0x30, 0x39>>(self).sub::<0x30, 0, 9>()
144+
as_repr::as_repr::<RangedU8<0x30, 0x39>>(self)
145+
.sub_to::<0x30, RangeU8<0, 9>>()
144146
}
145147

146148
/// Convert to [`char`].

0 commit comments

Comments
 (0)