Skip to content

Commit dff2f58

Browse files
committed
refactor: use <integer>: const Ord
`[ref:int_const_ord]` has been resolved by [rust-lang/rust#92390][1]. [1]: rust-lang/rust#92390
1 parent afc66ce commit dff2f58

File tree

10 files changed

+18
-96
lines changed

10 files changed

+18
-96
lines changed

Diff for: doc/toolchain_limitations.md

-15
Original file line numberDiff line numberDiff line change
@@ -562,21 +562,6 @@ const _: () = f::<()>();
562562
```
563563

564564

565-
### `[tag:int_const_ord]` `<integer>: !const Ord`
566-
567-
The standard library doesn't provide `const` trait implementations of `Ord` for the built-in integer types.
568-
569-
```rust
570-
assert!(2i32.max(3) == 3);
571-
```
572-
573-
```rust,compile_fail,E0277
574-
#![feature(const_trait_impl)]
575-
// error[E0277]: the trait bound `i32: ~const Ord` is not satisfied
576-
const _: () = assert!(2i32.max(3) == 3);
577-
```
578-
579-
580565
### `[tag:const_assert_eq]` `assert_eq!` and similar macros are unusable in `const fn`
581566

582567
```rust,compile_fail,E0015

Diff for: src/r3_core/src/hunk.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<System: raw::KernelBase + cfg::KernelStatic, T, InitTag: HunkIniter<T>>
154154
) -> Hunk<System, T> {
155155
let untyped_hunk = kernel::Hunk::<System>::define()
156156
.len(mem::size_of::<T>())
157-
.align(max(mem::align_of::<T>(), self.align))
157+
.align(mem::align_of::<T>().max(self.align))
158158
.finish(cfg);
159159

160160
assert!(self.len == 1, "Non-array hunk must have `len` of `1`");
@@ -195,7 +195,7 @@ impl<System: raw::KernelBase + cfg::KernelStatic, T, InitTag: HunkIniter<T>>
195195

196196
let untyped_hunk = kernel::Hunk::<System>::define()
197197
.len(mem::size_of::<T>() * self.len)
198-
.align(max(mem::align_of::<T>(), self.align))
198+
.align(mem::align_of::<T>().max(self.align))
199199
.finish(cfg);
200200

201201
let start = untyped_hunk.offset();
@@ -347,12 +347,3 @@ unsafe impl<System: raw::KernelBase + cfg::KernelStatic, T: ?Sized>
347347
stable_deref_trait::CloneStableDeref for Hunk<System, T>
348348
{
349349
}
350-
351-
// `Ord::max` is not available in `const fn` [ref:int_const_ord]
352-
const fn max(x: usize, y: usize) -> usize {
353-
if x > y {
354-
x
355-
} else {
356-
y
357-
}
358-
}

Diff for: src/r3_port_arm/src/sp804/cfg.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ pub trait Sp804Options {
116116
/// timer cycles.
117117
///
118118
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
119-
const HEADROOM: u32 = min128(
120-
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
121-
0x40000000,
122-
) as u32;
119+
const HEADROOM: u32 =
120+
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;
123121

124122
/// The interrupt priority of the timer interrupt line.
125123
/// Defaults to `0xc0`.
@@ -128,11 +126,3 @@ pub trait Sp804Options {
128126
/// The timer's interrupt number.
129127
const INTERRUPT_NUM: InterruptNum;
130128
}
131-
132-
const fn min128(x: u128, y: u128) -> u128 {
133-
if x < y {
134-
x
135-
} else {
136-
y
137-
}
138-
}

Diff for: src/r3_port_arm_m/src/systick_tickful/cfg.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@ pub trait SysTickOptions {
2222
/// Defaults to
2323
/// `(FREQUENCY / FREQUENCY_DENOMINATOR / 100).max(1).min(0x1000000)` (100Hz).
2424
const TICK_PERIOD: u32 = {
25-
// `Ord::max` is not available in `const fn` [ref:int_const_ord]
26-
let x = Self::FREQUENCY / Self::FREQUENCY_DENOMINATOR / 100;
27-
if x == 0 {
28-
1
29-
} else if x > 0x1000000 {
30-
0x1000000
31-
} else {
32-
x as u32
33-
}
25+
(Self::FREQUENCY / Self::FREQUENCY_DENOMINATOR / 100)
26+
.max(0)
27+
.min(0x1000000) as u32
3428
};
3529
}
3630

Diff for: src/r3_port_riscv/src/mtime/cfg.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,11 @@ pub trait MtimeOptions {
130130
/// timer cycles.
131131
///
132132
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
133-
const HEADROOM: u32 = min128(
134-
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
135-
0x40000000,
136-
) as u32;
133+
const HEADROOM: u32 =
134+
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;
137135

138136
/// The timer's interrupt number. Defaults to [`INTERRUPT_TIMER`].
139137
///
140138
/// [`INTERRUPT_TIMER`]: crate::INTERRUPT_TIMER
141139
const INTERRUPT_NUM: InterruptNum = crate::INTERRUPT_TIMER;
142140
}
143-
144-
const fn min128(x: u128, y: u128) -> u128 {
145-
if x < y {
146-
x
147-
} else {
148-
y
149-
}
150-
}

Diff for: src/r3_port_riscv/src/sbi_timer/cfg.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,11 @@ pub trait SbiTimerOptions {
109109
/// timer cycles.
110110
///
111111
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
112-
const HEADROOM: u32 = min128(
113-
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
114-
0x40000000,
115-
) as u32;
112+
const HEADROOM: u32 =
113+
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;
116114

117115
/// The timer's interrupt number. Defaults to [`INTERRUPT_TIMER`].
118116
///
119117
/// [`INTERRUPT_TIMER`]: crate::INTERRUPT_TIMER
120118
const INTERRUPT_NUM: InterruptNum = crate::INTERRUPT_TIMER;
121119
}
122-
123-
const fn min128(x: u128, y: u128) -> u128 {
124-
if x < y {
125-
x
126-
} else {
127-
y
128-
}
129-
}

Diff for: src/r3_portkit/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(core_panic)]
66
#![feature(decl_macro)]
77
#![feature(asm_const)]
8+
#![feature(const_cmp)]
89
#![feature(asm_sym)]
910
#![cfg_attr(
1011
feature = "doc",

Diff for: src/r3_portkit/src/num.rs

-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ pub(crate) const fn ceil_div128(x: u128, y: u128) -> u128 {
4141
(x + y - 1) / y
4242
}
4343

44-
/// Get the minimum of two numbers.
45-
pub(crate) const fn min128(x: u128, y: u128) -> u128 {
46-
if x < y {
47-
x
48-
} else {
49-
y
50-
}
51-
}
52-
5344
#[cfg(test)]
5445
mod tests {
5546
extern crate std;

Diff for: src/r3_portkit/src/tickless.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Implements the core algorithm for tickless timing.
2-
use core::fmt;
2+
use core::{cmp::min, fmt};
33
use num_rational::Ratio;
44

55
use crate::{
66
num::{
7-
ceil_div128, floor_ratio128, gcd128, min128, reduce_ratio128,
7+
ceil_div128, floor_ratio128, gcd128, reduce_ratio128,
88
wrapping::{Wrapping, WrappingTrait},
99
},
1010
utils::Init,
@@ -190,7 +190,7 @@ impl TicklessCfg {
190190
{
191191
// If the period is measurable without wrap-around in both ticks,
192192
// the stateless algorithm is applicable.
193-
let repeat = min128(
193+
let repeat = min(
194194
0x1_0000_0000 / hw_global_period,
195195
0x1_0000_0000 / global_period,
196196
);
@@ -277,7 +277,7 @@ impl TicklessCfg {
277277
// late_tick_count <= ref_tick_count + max_tick_count
278278
// )
279279
//
280-
let max_timeout = min128(
280+
let max_timeout = min(
281281
// `late_hw_tick_count <= ref_hw_tick_count + hw_max_tick_count`
282282
((hw_max_tick_count - hw_headroom_ticks) as u128 * *hw_ticks_per_micro.denom())
283283
.saturating_sub(*hw_ticks_per_micro.denom() - 1),

Diff for: src/r3_support_rza1/src/os_timer/cfg.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ pub trait OsTimerOptions {
121121
/// timer cycles.
122122
///
123123
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
124-
const HEADROOM: u32 = min128(
125-
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
126-
0x40000000,
127-
) as u32;
124+
const HEADROOM: u32 =
125+
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;
128126

129127
/// The interrupt priority of the timer interrupt line.
130128
/// Defaults to `0xc0`.
@@ -133,11 +131,3 @@ pub trait OsTimerOptions {
133131
/// OS Timer's interrupt number.
134132
const INTERRUPT_OSTM: InterruptNum = 134;
135133
}
136-
137-
const fn min128(x: u128, y: u128) -> u128 {
138-
if x < y {
139-
x
140-
} else {
141-
y
142-
}
143-
}

0 commit comments

Comments
 (0)