dep: use f64 for clock rate

This commit is contained in:
MaxOhn
2024-11-09 21:19:53 +01:00
parent 0448980cd5
commit 9edad8a3d5
4 changed files with 16 additions and 17 deletions
+2 -1
View File
@@ -19,7 +19,8 @@ tracing = ["rosu-map/tracing"]
[dependencies]
# rosu-map = { version = "0.1.2" }
rosu-map = { git = "https://github.com/MaxOhn/rosu-map", branch = "pp-update" }
rosu-mods = { version = "0.1.0" }
# rosu-mods = { version = "0.1.0" }
rosu-mods = { path = "../rosu-mods" }
[dev-dependencies]
proptest = "1.4.0"
+10 -12
View File
@@ -1,7 +1,7 @@
use std::{
borrow::Cow,
fmt::{Debug, Formatter, Result as FmtResult},
num::NonZeroU32,
num::NonZeroU64,
};
use rosu_map::section::general::GameMode;
@@ -51,12 +51,10 @@ pub struct Difficulty {
/// Clock rate will be clamped internally between 0.01 and 100.0.
///
/// Since its minimum value is 0.01, its bits are never zero.
/// Additionally, values between 0.01 and 100 are represented sufficiently
/// precise with 32 bits.
///
/// This allows for an optimization to reduce the struct size by storing its
/// bits as a [`NonZeroU32`].
clock_rate: Option<NonZeroU32>,
/// bits as a [`NonZeroU64`].
clock_rate: Option<NonZeroU64>,
ar: Option<ModsDependent>,
cs: Option<ModsDependent>,
hp: Option<ModsDependent>,
@@ -128,7 +126,7 @@ impl Difficulty {
InspectDifficulty {
mods,
passed_objects,
clock_rate: clock_rate.map(non_zero_u32_to_f32).map(f64::from),
clock_rate: clock_rate.map(non_zero_u64_to_f64),
ar,
cs,
hp,
@@ -171,11 +169,11 @@ impl Difficulty {
/// | :-----: | :-----: |
/// | 0.01 | 100 |
pub fn clock_rate(self, clock_rate: f64) -> Self {
let clock_rate = (clock_rate as f32).clamp(0.01, 100.0).to_bits();
let clock_rate = clock_rate.clamp(0.01, 100.0).to_bits();
// SAFETY: The minimum value is 0.01 so its bits can never be fully
// zero.
let non_zero = unsafe { NonZeroU32::new_unchecked(clock_rate) };
let non_zero = unsafe { NonZeroU64::new_unchecked(clock_rate) };
Self {
clock_rate: Some(non_zero),
@@ -334,7 +332,7 @@ impl Difficulty {
pub(crate) fn get_clock_rate(&self) -> f64 {
let clock_rate = self
.clock_rate
.map_or(self.mods.clock_rate(), non_zero_u32_to_f32);
.map_or(self.mods.clock_rate(), non_zero_u64_to_f64);
f64::from(clock_rate)
}
@@ -369,8 +367,8 @@ impl Difficulty {
}
}
fn non_zero_u32_to_f32(n: NonZeroU32) -> f32 {
f32::from_bits(n.get())
fn non_zero_u64_to_f64(n: NonZeroU64) -> f64 {
f64::from_bits(n.get())
}
impl Debug for Difficulty {
@@ -390,7 +388,7 @@ impl Debug for Difficulty {
f.debug_struct("Difficulty")
.field("mods", mods)
.field("passed_objects", passed_objects)
.field("clock_rate", &clock_rate.map(non_zero_u32_to_f32))
.field("clock_rate", &clock_rate.map(non_zero_u64_to_f64))
.field("ar", ar)
.field("cs", cs)
.field("hp", hp)
+2 -2
View File
@@ -417,9 +417,9 @@ impl ModsDependentKind {
}
}
fn value(&self, mods: &GameMods, mods_fn: impl Fn(&GameMods) -> Option<f32>) -> f32 {
fn value(&self, mods: &GameMods, mods_fn: impl Fn(&GameMods) -> Option<f64>) -> f32 {
match self {
ModsDependentKind::Default(inner) => mods_fn(mods).unwrap_or(inner.value),
ModsDependentKind::Default(inner) => mods_fn(mods).map_or(inner.value, |n| n as f32),
ModsDependentKind::Custom(inner) => inner.value,
}
}
+2 -2
View File
@@ -61,7 +61,7 @@ impl GameMods {
///
/// In case of variable clock rates like for `WindUp`, this will return
/// `1.0`.
pub(crate) fn clock_rate(&self) -> f32 {
pub(crate) fn clock_rate(&self) -> f64 {
match self.inner {
GameModsInner::Lazer(ref mods) => mods.clock_rate().unwrap_or(1.0),
GameModsInner::Intermode(ref mods) => mods.legacy_clock_rate(),
@@ -105,7 +105,7 @@ macro_rules! impl_map_attr {
#[doc = "Check whether the mods specify a custom "]
#[doc = $s]
#[doc = "value."]
pub(crate) fn $fn(&self) -> Option<f32> {
pub(crate) fn $fn(&self) -> Option<f64> {
match self.inner {
GameModsInner::Lazer(ref mods) => mods.iter().find_map(|gamemod| match gamemod {
$( impl_map_attr!( @ $mode $field) => *$field, )*