Skip to content

Commit

Permalink
refactor: use <integer>: const Ord
Browse files Browse the repository at this point in the history
`[ref:int_const_ord]` has been resolved by [rust-lang/rust#92390][1]. 

[1]: rust-lang/rust#92390
  • Loading branch information
yvt committed Aug 13, 2022
1 parent afc66ce commit dff2f58
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 96 deletions.
15 changes: 0 additions & 15 deletions doc/toolchain_limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,6 @@ const _: () = f::<()>();
```


### `[tag:int_const_ord]` `<integer>: !const Ord`

The standard library doesn't provide `const` trait implementations of `Ord` for the built-in integer types.

```rust
assert!(2i32.max(3) == 3);
```

```rust,compile_fail,E0277
#![feature(const_trait_impl)]
// error[E0277]: the trait bound `i32: ~const Ord` is not satisfied
const _: () = assert!(2i32.max(3) == 3);
```


### `[tag:const_assert_eq]` `assert_eq!` and similar macros are unusable in `const fn`

```rust,compile_fail,E0015
Expand Down
13 changes: 2 additions & 11 deletions src/r3_core/src/hunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<System: raw::KernelBase + cfg::KernelStatic, T, InitTag: HunkIniter<T>>
) -> Hunk<System, T> {
let untyped_hunk = kernel::Hunk::<System>::define()
.len(mem::size_of::<T>())
.align(max(mem::align_of::<T>(), self.align))
.align(mem::align_of::<T>().max(self.align))
.finish(cfg);

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

let untyped_hunk = kernel::Hunk::<System>::define()
.len(mem::size_of::<T>() * self.len)
.align(max(mem::align_of::<T>(), self.align))
.align(mem::align_of::<T>().max(self.align))
.finish(cfg);

let start = untyped_hunk.offset();
Expand Down Expand Up @@ -347,12 +347,3 @@ unsafe impl<System: raw::KernelBase + cfg::KernelStatic, T: ?Sized>
stable_deref_trait::CloneStableDeref for Hunk<System, T>
{
}

// `Ord::max` is not available in `const fn` [ref:int_const_ord]
const fn max(x: usize, y: usize) -> usize {
if x > y {
x
} else {
y
}
}
14 changes: 2 additions & 12 deletions src/r3_port_arm/src/sp804/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ pub trait Sp804Options {
/// timer cycles.
///
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
const HEADROOM: u32 = min128(
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
0x40000000,
) as u32;
const HEADROOM: u32 =
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;

/// The interrupt priority of the timer interrupt line.
/// Defaults to `0xc0`.
Expand All @@ -128,11 +126,3 @@ pub trait Sp804Options {
/// The timer's interrupt number.
const INTERRUPT_NUM: InterruptNum;
}

const fn min128(x: u128, y: u128) -> u128 {
if x < y {
x
} else {
y
}
}
12 changes: 3 additions & 9 deletions src/r3_port_arm_m/src/systick_tickful/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ pub trait SysTickOptions {
/// Defaults to
/// `(FREQUENCY / FREQUENCY_DENOMINATOR / 100).max(1).min(0x1000000)` (100Hz).
const TICK_PERIOD: u32 = {
// `Ord::max` is not available in `const fn` [ref:int_const_ord]
let x = Self::FREQUENCY / Self::FREQUENCY_DENOMINATOR / 100;
if x == 0 {
1
} else if x > 0x1000000 {
0x1000000
} else {
x as u32
}
(Self::FREQUENCY / Self::FREQUENCY_DENOMINATOR / 100)
.max(0)
.min(0x1000000) as u32
};
}

Expand Down
14 changes: 2 additions & 12 deletions src/r3_port_riscv/src/mtime/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,11 @@ pub trait MtimeOptions {
/// timer cycles.
///
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
const HEADROOM: u32 = min128(
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
0x40000000,
) as u32;
const HEADROOM: u32 =
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;

/// The timer's interrupt number. Defaults to [`INTERRUPT_TIMER`].
///
/// [`INTERRUPT_TIMER`]: crate::INTERRUPT_TIMER
const INTERRUPT_NUM: InterruptNum = crate::INTERRUPT_TIMER;
}

const fn min128(x: u128, y: u128) -> u128 {
if x < y {
x
} else {
y
}
}
14 changes: 2 additions & 12 deletions src/r3_port_riscv/src/sbi_timer/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,11 @@ pub trait SbiTimerOptions {
/// timer cycles.
///
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
const HEADROOM: u32 = min128(
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
0x40000000,
) as u32;
const HEADROOM: u32 =
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;

/// The timer's interrupt number. Defaults to [`INTERRUPT_TIMER`].
///
/// [`INTERRUPT_TIMER`]: crate::INTERRUPT_TIMER
const INTERRUPT_NUM: InterruptNum = crate::INTERRUPT_TIMER;
}

const fn min128(x: u128, y: u128) -> u128 {
if x < y {
x
} else {
y
}
}
1 change: 1 addition & 0 deletions src/r3_portkit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(core_panic)]
#![feature(decl_macro)]
#![feature(asm_const)]
#![feature(const_cmp)]
#![feature(asm_sym)]
#![cfg_attr(
feature = "doc",
Expand Down
9 changes: 0 additions & 9 deletions src/r3_portkit/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ pub(crate) const fn ceil_div128(x: u128, y: u128) -> u128 {
(x + y - 1) / y
}

/// Get the minimum of two numbers.
pub(crate) const fn min128(x: u128, y: u128) -> u128 {
if x < y {
x
} else {
y
}
}

#[cfg(test)]
mod tests {
extern crate std;
Expand Down
8 changes: 4 additions & 4 deletions src/r3_portkit/src/tickless.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Implements the core algorithm for tickless timing.
use core::fmt;
use core::{cmp::min, fmt};
use num_rational::Ratio;

use crate::{
num::{
ceil_div128, floor_ratio128, gcd128, min128, reduce_ratio128,
ceil_div128, floor_ratio128, gcd128, reduce_ratio128,
wrapping::{Wrapping, WrappingTrait},
},
utils::Init,
Expand Down Expand Up @@ -190,7 +190,7 @@ impl TicklessCfg {
{
// If the period is measurable without wrap-around in both ticks,
// the stateless algorithm is applicable.
let repeat = min128(
let repeat = min(
0x1_0000_0000 / hw_global_period,
0x1_0000_0000 / global_period,
);
Expand Down Expand Up @@ -277,7 +277,7 @@ impl TicklessCfg {
// late_tick_count <= ref_tick_count + max_tick_count
// )
//
let max_timeout = min128(
let max_timeout = min(
// `late_hw_tick_count <= ref_hw_tick_count + hw_max_tick_count`
((hw_max_tick_count - hw_headroom_ticks) as u128 * *hw_ticks_per_micro.denom())
.saturating_sub(*hw_ticks_per_micro.denom() - 1),
Expand Down
14 changes: 2 additions & 12 deletions src/r3_support_rza1/src/os_timer/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ pub trait OsTimerOptions {
/// timer cycles.
///
/// Defaults to `min(FREQUENCY * 60 / FREQUENCY_DENOMINATOR, 0x40000000)`.
const HEADROOM: u32 = min128(
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
0x40000000,
) as u32;
const HEADROOM: u32 =
(Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128).min(0x40000000) as u32;

/// The interrupt priority of the timer interrupt line.
/// Defaults to `0xc0`.
Expand All @@ -133,11 +131,3 @@ pub trait OsTimerOptions {
/// OS Timer's interrupt number.
const INTERRUPT_OSTM: InterruptNum = 134;
}

const fn min128(x: u128, y: u128) -> u128 {
if x < y {
x
} else {
y
}
}

0 comments on commit dff2f58

Please sign in to comment.