Skip to content

Commit 3f178e1

Browse files
committed
Create a SynapseDuration rust class
This makes it easier to create a `Duration` instance in Rust.
1 parent 6078d5a commit 3f178e1

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

rust/src/duration.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is licensed under the Affero General Public License (AGPL) version 3.
3+
*
4+
* Copyright (C) 2025 Element Creations, Ltd
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* See the GNU Affero General Public License for more details:
12+
* <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
*/
14+
15+
use once_cell::sync::OnceCell;
16+
use pyo3::{
17+
types::{IntoPyDict, PyAnyMethods},
18+
Bound, BoundObject, IntoPyObject, Py, PyAny, PyResult, Python,
19+
};
20+
21+
/// A reference to the `synapse.util.duration` module.
22+
static DURATION: OnceCell<Py<PyAny>> = OnceCell::new();
23+
24+
/// Access to the `synapse.util.duration` module.
25+
fn duration_module(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> {
26+
Ok(DURATION
27+
.get_or_try_init(|| py.import("synapse.util.duration").map(Into::into))?
28+
.bind(py))
29+
}
30+
31+
/// Mirrors the `synapse.util.duration.Duration` Python class.
32+
pub struct SynapseDuration {
33+
microseconds: u64,
34+
}
35+
36+
impl SynapseDuration {
37+
pub fn from_milliseconds(milliseconds: u64) -> Self {
38+
Self {
39+
microseconds: milliseconds * 1_000,
40+
}
41+
}
42+
}
43+
44+
impl<'py> IntoPyObject<'py> for &SynapseDuration {
45+
type Target = PyAny;
46+
type Output = Bound<'py, Self::Target>;
47+
type Error = anyhow::Error;
48+
49+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
50+
let duration_module = duration_module(py)?;
51+
let kwargs = [("microseconds", self.microseconds)].into_py_dict(py)?;
52+
let duration_instance = duration_module
53+
.call_method("Duration", (), Some(&kwargs))
54+
.map_err(|e| anyhow::anyhow!("Failed to create Duration instance: {}", e))?;
55+
Ok(duration_instance.into_bound())
56+
}
57+
}

0 commit comments

Comments
 (0)