diff --git a/src/lib.rs b/src/lib.rs index a802ae3..00bbdf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ mod tictoc { seconds: f64, } + #[derive(Clone)] #[pyclass(name = "init")] pub struct Init { time: Instant, @@ -44,23 +45,32 @@ mod tictoc { } } - fn tic(&mut self) { + fn tic(&mut self) -> PyResult { self.time = Instant::now(); self.status = true; + Ok(Init::new()) } - fn toc(&mut self) -> PyResult<()> { - if self.status == false { + fn toc(&mut self, tic: Option) -> PyResult { + let elapsed_time = match tic { + Some(ref tic) => tic.time.elapsed(), + None => self.time.elapsed(), + }; + let status = match tic { + Some(ref _tic) => true, + None => self.status, + }; + if status == false { Err(PyException::new_err("tic() must be called before toc()")) } else { - let elapsed_time = self.time.elapsed(); self.results = Results { nanos: elapsed_time.as_nanos(), micros: elapsed_time.as_micros(), millis: elapsed_time.as_millis(), seconds: elapsed_time.as_secs_f64(), }; - Ok(()) + println!("The elapsed time was {} seconds.",self.results.seconds); + Ok(self.results.clone()) } } } diff --git a/tictoc/__init__.py b/tictoc/__init__.py index 170a70b..9e15bc7 100644 --- a/tictoc/__init__.py +++ b/tictoc/__init__.py @@ -4,6 +4,6 @@ __doc__ = tictoc.__doc__ if hasattr(tictoc, "__all__"): __all__ = tictoc.__all__ -results = tictoc.init(); -tic = results.tic; -toc = results.toc; +t = tictoc.init(); +tic = t.tic; +toc = t.toc;