[2024-12-16] Changing to mod syntax in Rust

This commit is contained in:
Andrew Conlin 2024-12-16 11:09:34 +00:00
parent 6a4e818144
commit c47dd577f5

View File

@ -1,10 +1,14 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use std::time::Instant;
use pyo3::exceptions::PyException;
#[pyclass] #[pymodule]
#[derive(Clone)] mod tictoc {
struct Results { use super::*;
use std::time::Instant;
use pyo3::exceptions::PyException;
#[pyclass]
#[derive(Clone)]
struct Results {
#[pyo3(get)] #[pyo3(get)]
nanos: u128, nanos: u128,
#[pyo3(get)] #[pyo3(get)]
@ -13,18 +17,18 @@ struct Results {
millis: u128, millis: u128,
#[pyo3(get)] #[pyo3(get)]
seconds: f64, seconds: f64,
} }
#[pyclass(module = "tictoc", name = "init")] #[pyclass(name = "init")]
struct Init { pub struct Init {
time: Instant, time: Instant,
#[pyo3(get)] #[pyo3(get)]
results: Results, results: Results,
status: bool, status: bool,
} }
#[pymethods] #[pymethods]
impl Init { impl Init {
#[new] #[new]
fn new() -> Self { fn new() -> Self {
let res = Results { let res = Results {
@ -59,34 +63,28 @@ impl Init {
Ok(()) Ok(())
} }
} }
} }
#[test]
#[pymodule] fn test_new() {
fn tictoc(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<Init>()?;
Ok(())
}
#[test]
fn test_new() {
let init = Init::new(); let init = Init::new();
assert_eq!(init.results.nanos,0); assert_eq!(init.results.nanos,0);
} }
#[test] #[test]
fn test_tic() { fn test_tic() {
let mut init = Init::new(); let mut init = Init::new();
let time1 = init.time; let time1 = init.time;
init.tic(); init.tic();
let time2 = init.time; let time2 = init.time;
assert!(time2 > time1) assert!(time2 > time1)
} }
#[test] #[test]
fn test_toc() { fn test_toc() {
let mut init = Init::new(); let mut init = Init::new();
init.tic(); init.tic();
println!("{}","test"); println!("{}","test");
let _ = init.toc(); let _ = init.toc();
assert!(init.results.nanos > 0) assert!(init.results.nanos > 0)
}
} }