diff --git a/rust/src/duration.rs b/rust/src/duration.rs index 7eb866743a..6c2e2653d1 100644 --- a/rust/src/duration.rs +++ b/rust/src/duration.rs @@ -31,26 +31,22 @@ fn duration_module(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> { /// Mirrors the `synapse.util.duration.Duration` Python class. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct SynapseDuration { - microseconds: u64, + milliseconds: u64, } impl SynapseDuration { - /// For now we only need to create durations from milliseconds. + /// Creates a `SynapseDuration` from a number of milliseconds. pub const fn from_milliseconds(milliseconds: u64) -> Self { - Self { - // We saturate at u64::MAX microseconds to avoid overflow, which - // means that the maximum duration we can represent is approximately - // 584,942 years. - microseconds: milliseconds.saturating_mul(1_000), - } + Self { milliseconds } } - pub const fn from_hours(hours: u64) -> Self { + /// Creates a `SynapseDuration` from a number of hours. + pub const fn from_hours(hours: u32) -> Self { + // We take a u32 here so that we know the multiplication won't overflow. + // We could instead panic, but that is unstable in a const context (for + // the current MSRV 1.82). Self { - // We saturate at u64::MAX microseconds to avoid overflow, which - // means that the maximum duration we can represent is approximately - // 584,942 years. - microseconds: hours.saturating_mul(3_600_000_000), + milliseconds: (hours as u64) * 3_600_000, } } } @@ -62,7 +58,7 @@ impl<'py> IntoPyObject<'py> for SynapseDuration { fn into_pyobject(self, py: Python<'py>) -> Result { let duration_module = duration_module(py)?; - let kwargs = [("microseconds", self.microseconds)].into_py_dict(py)?; + let kwargs = [("milliseconds", self.milliseconds)].into_py_dict(py)?; let duration_instance = duration_module.call_method("Duration", (), Some(&kwargs))?; Ok(duration_instance.into_bound()) } @@ -75,7 +71,7 @@ impl<'py> IntoPyObject<'py> for &SynapseDuration { fn into_pyobject(self, py: Python<'py>) -> Result { let duration_module = duration_module(py)?; - let kwargs = [("microseconds", self.microseconds)].into_py_dict(py)?; + let kwargs = [("milliseconds", self.milliseconds)].into_py_dict(py)?; let duration_instance = duration_module.call_method("Duration", (), Some(&kwargs))?; Ok(duration_instance.into_bound()) }